Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Madan_Para's avatar

How to solve this error "Laravel\Socialite\Two\InvalidStateException"

I am trying to integrate Google Login in my web app. When I enter my google account and password, after that the upper error comes. I don't know why this error is coming. I have used some of the solutions in the Stackoverflow but it is still unsolved.

My GoogleController is :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Socialite;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

class GoogleController extends Controller
{
    public function redirectToGoogle()
    {
        return Socialite::driver('google')->redirect();
    }

    public function handleGoogleCallback()
    {
        try{

            $user = Socialite::driver('google')->user();
            //dd($user);

            $finduser = User::where('google_id',$user->id)->first();

            if($finduser) {
                
                Auth::login($finduser);

                return redirect('home');
            }else{
                $newUser = User::create([
                    'name' => $user->name,
                    'email' => $user->email,
                    'google_id' => $user->id,
                    'password' => encrypt('123456dummy')
                ]);

                Auth::login($newUser);

                return redirect('home');
            }
        } catch(Exception $e) {
            dd($e->getMessage());
        }
    }
}

Please tell me any solution .

0 likes
2 replies

Please or to participate in this conversation.