简体   繁体   English

Laravel loginUsingId似乎不起作用

[英]Laravel loginUsingId doesn't seem to work

I need to manually login a user in Laravel 5.7 via Auth. 我需要通过Auth手动登录Laravel 5.7中的用户。 Once I run Auth::loginUsingId($userId, true) I then relocate the user to his Account page. 一旦我运行Auth :: loginUsingId($ userId,true),我就会将用户重新定位到他的帐户页面。

The point of this is for a user coming through a token can be logged in into the website, without adding his credentials again. 这一点是通过令牌的用户可以登录到网站,而无需再次添加他的凭据。

I've tried anything I could find online, including moving the Session from MiddlewareGroup to Middleware, checking the Cookie name and some other things that didn't work. 我尝试过任何我能在网上找到的东西,包括将Session从MiddlewareGroup转移到Middleware,检查Cookie名称以及其他一些不起作用的东西。

My Controller looks something like this: 我的控制器看起来像这样:

public function loginExternal(Request $request) {
  $userId = $request->uid;

  Auth::loginUsingId($userId, true);

  redirect()->to('/account')->send();
}

and the route for it is pretty simple: 它的路线非常简单:

Route::get('/oneclick/{token}', 'Auth\AccountController@loginExternal')->middleware('signed')->name('oneclick');

I would expect the user to be logged in and taken to his account automatically. 我希望用户能够自动登录并进入他的帐户。 Now it just sends me to the login page. 现在它只是将我发送到登录页面。

What I noticed is that the loginUsingId() method generates a new session id only in this controller, but in other pages of the website, the website is using a different session, the same one (which should happen). 我注意到loginUsingId()方法仅在此控制器中生成新的会话ID,但在网站的其他页面中,网站使用的是相同的会话(应该发生)。

I need to mention that the user does get loggedin in the LoginExternal method. 我需要提一下,用户确实登录了LoginExternal方法。 It just doesn't persist to the account page. 它只是不会持久到帐户页面。

Any ideas? 有任何想法吗?

In controller: 在控制器中:

public function loginExternal($id) {

  $user = User::find($id);

  if($user){
     \Auth::loginUsingId($id, true);

     return redirect('/account');
  } else {
     return redirect('/')->with('error_message', 'No user found!');
  }

}

In route file (web.php) 在路由文件(web.php)中

Route::get('/oneclick/{id}', 'Auth\AccountController@loginExternal')->name('oneclick');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM