8 comments

    • Thank you Samuel!

      Since Laravel Sanctum is used, logout should be pretty straight forward. You basically need to:

      1. Revoke token using Revoking Tokens
      2. Clear browser local storage/cookies

      Let me know if this worked for you or if you need additional assistance.

  • Hi! Thanks for this. I was completely lost and it was really helpful.
    I have a doubt though. I implemented this exactly as you said but I’m getting this error:

    GuzzleHttp\Exception\ClientException: Client error: `POST https://oauth2.googleapis.com/token` resulted in a `400 Bad Request` response:
    {
    “error”: “invalid_grant”,
    “error_description”: “Bad Request”
    }

    I’ve been trying to debug it and searched for hours in google but couldn’t find a solution. Did you, by any chance, encounter this error?
    I think google is calling handleAuthCallback two times because i can see briefly the correct returned data in the browser before seeing ‘Invalid credentials provided.’

    Thanks again for this post.

    • Thank you Débora!

      This looks like backend issue (because of guzzle). handleAuthCallback must be called from react app, not by Google.

      The flow should be the following:
      1. React calls Laravel /auth to get redirect url
      2. Laravel sends back redirect url and React adds it to the button
      3. User clicks on button and is redirected to Google
      4. User logs in and is redirected back to React (that is configured in Laravel .env var GOOGLE_REDIRECT_URI)
      5. React receives data on /auth/google endpoint (GoogleCallback component)
      6. React calls Laravel /auth/callback and sends data it received from google callback along the way (that’s ${location.search} part)
      7. Laravel can, from that info React sent along the way, get the user data with Socialite::driver(‘google’)->stateless()->user(); – take note that this is google user, not local user from users table
      8. Once Laravel has the user it firstOrCreate the user locally and issues access token ($user->createToken(‘google-token’)->plainTextToken)
      9. That token, along with other user data, is returned to React
      10. React uses that token to authorize every next request to Laravel (fetchUserData function)

      My best guess is that something is not correctly configured in Google. Most probably user type in consent screen is configured as internal and you are trying to log in with external user. I would suggest to try to reconfigure Google from scratch following the steps exactly.

      If you are still having troubles, feel free to contact me directly at [email protected].

    • Did you followed the guide correctly? Specifically referring to this point – “GOOGLE_REDIRECT_URI is the same as the one you configured in “Authorized Redirect URI” in Google project. This points to React App!!”

      You must configure GOOGLE_REDIRECT_URI correctly to point to frontend app instead of Laravel server.

  • I have an issue, sometimes it works and sometimes it doesn’t. It usually returns invalid credentials but when I make changes in the codes and save. It works. What could be the reason?

Leave a Reply

Your email address will not be published. Required fields are marked *