【问题标题】:User Authentication with Mobile Number and OTP in Laravel在 Laravel 中使用手机号码和 OTP 进行用户身份验证
【发布时间】:2018-09-22 19:40:16
【问题描述】:

如何允许用户使用手机号和一次性密码登录我们的 Laravel 应用程序

【问题讨论】:

标签: laravel laravel-5 laravel-5.5


【解决方案1】:

我通过以下方法实现了它, 在 Laravel 5.7 和 Passport 上:

文件路径: /app/Http/Controllers/AuthController.php

编辑:

$credentials = request(['mobile_no', 'password', 'email', 'otp']);

文件路径: /vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php

编辑:

public function validateCredentials(UserContract $user, array $credentials)
{
    if (isset($credentials['password'])) {
      $plain = $credentials['password'];
      return $this->hasher->check($plain, $user->getAuthPassword());
    }else{
      $otp = $credentials['otp'];
      if ($otp==$user->getAuthOtp()) {
        return true;
      }
    }
}

文件路径: /vendor/laravel/framework/src/Illuminate/Auth/Authenticatable.php

添加:

public function getAuthOtp()
    {
        return $this->otp;
    }

注意:使用此用户也可以使用 OTP 和密码登录,当我找到合适的解决方案时,我会更新我的答案。

【讨论】:

  • 非常感谢...这个解决方案也适用于 laravel 6。如果有人想使用 bcrypt,只需将 Str::contains($key, 'your_key') 添加到 retrieveByCredentials 方法的 if 条件中。现在您可以访问 your_key在validateCredentials
【解决方案2】:

不要使用任何软件包。我想你会因为使用它而感到困惑。请参阅此 (https://laravelcode.com/post/laravel-55-login-with-only-mobile-number-using-laravel-custom-auth) 教程并使用移动设备进行基于自定义的登录并验证 OTP。

【讨论】:

  • otp的概念在哪里?
猜你喜欢
  • 1970-01-01
  • 2018-06-07
  • 1970-01-01
  • 1970-01-01
  • 2021-02-23
  • 2021-11-07
  • 1970-01-01
  • 2021-10-28
  • 2018-06-20
相关资源
最近更新 更多