Truy cập vào link sau để đăng ký secret key và client key: Google Console
Trong Authorized JavaScript origins ta khai báo
http://localhost:8000
Trong Authorized redirect URIs ta khai báo
http://localhost:8000/signin-google
Sau khi cấu hình trên Google console ta được Client Key và Secret Key, ta khai báo trong .env như sau
GOOGLE_CLIENT_ID=client_id
GOOGLE_CLIENT_SECRET=client_secret
GOOGLE_REDIRECT=http://localhost:8000/auth/google-signin
composer require laravel/socialite
Cấu hình trong config/services.php như sau:
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => env('GOOGLE_REDIRECT'),
],
class User extends Authenticatable{
protected $fillable = [
'id',
'name',
'email',
'password',
];
protected function casts(): array{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'id' => 'string',
];
}
}
Schema::create('users', function (Blueprint $table) {
$table->string('id', 32)->primary();
$table->string('name', 32);
});
Route::get('/auth/google', function () {
return Socialite::driver('google')->redirect();
});
Route::get('/auth/google-signin', function () {
$googleUser = Socialite::driver('google')->user();
$user = User::updateOrCreate([
'id' => $googleUser->id,
], [
'id' => $googleUser->id,
'name' => $googleUser->name,
'email' => $googleUser->email,
'password' => Str::random(16),
]);
Auth::login($user);
return redirect('/dashboard');
});
Turn off verify ssl in vender/guzzlehttp/guzzle/Client.php
private function configureDefaults(array $config): void{
$defaults = [
'verify' => false,
];
}