【问题标题】:Authentificate user via stored procedure in Laravel通过 Laravel 中的存储过程对用户进行身份验证
【发布时间】:2019-12-25 20:44:35
【问题描述】:

我有一个接受登录名和密码,并给出状态或用户 ID 的存储过程。我扩展了 LoginController 来处理它。 这一直有效,直到我输入正确的数据。 执行过程后,我尝试通过 id 授权用户,但这是错误的(并且用户模型不存在)。

public function login(Request $request)
{
    $this->validateLogin($request);

    if (method_exists($this, 'hasTooManyLoginAttempts') &&
        $this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);

        return $this->sendLockoutResponse($request);
    }

    $result = DB::select(DB::raw("SET NOCOUNT ON {CALL users.Login (@Login =:username, @Password =:password)}"),[
        ':username' => $request->login,
        ':password' => $request->password,
    ]);

    switch($result[0]->status){
        case '-3':
            $message = "Login is banned";
            break;
        case '-2':
            $message = 'Wrong password';
            break;
        case '-1':
            $message = 'You are haven\'t rights';
            break;
    }

    if ($result[0]->status > 0) {
        //Auth::loginUsingId($result[0]->status);
        return $this->sendLoginResponse($request);
    }

    $this->incrementLoginAttempts($request);

    return $this->sendFailedLoginResponse($request, $message);
}

我接受这个错误:

Symfony\Component\Debug\Exception\FatalThrowableError 类

'\App\Models\User' 未找到

如何将用户数据写入会话并通过 Auth 门面使用?


P.S 我现在存在 this question 但这里没有答案...

【问题讨论】:

  • '\App\Models\User' not found 来自哪里?
  • @MuhammadDyasYaskur 来自Auth::loginUsingId($result[0]->status); (?)
  • 确保您的用户模型存在

标签: sql laravel stored-procedures


【解决方案1】:

Class '\App\Models\User' not found 表示用户模型不存在。如果您认为该模型存在于 App/Models 中,请确保命名空间(顶部的代码)为 namespace App\Models;

第二种情况是您可能会尝试使用状态登录?您应该使用Auth::loginUsingId($result[0]->id) 而不是状态。

我建议你使用Auth::attempt(),比如Auth::attempt(['login' => request('login'), 'password' => request('password')])

【讨论】:

  • 我修复了这个(我忘了重命名命名空间)。但是数据库中没有用户表,我无法从该表中获取任何信息。我只能通过程序检查请求并通过用户 ID 从另一个程序获取信息。
  • 对不起,我不明白,所以这个问题解决了吗?
猜你喜欢
  • 1970-01-01
  • 2015-09-09
  • 2017-07-03
  • 2019-01-06
  • 2016-04-15
  • 2020-07-19
  • 1970-01-01
  • 2019-09-18
  • 2021-07-18
相关资源
最近更新 更多