mercredi 20 juillet 2016

Laravel 5.1 Intermittent State Exception

There have been several posts on SO regarding this issue and having tried all of the solutions I've found, I'm confident that there's specifically something wrong with my code. Here's the current iteration of my social login code for Google.

To be clear, this code works about 90% of the time. For about 10% of users on my site, they're triggering invalid state exception errors and can't log in.

I plan to rebuild the entire site on Laravel 5.3 next month so I just need this fix to last 30 days.

Here's my code:

    public function create()
    {
        return Socialite::driver('facebook')->redirect();
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Request $request
     * @return Response
     */
    public function store(Request $request)
    {
        $socialUser = Socialite::driver('facebook')->user();

        $user = User::firstOrNew(['socialite_provider' => 'facebook', 'socialite_id' => $socialUser->getId()]);
        $user->socialite_provider = 'facebook';
        $user->socialite_id = $socialUser->getId();
        $user->socialite_nickname = $socialUser->getNickname();
        $user->socialite_name = $socialUser->getName();
        $user->socialite_avatar = $socialUser->getAvatar();
        $user->socialite_email = $socialUser->getEmail();
        $user->save();

        /*
         * Hack to fix invalid state error, I think this helped a little but it's still not working for all users.
         */
        $state = $request->get('state');
        $request->session()->put('state', $state);
        if (Auth::check() == false) {
            session()->regenerate();
        }

        // Update orders so that we don't lose our guest cart
        $oldSessionId = Session::getId();

        Auth::login($user, true);

        $newSessionId = Session::getId();
        Order::updateSession($oldSessionId, $newSessionId);

        return redirect()->intended('/');
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire