0

我正在使用 Laravel 5.1 并尝试使用 LinkedIn 登录到我的应用程序。OAuth 2 登录过程运行良好,我设法users使用正确的详细信息将新用户添加到表中。我也可以设法让这个用户使用 LaravelloginUsingId()函数登录(我可以在它之后看到 \Auth::user()),但它在Authenticate中间件中失败了。

这是 providerExecute我的功能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.');
    }
}

这是Authenticate中间件:

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);
}
}

任何人都知道它发生的原因以及解决方案是什么?多谢!

4

1 回答 1

0

也许我错了,但我在你的中间件类中没有看到变量 $auth,在构造中你得到了 assign $this->auth = $auth. 也许为此,当你尝试 dd($this->auth->user()) 时你得到了 null

于 2016-09-12T11:04:31.780 回答