0

저는 Laravel 5.1을 사용하고 있으며 내 앱에 LinkedIn을 사용하여 로그인을 구현하려고합니다. OAuth 2 로그인 프로세스가 정상적으로 작동하며 정확한 세부 정보가 포함 된 users 테이블에 새 사용자를 추가 할 수 있습니다. 나는이 사용자를 Laravel loginUsingId() 기능을 사용하여 기록 할 수있다. (그 후에 \ Auth :: user()를 볼 수는 있지만) Authenticate 미들웨어에서는 실패한다. Laravel 5.1 linkedin 계정 (socialite 사용)이 로그인 미들웨어에서 성공하지 못했습니다.

AuthController에서 providerExecute 기능입니다 :

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())를 시도 할 때 null이 생겼을 것입니다.

+0

나는 기능 만 붙여 넣습니다. 죄송합니다. 지금 제 질문을 수정하겠습니다. – benjah