简体   繁体   English

在Laravel 5.3中创建自定义重定向逻辑; 无法登录用户

[英]Creating custom redirect logic in Laravel 5.3; can't log users in

I wanted to do custom redirect logic upon logging in to my site. 我想在登录到我的站点时执行自定义重定向逻辑。 So I wrote this code 所以我写了这段代码

public function login()
{
    $user = //what do I put here?
    $this->guard()->login($user);
    $id = Auth::id();
    //dd($id);
    $rfid = DB::table('users')->where('id', $id)->value('reference_id');
    if ($rfid == null){
        dd($id);
        return redirect()->action('RedirectController@client');
    }
    else {
        //dd($rfid);
        return redirect()->action('RedirectController@employee');
    }
}

However my problem is that all it does is redirect. 但是我的问题是它所做的就是重定向。 Which makes sense. 有道理。 The code I have there only manages the redirect, there is nothing there to actually log anyone in. So I did some researching and found 我那里的代码只管理重定向,没有任何东西可以实际登录任何人。所以我做了一些研究,发现

 $this->guard()->login($user);

As a way to log the user in. However I don't know what to define the variable $user as. 作为登录用户的一种方式。但是,我不知道将变量$ user定义为什么。 I know this should work because I use it in a different place on my site, but it the $user variable that I use there wouldn't work here. 我知道这应该工作,因为我在网站的其他位置使用了它,但是在这里使用的$ user变量在这里不起作用。 So under the scenario of simply login in the user, what do put there in order to authorize the user as in our database, and then log them in? 因此,在仅登录用户的情况下,为了授权用户在我们的数据库中然后登录他们,该放置什么?

If you look at Laravel 5.3 AuthenticatesUsers traits , you will understand how login works behind the scene. 如果您查看Laravel 5.3 AuthenticatesUsers traits,您将了解登录如何在后台工作。

you need to change your login method to use attempt instead of login as follows to work it accordingly. 您需要按照以下方式更改login方法以使用attempt而不是login ,以进行相应的操作。

public function login(Request $request)
{
 $user = //what do I put here?
 $this->guard()->attempt($this->credentials($request), $request->has('remember'));
 $id = Auth::id();
 //dd($id);
 $rfid = DB::table('users')->where('id', $id)->value('reference_id');
 if ($rfid == null){
     dd($id);
     return redirect()->action('RedirectController@client');
 }
 else {
     //dd($rfid);
     return redirect()->action('RedirectController@employee');
 }}

I recommend you to use it as I mentioned. 我建议您使用我提到的方法。 If you still want to get $user value you can try out 如果您仍然想获得$user价值,可以尝试一下

$user = $this->guard()->user();

Though I haven't used it like that. 尽管我还没有那样使用它。

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

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