How to Add a Google Login using Socialite on Laravel 8 with Jetstream

composer create-project --prefer-dist laravel/laravel socialite
composer require laravel/jetstream
php artisan jetstream:install livewire --teamsnpm install && npm run dev

php artisan migrate
composer require laravel/socialite
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => 'http://your-callback-url',
],
php artisan make:migration add_google_id_column
protected $fillable = [
'name',
'email',
'password',
'google_id'
];
Route::get('auth/google', [LoginController::class, 'redirectToGoogle']);
Route::get('auth/google/callback', [LoginController::class, 'handleGoogleCallback']);
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Laravel\Socialite\Facades\Socialite;
use Illuminate\Support\Facades\Auth;
use Exception;
use App\Models\User;
use App\Models\Team;

class LoginController extends Controller
{
/**
* Redirect the user to the GitHub authentication page.
*
*
@return \Illuminate\Http\Response
*/
public function redirectToGoogle()
{
return Socialite::driver('google')->redirect();
}

/**
* Obtain the user information from GitHub.
*
*
@return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Routing\Redirector
*/
public function handleGoogleCallback()
{
try {
//create a user using socialite driver google
$user = Socialite::driver('google')->user();
// if the user exits, use that user and login
$finduser = User::where('google_id', $user->id)->first();
if($finduser){
//if the user exists, login and show dashboard
Auth::login($finduser);
return redirect('/dashboard');
}else{
//user is not yet created, so create first
$newUser = User::create([
'name' => $user->name,
'email' => $user->email,
'google_id'=> $user->id,
'password' => encrypt('')
]);
//every user needs a team for dashboard/jetstream to work.
//create a personal team for the user
$newTeam = Team::forceCreate([
'user_id' => $newUser->id,
'name' => explode(' ', $user->name, 2)[0]."'s Team",
'personal_team' => true,
]);
// save the team and add the team to the user.
$newTeam->save();
$newUser->current_team_id = $newTeam->id;
$newUser->save();
//login as the new user
Auth::login($newUser);
// go to the dashboard
return redirect('/dashboard');
}
//catch exceptions
} catch (Exception $e) {
dd($e->getMessage());
}
}
}
<a href="{{ url('auth/google') }}" style="margin-top: 0px !important;background: green;color: #ffffff;padding: 5px;border-radius:7px;" class="ml-2 btn-google">
<strong>
Login With Google</strong>
</a>

--

--

--

System designer and administrator.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

React Firebase: Resize image on client side (for free)

BAND | bitkub.com/market/BAND

Android app to reset your old smart TV’s pin number — Panasonic Viera

A Step-By-Step Guide to Creating a Twitter Clone With React and Supabase — Part 1

Reusable Directive For Authorisation in Angular, no more *ngIf!!

A-Z Logging for your node application

How to disable Nextjs Telemetry data collected

Checking the licenses about 1200 JavaScript packages

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cody Rigg

Cody Rigg

System designer and administrator.

More from Medium

PHP cookies with NextJS app

🔥Get to Know LaraconTZ (Virtual Event) 🔥 Speakers on April 03rd, 2022✅

Top PHP Development Trends That Will Dominate in 2022

Laravel and Vue: A Deadly Combination for Web Application Development