laravel socialite google login / google auth, callback "404 not found"

Viewed 371

i have this on link pop up

<a href="login/google" class="btn btn-white btn-outline-white"><span class="fa fa-google">oogle</span></a>

My route

Route::get('login/google', [GoogleController::class, 'login']);
Route::get('login/google/callback', [GoogleController::class, 'callback']);

Route::middleware(['auth'])->group(function () {
    Route::get('logout', [GoogleController::class, 'logout']);
    Route::get('user', [UserController::class, 'index']);
});

authorize redirect url from google cloud platform

enter image description here

services.php

  'google' => [
    'client_id' => env('GOOGLE_CLIENT_ID'),
    'client_secret' => env('GOOGLE_CLIENT_SECRET'),
    'redirect' => 'http://127.0.0.1:8000/login/google/callback',

my controller

public function login() {
    return Socialite::driver('google')->redirect();
}
public function callback() {
    try {
        $google_user = Socialite::driver('google')->user();
        $user = User::where('email', $google_user->email)->first();
        if($user) {
            Auth::login($user);
            return redirect('user');
        }
        else {
            $new_user = User::create([
                'name'=> ucwords($google_user->name),
                'email'=> $google_user->email,
                'email_verified_at'=> now(),
                'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi',
                'remember_token'=> Str::random(10),
            ]);
            Auth::login($new_user);
            return redirect('user'); 
        }

       
    } catch (\Throwable $th) { 
       abort(404);
    }
}

but still got this message enter image description here

i hope you guys can help me, i have to finish this before my thesis defence

2 Answers
use ->stateless() on the following lines
 
return Socialite::driver('google')->stateless()->redirect();

$google_user = Socialite::driver('google')->stateless()->user();

If that doesn't work you could print out the error message and post it so that we can get a clear understanding of your error.

Use catch (Exception $e) {dd($e->getMessage());}

instead of catch (\Throwable $th) { abort(404);}

Thanks for trying to help @SachithMuhandiram, the problem is array to string conversion when i try to dump,

this is user controller, problematic code is here

public function index()

{

    return view('index'. [

        'menus' => Menus::get(), **redline here**
        'most' => Menus::where('most','2')->get(), **redline here**
        'categories' => Categories::get(), **redline here**
        'user' => Auth::user() **redline here**

    ]);

}

this is my user model, and i protected this

` protected $fillable = [ 'name', 'email', 'avatar', 'occupation', 'is_admin' ];

/**
 * The attributes that should be hidden for serialization.
 *
 * @var array<int, string>
 */
protected $hidden = [
    'password',
    'remember_token',
];

/**
 * The attributes that should be cast.
 *
 * @var array<string, string>
 */
protected $casts = [
    'email_verified_at' => 'datetime',
];

` Index.php

 @auth  
      @foreach ($user as $user )
        <tr>
          <td>{{ $user->name }}</td>
        </tr>
      @endforeach
      @endauth

i dont know how to fix it, i search and there's not same case

Related