1

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.

achraf bourki
  • 159
  • 2
  • 11
  • Does this help: https://stackoverflow.com/questions/28066409/how-to-ignore-invalid-ssl-certificate-errors-in-guzzle-5 – Techno Jul 29 '21 at 10:50

1 Answers1

1

To answer your question, your local development environment isn't serving up an SSL certificate. Depending on your setup you can configure this, but it's really easier to simply ignore it during development as you have suggested. You were very close to the solution. Here's how you can configure that. I'm only applying it if the website isn't be served in a production environment.

public function handleGoogleCallback () {
    if (app()->environment('local')) {
        $user = Socialite::driver('google')
            ->setHttpClient(new \GuzzleHttp\Client(['verify' => false]))
            ->user();
    } else {
        $user = Socialite::driver('google')->user();
    }
    $this->_registerOrLoginUser($user);
    
    return redirect()->route('home');
}

Additionally, when you define your redirect URL, you may specify it as a relative URL so that it doesn't have your local URL structure hardcoded.

'google' => [
    'client_id' => env('GOOGLE_CLIENT_ID'),
    'client_secret' => env('GOOGLE_CLIENT_SECRET'),
    'redirect' => '/laravel/socialite/public/login/google/callback',
],

In Google Cloud Console, you can edit your OAuth credentials and list as many allowable redirect URLs as you like. Simply add one for each domain you will be serving your website from. Since the redirect happens locally in the browser, adding proxy domains or localhost works just fine. As long as your local system is configured to resolve the address, it will be served back to the browser during the OAuth flow.

Nilpo
  • 4,675
  • 1
  • 25
  • 39