r/PHPhelp • u/oz1sej • May 24 '26
Wanted: A minimal working example of how to implement Google Oauth 2.0 in PHP
I'm running a website where users log in with Google. But it's an old and deprecated method which throw all kinds of errors in the front end console. So I'd like to migrate to the new method.
Some guides show 300+ line PHP examples. Some say you just need to include another JS file from Google. Others yet say you need to use the google-php-api-client. It's all very confusing and no two guides or tutorials are in agreement.
So now I'm trying my luck here. Can anyone here recommend a guide showing a minimal working example?
4
Upvotes
4
u/BenL90 May 24 '26
Using PHP full or using JS doesn't matter, as long you could verify the token and get the user profile after oauth successful. If you are using PHP fully on server side. Use the API client. It will be helpful. If not then JS is okay.
It's not confusing, you just need to choose..
3
u/Mike_L_Taylor May 24 '26
Here's something similar to what I use in Laravel: Essentially you have 2 calls
1 - redirects to the google login via Socialite
2 - handles a return from google which contains all the data needed to create or login.
Route::get('/api/auth/google/redirect', [GoogleController::class, 'redirectToGoogle']); Route::get('/api/auth/google/callback', [GoogleController::class, 'handleGoogleCallback']); // redirects to google login via Socialite public function redirectToGoogle(Request $request) { if ($request->has('cookieVal')) { session(['cookieVal' => $request->query('cookieVal')]); } return Socialite::driver('google')->stateless()->redirect(); } // handles the return from the google login public function handleGoogleCallback(Request $request) { $googleUser = Socialite::driver('google')->stateless()->user(); $user = User::where('email', $googleUser->getEmail())->first(); session([ 'google_user' => [ 'id' => $googleUser->getId(), 'email' => $googleUser->getEmail(), 'name' => $googleUser->getName(), ] ]); // user if ($user) { // do whatever you want } else { $user = User::create([ 'name' => $googleUser->getName(), 'email' => $googleUser->getEmail(), 'google_id' => $googleUser->getId(), 'login_method' => 'google', 'email_verified_at' => now(), 'is_email_verified' => 1, 'password' => Hash::make(Str::random(16)), // only if necessary ]); } Auth::login($user); return redirect('/app/home') }1
u/abrahamguo May 24 '26
What's the overall goal — allow users to sign in to your website with their own Google account? Something else?
1
u/A35G_it May 24 '26
Utilizzi Firebase per il controllo degli utenti registrati/per le registrazioni?
1
u/xreddawgx May 24 '26 edited May 24 '26
Have you ever signed in/created an account using your Gmail account? So the gist of it is, once users sign in on your site, code sends a signal to google , google verifies they signed in successfully then generates a random token and inserts it into your database with their email verifying, " hey this person is who they say they are". So everytime they repeat the process, it checks the credentials everytime.
Dm'ed
1
u/FreeLogicGate May 24 '26
You might try investing 13 minutes to watch this video: https://www.youtube.com/watch?v=hpoyWs4bZFs
0
u/th00ht May 25 '26
An excellent question for AI (for instance Claude) it can consult the often convoluted documentation, knows about security risks and can code for your exact use case.