Php 通过电子邮件或手机在Laravel 5.5中重置密码
默认情况下,Laravel 5.5的密码重置系统在电子邮件上工作,但我需要添加对手机号码的支持(通过OTP验证并生成令牌并重定向到密码重置页面)。我正在做这部分的工作,我在password_resets表上创建了一个mobile列 但问题是Php 通过电子邮件或手机在Laravel 5.5中重置密码,php,laravel,forgot-password,Php,Laravel,Forgot Password,默认情况下,Laravel 5.5的密码重置系统在电子邮件上工作,但我需要添加对手机号码的支持(通过OTP验证并生成令牌并重定向到密码重置页面)。我正在做这部分的工作,我在password_resets表上创建了一个mobile列 但问题是exist方法上的\illumb\Auth\Passwords\DatabaseTokenRepository和&\illumb\Auth\Passwords\TokenRepositoryInterface上,它似乎不可配置 public function
exist
方法上的\illumb\Auth\Passwords\DatabaseTokenRepository
和&\illumb\Auth\Passwords\TokenRepositoryInterface
上,它似乎不可配置
public function exists(CanResetPasswordContract $user, $token)
{
$record = (array) $this->getTable()->where(
'email', $user->getEmailForPasswordReset()
)->first();
return $record &&
! $this->tokenExpired($record['created_at']) &&
$this->hasher->check($token, $record['token']);
}
我需要重写那个方法。继承的遗产太多了。
我需要扩展哪些类以及如何重写该方法 如果您想覆盖
\illumb\Auth\Passwords\DatabaseTokenRepository
方法的行为,您必须覆盖引用“email”列的每个方法。创建以下文件:
/App/Auth/DatabaseTokenRepository.php
<?php
namespace App\Auth;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Auth\Passwords\DatabaseTokenRepository as DatabaseTokenRepositoryBase;
class DatabaseTokenRepository extends DatabaseTokenRepositoryBase
{
public function create(CanResetPasswordContract $user)
{
$email = $user->getEmailForPasswordReset();
$mobile = $user->getMobileForPasswordReset();
$this->deleteExisting($user);
$token = $this->createNewToken();
$this->getTable()->insert($this->getPayload($email, $mobile, $token));
return $token;
}
protected function deleteExisting(CanResetPasswordContract $user)
{
return $this->getTable()
->where('email', $user->getEmailForPasswordReset())
->orWhere("mobile", $user->getMobileForPasswordReset())
->delete();
}
protected function getPayload($email, $mobile, $token)
{
return ['email' => $email, 'mobile' => $mobile, 'token' => $this->hasher->make($token), 'created_at' => new Carbon];
}
public function exists(CanResetPasswordContract $user, $token)
{
$record = (array) $this->getTable()
->where('email', $user->getEmailForPasswordReset())
->orWhere("mobile", $user->getMobileForPasswordReset())
->first();
return $record &&
! $this->tokenExpired($record['created_at']) &&
$this->hasher->check($token, $record['token']);
}
}
<?php
namespace App\Auth;
use Illuminate\Support\Str;
use Illuminate\Auth\Passwords\PasswordBrokerManager as PasswordBrokerManagerBase;
class PasswordBrokerManager extends PasswordBrokerManagerBase
{
protected function createTokenRepository(array $config)
{
$key = $this->app['config']['app.key'];
if (Str::startsWith($key, 'base64:')) {
$key = base64_decode(substr($key, 7));
}
$connection = $config['connection'] ?? null;
return new DatabaseTokenRepository(
$this->app['db']->connection($connection),
$this->app['hash'],
$config['table'],
$key,
$config['expire']
);
}
}
<?php
namespace App\Providers;
use App\Auth\PasswordBrokerManager;
use Illuminate\Auth\Passwords\PasswordResetServiceProvider as PasswordResetServiceProviderBase;
class PasswordResetServiceProvider extends PasswordResetServiceProviderBase
{
protected function registerPasswordBroker()
{
$this->app->singleton('auth.password', function ($app) {
return new PasswordBrokerManager($app);
});
$this->app->bind('auth.password.broker', function ($app) {
return $app->make('auth.password')->broker();
});
}
}
您的用户没有电子邮件地址吗?或者你是说你只通过密码重置表上的电话号码来引用用户我不知道触摸这些类是否是一种方法。我将引入一个新的防护,用于使用电话号码进行身份验证,并覆盖在用户模型@lagbox上发送通知的方法-是的,我们的用户有电子邮件地址,但是可选的。手机号码是必须的。所以我们在密码重置时添加了mobile列table@nakov,我觉得这个帖子很有帮助-。我正在关注这一点,但我想我错过了一些things@HasanHafizPasha你可以在那里的帖子上发表评论,看看实施这项技术的人是否能给你更多帮助。