lundi 12 septembre 2016

Laravel 5.1 loginUsingId() with linkedin account (using socialite) failed in Authenticate middleware

I'm using Laravel 5.1 and try implements login with LinkedIn to my app. The OAuth 2 login process working fine and I manage to add a new user to users table with the correct details. I can manage to log this user in using Laravel loginUsingId() function as well (i can see \Auth::user() after it) but it fails in the Authenticate middleware.

This is the providerExecute function in my AuthController:

 public function providerExecute($provider, Request $request, $token)
{
    if(!is_null($token)){
        session(['invitation_token' => $token]);
    }
    if (!$request->has('code')) {
        return $this->getAuthFirst($provider);
    }
    $user = Socialite::with($provider)->user();
    if ($user) {
        $pid = $user->id;
        $exists = User::where('email', $user->email)->first();
        if ($exists) {
            Auth::loginUsingId((int)$exists->id,true); //when dd() the \Auth::user() here - it's returns the user data
            return redirect('home');
        }
        $new_user = new User();
        if (session('invitation_token')) {
            $invitation = $this->getUserInvitation(session('invitation_token'));
            if(!is_null($invitation->client_id)){
                $client = Client::findOrFail($invitation->client_id);
                if(!$this->canAddUsers($client)){
                    Log::info('Register from invitation! Client ' . $client->name . ' reached max users count, client id: ' . $client->id);
                    return response()->view('errors.auth',
                        ['error' => 'Error! Reached users limit, to add more users please upgrade your account plan']);
                }
            }
            $new_user->client_id = $invitation->client_id;
            $new_user->agency_id = $invitation->agency_id;
            $new_user->auth_level = $invitation->auth_level;
            $invitation->active = 0;
            $invitation->save();
            $redirect_path = "verification/send/";
        }else{
            $new_user->auth_level = config('LM2.ADMIN_AUTH_LEVEL');
            $redirect_path = "welcome/send/";
        }
        $new_user->name = $user->name;
        $new_user->email = $user->email;
        $new_user->provider_id = $user->id;
        $new_user->provider_token = $user->token;
        $new_user->img_src = $user->avatar ? $user->avatar : $this->getDefaultAvatarSrc();
        $new_user->account_verified = 1;
        $new_user->save();
        Auth::loginUsingId($new_user->id);
        return redirect($redirect_path.$new_user->id);
    }else{
        abort('403' , 'Unauthorized.');
    }
}

And this is the Authenticate middleware:

public function __construct(Guard $auth)
{
    $this->auth = $auth;
}


public function handle($request, Closure $next)
{
    //When dd($this->auth->user()) - returns null;
    if (!$this->auth->user()) {
        if ($request->ajax()) {
            return response('Unauthorized.', 401);
        } else {
            return redirect()->guest('login') ;
        }
    }elseif(!$this->auth->user()->active){
        $this->auth->logout();
        if ($request->ajax()) {
            return response('Unauthorized.', 401);
        } else {
            return redirect()->guest('login')->withErrors(['Inactive User']);
        }
    }

    return $next($request);
}

Anyone know the reason it's happans and what can be the solution for it? thank's a lot!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire