-
Notifications
You must be signed in to change notification settings - Fork 10
2.x Usage Laravel
Sam edited this page Apr 28, 2019
·
4 revisions
- Create a controller
php artisan make:controller Auth/SteamLoginController
-
Extend
kanalumaddela\LaravelSteamLogin\Http\Controllers\AbstractSteamLoginController
.- An example is shown below. You can override the other methods if you choose to handle this a different way such as suppressing exceptions.
- If you choose to handle post steam login differently (e.g. redirecting elsewhere) you can return a
RedirectResponse
. - If anything is returned, that will be used instead of the default redirect to previous page.
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use kanalumaddela\LaravelSteamLogin\Http\Controllers\AbstractSteamLoginController;
use kanalumaddela\LaravelSteamLogin\SteamUser;
class SteamLoginController extends AbstractSteamLoginController
{
/**
* {@inheritdoc}
*/
public function authenticated(Request $request, SteamUser $steamUser)
{
// find user by their steam account id, example assumes `steam_account_id` on `users` table
$user = User::where('steam_account_id', $steamUser->accountId)->first();
// if the user doesn't exist, create them
if (!$user) {
$steamUser->getUserInfo(); // retrieve and set user info pulled from steam
$user = User::create([
'name' => $steamUser->name, // personaname
'steam_account_id' => $steamUser->accountId,
]);
}
// login the user using the Auth facade
Auth::login($user);
// let the extended controller handle redirect back to the page the user was just on
}
}
- Add the following routes to your
routes/web.php
. If you choose to use different route names, be sure to properly set those names in the config in the.env
file so everything runs smoothly.
Route::get('login/steam', 'Auth\SteamLoginController@login')->name('login.steam');
Route::get('auth/steam', 'Auth\SteamLoginController@authenticate')->name('auth.steam'); // callback route when returning from steam
// or (this applies to v2.4+)
use App\Http\Controllers\Auth\SteamLoginController;
use kanalumaddela\LaravelSteamLogin\Facades\SteamLogin;
SteamLogin::routes(['controller' => SteamLoginController::class]);
- If you ARE NOT using Laravel's built in
Auth::routes()
, be sure to add a logout route
// Logout routes should not be GET unless you are passing the csrf token as a query param.
Route::post('logout', 'Auth\LoginController@logout')->name('logout');