douxing7101 2016-09-12 10:51
浏览 126

使用linkedin帐户的Laravel 5.1 loginUsingId()(使用socialite)在Authenticate中间件中失败

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:

class Authenticate
{

protected $auth;

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

public function handle($request, Closure $next)
{
    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!

  • 写回答

1条回答 默认 最新

  • drwurqczo39355510 2016-09-12 11:04
    关注

    Maybe im wrong, but i dont see variable $auth in your middleware class, in construct you got assign $this->auth = $auth. Maybe for that, u got null when you try dd($this->auth->user())

    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线