0

I need to bind a Vkontakte account to a user who is already registered and authorized. However, I can't retrieve the currently authenticated user in the handler.

Controller

class VkAuthController extends Controller
{
    public function index(Request $request)
    {
        if($request->token) {
            $token = PersonalAccessToken::findToken($request->token);

            session(['currentUser' => $token->tokenable]);
        }

        return Socialite::driver('vkontakte')->stateless()->redirect();
    }

    public function callback()
    {
        if(session('currentUser')) {
            $user = VkBindAction::execute(session('currentUser'));

            return redirect(env('APP_URL') . '/settings?data=' . json_encode((new UserViewModel($user))->toArray()));
        }

        $user = VkAuthAction::execute();
        $token = CreateTokenByUserAction::generateToken($user);

        return redirect(env('APP_URL') . '?data=' . json_encode((new LoginUserViewModel(['user' => $user, 'tokenAccess' => $token]))->toArray()));

    }
}

But with going on Vkontakte auth, Laravel session is dropping, and in session('currentUser'), I get null in the callback method. Use

Socialite::driver('vkontakte')->stateless()->with(['currentUser' => $token->tokenable])->redirect();

The changes also have no effect; I only receive null in the callback. Could anyone possibly suggest a solution to this?

0

Browse other questions tagged or ask your own question.