Linked Questions

Popular Questions

I am using facebook as the login of my application. After the user successfully logs in, they are redirected to my user homepage. But when I try to refresh the page after log in it throws an error:

Laravel \ Socialite \ Two \ InvalidStateException No message

This is the code displayed:

public function user()
{
    if ($this->hasInvalidState()) {
        throw new InvalidStateException; // this line is highlighted
    }

    $response = $this->getAccessTokenResponse($this->getCode());

    $user = $this->mapUserToObject($this->getUserByToken(
        $token = Arr::get($response, 'access_token')
    ));

    return $user->setToken($token)
                ->setRefreshToken(Arr::get($response, 'refresh_token'))
                ->setExpiresIn(Arr::get($response, 'expires_in'));
}

This is my Controller code:

<?php
 namespace App\Http\Controllers\Auth;

 use Illuminate\Http\Request;
 use App\Http\Controllers\Controller;
 use Socialite;

 class SocialAccountController extends Controller
{
/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest');
}
/**
 * Redirect the user to the SNS authentication page.
 *
 * @return Response
 */
public function redirectToProvider($provider)
{
    if ($provider !== 'facebook') {
        return abort(404);
    }

    return Socialite::with($provider)->redirect();
}

public function handleProviderCallback(\App\Models\User $accountService, $provider)
 {
    try {
        $user = Socialite::with($provider)->user();
        $create['name'] = $user->getName();
        $create['email'] = $user->getEmail();
        $create['facebook_id'] = $user->getId();

        $user = $accountService->addNew($create);

        return view ('user.home')->withDetails($user)->withService($provider);
    } catch(Exception $e){
        return redirect('/login');
    }
}
}

I have found some answers but is not effective on my side. I'm stuck on this for 2 days now. How can I resolve this?

Related Questions