-
composer.json
Open in GitHub{ // "require": { "php": "^7.4", // "laravel/socialite": "5.0.1", // }, // }
-
app/Http/Controllers/Auth/AuthController.php
Open in GitHubuse App\Http\Controllers\Controller; use App\Models\User; use Auth; use Socialite; class AuthController extends Controller { public function redirectToProvider($provider) { return Socialite::driver($provider)->redirect(); } public function handleProviderCallback($provider) { try { $user = Socialite::driver($provider)->stateless()->user(); } catch (Exception $e) { return redirect()->route('login'); } $authUser = $this->findOrCreateUser($user, $provider); Auth::login($authUser, true); return redirect()->route('home')->withSuccess(__('auth.logged_in_provider', ['provider' => $provider])); } private function findOrCreateUser($user, $provider) { $authUser = User::where('provider_id', $user->id)->first(); if ($authUser) { return $authUser; } return User::create([ 'name' => $user->name ?? $user->email, 'email' => $user->email, 'provider' => $provider, 'provider_id' => $user->id ]); } }
-
resources/views/auth/login.blade.php
Open in GitHub// ... <div class="d-flex justify-content-between flex-wrap"> @if (env('GITHUB_ID')) <a href="{{ route('auth.provider', ['provider' => 'github']) }}" class="btn btn-secondary mb-2"> @lang('auth.services.github') <i class="fa fa-github" aria-hidden="true"></i> </a> @endif @if (env('TWITTER_ID')) <a href="{{ route('auth.provider', ['provider' => 'twitter']) }}" class="btn btn-secondary mb-2"> @lang('auth.services.twitter') <i class="fa fa-twitter" aria-hidden="true"></i> </a> @endif </div> // ...
-
routes/auth.php
Open in GitHubuse Illuminate\Support\Facades\Route; // Route::prefix('auth')->group(function () { Route::get('{provider}', 'Auth\AuthController@redirectToProvider')->name('auth.provider'); Route::get('{provider}/callback', 'Auth\AuthController@handleProviderCallback'); }); //