Skip to content

devsohelrana/GSocialite

Repository files navigation

Getting Started with Socialite Package in Laravel 11 | Visual Dev - Elegant Coding

Create Laravel New Project

composer create laravel/laravel ProjectName

or

composer global require laravel/installer

laravel new ProjectName

Database create and configure .env database name.

php artisan migrate
npm install
npm run dev

Laravel Socialite

composer require laravel/socialite

Configuration config/services.php

'google' => [
    'client_id' => env('GOOGLE_CLIENT_ID'),
    'client_secret' => env('GOOGLE_CLIENT_SECRET'),
    'redirect' => env('GOOGLE_REDIRECT_URL'),
],

.env

GOOGLE_CLIENT_ID= // Google Client ID
GOOGLE_CLIENT_SECRET= // Google Client Secret
GOOGLE_REDIRECT_URL= // Google Callback Url // http://localhost:8000/auth/google/callback

Create Controller GSocialiteController

php artisan make:controller GSocialiteController

Create Route web.php

Route::post('/auth/google/redirect', [GSocialiteController::class, 'redirect'])->name('google.auth');
Route::get('/auth/google/callback', [GSocialiteController::class, 'callback']);

GoogleAuthController

public function redirect() {
	return Socialite::driver('google')->redirect();
}

public function callback() {
	$googleUser = Socialite::driver('google')->user();

	$user = User::firstOrCreate([
		'email' => $googleUser->email,
	], [
		'name' => $googleUser->name,
		'email' => $googleUser->email,
		'password' => 'password',
		'avatar' => $googleUser->avatar,
		'email_verified_at' => $googleUser->email_verified_at,
	]);

	Auth::login($user);
	return redirect('/dashboard');
}

Edit user table && add filed

$table->string('avatar')->nullable();

Edit user model

protected $fillable = [
	'name',
	'email',
	'password',
	'avatar',
];

Edit login.blade.php file

<div>
    <x-primary-button class="mt-5">
        <a href="{{ route('google.auth') }}">
            {{ __('Log in with google') }}
        </a>
    </x-primary-button>
</div>

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages