简体   繁体   English

Laravel 8:用数据库表检查用户数据不能正常工作

[英]Laravel 8: Checking user's data with database table does not work properly

I have created a two factor authentication system, and it redirects user to token.blade.php where he must enters the token that is going to be sent to his phone number and also stored at active_codes table.我创建了一个双因素身份验证系统,它将用户重定向到token.blade.php ,他必须在其中输入要发送到他的电话号码并存储在active_codes表中的令牌。

Now I want to check if the user has entered the same token code that was stored at active_codes table which looks like this:现在我想检查用户是否输入了存储在active_codes表中的相同令牌代码,如下所示:

在此处输入图像描述

And then at the Controller, I tried this:然后在 Controller,我尝试了这个:

public function submit(Request $request)
    {
        $data = $request->validate([
            'token' => 'required|min:6'
        ]);

        if($data['token'] === auth()->user()->activeCode()->code){
            dd('same');
        }
    }

But when I enter the same token, I get this error:但是当我输入相同的令牌时,我得到了这个错误:

ErrorException Undefined property: Illuminate\Database\Eloquent\Relations\HasMany::$code ErrorException未定义属性:Illuminate\Database\Eloquent\Relations\HasMany::$code

so my question is how to check if the requested token code of user, is as same as the token code which is stored on the DB?所以我的问题是如何检查用户请求的令牌代码是否与存储在数据库中的令牌代码相同?

Note: The relationship between User and ActiveCode Models is OneToMany.注意: UserActiveCode模型之间的关系是 OneToMany。

User.php : User.php

public function activeCode()
    {
        return $this->hasMany(ActiveCode::class);
    }

ActiveCode.php : ActiveCode.php

public function user()
    {
        return $this->belongsTo(User::class);
    }

Now when you have the relationship between User Model and ActiveCode Model, you can add this to ActiveCode Model:现在,当您拥有用户 Model 和 ActiveCode Model 之间的关系时,您可以将其添加到ActiveCode Model 中:

public function scopeVerifyCode($query, $code, $user)
{
   return !! $user->activeCode()->whereCode($code)->where('expired_at', '>', now())->first();
}

You can readmore about local and global scope here您可以在此处阅读有关本地和全局 scope 的更多信息

But you need some session variables to check if user is not seeing token.blade.php without redirecting from login.blade.php .但是你需要一些 session 变量来检查用户是否没有看到token.blade.php而不从login.blade.php重定向。 And also for sending user id to that route.并且还用于将用户 ID 发送到该路由。

So at Auth\LoginController , add this:所以在Auth\LoginController ,添加这个:

protected function authenticated(Request $request, $user)
{
    if($user->two_factor_type !== 'off'){ // I assume you have this column at users table 
       auth()->logout(); // reject user from loggin in before submitting token 
       
       $request->session()->flash('auth', [
           'user_id' => $user->id,
           'remember' => $request->has('remember')
       ]);
    }
}

And then you can go to your Controller and write this:然后你可以 go 到你的 Controller 并写下这个:

public function submit(Request $request)
{
        $request->validate([
            'token' => 'required|min:6'
        ]);

        if($request->session()->has('auth')){
            return redirect(route('login'));
        }

        $user = User::findOrFail($request->session()->get('auth.user_id'));

        $status = ActiveCode::verifyCode($request->token,$user);

        if(!$status){
            return redirect(route('login'));
        }
        
        if(auth()->loginUsingId($user->id,$request->session()->get('auth.remember'))){
            return redirect('/'); // logs in user to homepage or any other route as you wish
        }
}

Your solution is pretty easy, you are not doing ->first() or ->get() , so you are trying to access a model property on a HasMany class.您的解决方案非常简单,您没有执行->first()->get() ,因此您尝试访问HasMany class 上的 model 属性。

This code should be similar to:此代码应类似于:

auth()->user()->activeCode()->first()->code

But if you have more than 1 code, then you should do something like:但是,如果您有超过 1 个代码,那么您应该执行以下操作:

public function submit(Request $request)
{
    $data = $request->validate([
        'token' => 'required|min:6'
    ]);

    if(auth()->user()->activeCode()->where('code', $data['token'])->exists()){
        dd('same');
    }
}

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

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