【问题标题】:Laravel authentication model changeLaravel 身份验证模型更改
【发布时间】:2016-09-14 04:25:40
【问题描述】:

我有两个用于管理员和客户端的表,每种类型的用户有两种不同的登录方法。 我已经对管理员使用了尝试()方法。我正在尝试为客户端创建 JWTAuthentication。目前,当我尝试验证客户端登录时,laravel 在管理表中查询,而我希望它在客户端表中查询。我试图将配置设置为专门研究客户端模型。但它仍在调查管理员。

当客户端尝试登录时,我如何告诉 laravel 避免查看管理表?

    if($dbPass==$password)
    {
        //find secret api key and send it to curl
        //$this->callTeamworkApi($query->secretApiKey);
        //set session
    //JWT authenticate
     //$credentials = ["email"=>$email,"password"=>$password];
        //$credentials = $request->all('email','password');
        $credentials =[];
        $credentials['email'] = $email;
        $credentials['password'] = $request->password;

        try{
             \Config::set('auth.model', 'Client');
             \Config::set( 'auth.table' , 'clients' );
             if( ! $token = JWTAuth::attempt($credentials))
              {
                return response()->json([
                    'response' => 'Some error with user credentials' 
                ]);
              }else{
                // $request->session()->put('secretApiKey', $query->secretApiKey);
                // $request->session()->put('userEmail', $sessionEmail);
                // $request->session()->put('userId', $sessionId);
                // $request->session()->put('userName', $sessionName);
                // $request->session()->put('timeCreated', $timeCreated);
                //find user details and put them inside session array
                $msg = "Login_checked";
                return response()->json(["token"=>compact('token'), "msg"=> $msg]);
            }

        }catch(JWTExceptions $err){
            return response()->json(['response'=>$err]);
        }

    }else{
        $msg = "Password_wrong";
    }

【问题讨论】:

    标签: php laravel jwt


    【解决方案1】:

    所有身份验证驱动程序都有一个用户提供程序。这定义了如何从您的数据库或此应用程序用于持久保存用户数据的其他存储机制中实际检索用户。

    如果您有多个用户表或模型,您应该配置多个 代表每个模型/表格的来源。

    这应该在config/auth.php 文件中完成。

    'providers' => [
    
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],
       'clients' => [
            'driver' => 'eloquent',
            'model' => App\Client::class,
        ],
    
    ],
    

    这适用于 Laravel 5,我不确定版本 4 及以下版本。

    【讨论】:

      猜你喜欢
      • 2019-08-23
      • 1970-01-01
      • 2015-06-08
      • 2016-04-06
      • 1970-01-01
      • 2018-01-19
      • 2018-09-15
      • 1970-01-01
      • 2017-11-23
      相关资源
      最近更新 更多