2015-08-17 46 views
0

我正在使用Laravel社交网站启用社交登录。用户从社交网站返回后,我将以/dashboard/id等形式重定向到仪表板页面。

现在,当我直接关闭此仪表板页面,并在浏览器中重新打开登录页面(url: auth/login)。它开始重定向到默认/home。但有线的是,它没有通过默认的getLogin()方法,所以我在AuthController中设置的$redirectPath根本不起作用。

在sociallite中,我使用Auth::login()方法来验证用户。

Laravel版本是5.1。

任何人都可以解释这背后的逻辑是什么?为什么不调用getLogin方法?

社会名流回调:


public function handleProviderCallback() 
    { 
     $user = Socialite::with('facebook')->user(); 
     $authuser = $this->findOrCreateUser($user); 
     Auth::login($authuser, true); 
     //todo: here it should direct to lobby. 
     return redirect()->intended('/lobby/'.$authuser->id); 
    } 

AuthController:


class AuthController extends Controller 
{ 
    /* 
    |-------------------------------------------------------------------------- 
    | Registration & auth Controller 
    |-------------------------------------------------------------------------- 
    | 
    | This controller handles the registration of new users, as well as the 
    | authentication of existing users. By default, this controller uses 
    | a simple trait to add these behaviors. Why don't you explore it? 
    | 
    */ 

    use AuthenticatesAndRegistersUsers, ThrottlesLogins; 

    protected $redirectTo = '/'; 
    protected $redirectPath = '/lobby'; 

    /** 
    * Create a new authentication controller instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     $this->middleware('guest', ['except' => 'getLogout']); 
    } 

    /** 
    * Get a validator for an incoming registration request. 
    * 
    * @param array $data 
    * @return \Illuminate\Contracts\Validation\Validator 
    */ 
    protected function validator(array $data) 
    { 
     return Validator::make($data, [ 
      'name' => 'required|max:255', 
      'email' => 'required|email|max:255|unique:users', 
      'password' => 'required|confirmed|min:6', 
     ]); 
    } 

    /** 
    * Create a new user instance after a valid registration. 
    * 
    * @param array $data 
    * @return User 
    */ 
    protected function create(array $data) 
    { 
     return User::create([ 
      'name' => $data['name'], 
      'email' => $data['email'], 
      'password' => bcrypt($data['password']), 
     ]); 
    } 
} 

回答

2

你需要在你的路由定义 “家” 这样的

Route::get('/', [ 
    'uses' => '[email protected]', 
    'as' => 'home' //As home 
]); 

并且当您将该路线设置为“主页”时,您需要修改RedirectIfAuthenticated.php中的中间件

public function handle($request, Closure $next) 
    { 
     if ($this->auth->check()) { 
      return redirect()->route('home'); //Redirect to 'home' previously defined in your routes 
     } 

     return $next($request); 
    }