i want to login with gmail, i create laravel authentication, i add button login with google, i install laravel socialite package by command composer require laravel/socialite, i add configuration in config/services:
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => 'http://localhost:8000/laravel-socialite/public/login/google/callback',
],
I added in routes/web.php the routes:
Route::get ('login/google',[App\Http\Controllers\Auth\LoginController::class, 'redirectToGoogle'])->name('login.google');
Route::get('login/google/callback',[App\Http\Controllers\Auth\LoginController::class,'handleGoogleCallback']);
I put in the button "login with google" url:
<a href="{{ route('login.google') }}" class="btn btn-danger btn-block">login with Google</a>
I put id and variable key in the .env file:
GOOGLE_CLIENT_ID="759714452408-gcbbdff9d7tp6215vgla9qhvrf60juh8.apps.googleusercontent.com"
GOOGLE_CLIENT_SECRET="ZKgJSqgibBNcHzD9vAMB80cw"
I created the _registerOrLoginUser function in loginController:
protected function _registerOrLoginUser ($ data)
{
$ user = User :: where ('email', '=', $ data-> email) -> first ();
if (! $ user) {
$ user = new User ();
$ user-> name = $ data-> name;
$ user-> email = $ data-> email;
$ user-> provider_id = $ data-> provider_id;
$ user-> avatar = $ data-> avatar;
$ user-> save ();
}
Auth :: login ($ user);
}
I added the two function in LoginController.php and called the _registerOrLoginUser function and redirect to "home".
public function redirectToGoogle () {
return Socialite :: driver ('google') -> redirect ();
}
public function handleGoogleCallback () {
$ user = Socialite :: driver ('google') -> user ();
$ this -> _ registerOrLoginUser ($ user);
return redirect () -> route ('home');
}
I created google application to get id and sercret.
database/table users migrations:
public function up ()
{
Schema :: create ('users', function (Blueprint $ table) {
$ table-> id ();
$ table-> string ('name');
$ table-> string ('email');
$ table-> timestamp ('email_verified_at') -> nullable ();
$ table-> string ('password');
$ table-> string ('provider_id') -> nullable ();
$ table-> string ('avatar') -> nullable ();
$ table-> rememberToken ();
$ table-> timestamps ();
});
}
and finally when I click on the button "login with google"
it opens gmail and I choose my account,it give me error:
cURL error 77: error setting certificate verify locations
.
even I have already downloaded cacert.pem file and copy it to the root D:\wamp\bin\php \php7.3.5\extras\ssl
and I modify php.ini
.
I find the solution is to disable guzzle ssl, so add the code:
$user->get('/url', ['verify' => false]);
but i dont know where.