0

私はLaravel 5.1を使用しており、私のアプリケーションにLinkedInでログインしています。 OAuth 2のログイン処理が正常に動作しています。新しいユーザーをusersテーブルに追加して、正しい詳細を付けます。私はこのユーザーをLaravel loginUsingId()関数でログに記録することもできます(後に\ Auth :: user()が表示されます)が、Authenticateミドルウェアで失敗します。Laravel 5.1のloginUsingId()はsocialiteを使用しています(socialiteを使用)は、ミドルウェアの認証に失敗しました

これは私のAuthControllerproviderExecute機能です:

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

誰もが、それはhappansだと何が解決策になることができた理由を知っていますそれのための?どうもありがとう!

答えて

0

私は間違っているかもしれませんが、ミドルウェアクラスで変数$ authが表示されません。$ this-> auth = $ authを割り当てられています。多分、あなたはdd($ this-> auth-> user())を試してみるとヌルになった

+0

私は関数だけを貼り付けます。申し訳ありません、今質問を編集します。 – benjah

関連する問題