我正在编写一个测试,以确保我的应用程序的密码重置功能有效。密码重置系统是使用 php artisan make:auth
命令创建的。为了使测试通过,我需要自动向 /password/reset/{$token}
发出 GET 请求,其中 $token
是存储在 中的值password_resets
表。 Laravel 像这样存储 token :
$2y$10$9grKb3c6.Toiv0kjUWbCUeT8Q8D.Fg2gZ/xDLGQUAkmdyHigmRkNW
但是当 Laravel 向用户发送密码重置邮件时,邮件中的重置 token 如下所示:
382aa64567ecd05a774c2e4ebb199d3340a1424300707053354c749c10487594
。
我对 /password/reset/$2y$10$9grKb3c6.Toiv0kjUWbCUeT8Q8D.Fg2gZ/xDLGQUAkmdyHigmRkNW
的 GET 请求因重置 token 中的正斜杠而失败。 (紧跟在“g2gZ”之后)
我尝试使用辅助函数 decrypt()
但没有成功。
我如何转换从 password_resets
表中提取的密码重置 token 以匹配 Laravel 发送给用户的内容?
不确定这是否相关,但我确实将我的应用程序从 5.3 升级到了 5.4。
最佳答案
您可以从用于传递给 Notification 的 assertSentTo 方法的额外检查的闭包中获取 token ,因为 $token
是标准 ResetPassword
通知的公共(public)属性。
在你的测试中:
Notification::fake();
$this->postJson('api/user/reset', ['email' => $user->email])
->assertStatus(200);
$token = '';
Notification::assertSentTo(
$this->user,
\Illuminate\Auth\Notifications\ResetPassword::class,
function ($notification, $channels) use (&$token) {
$token = $notification->token;
return true;
});
$this->postJson('api/user/resetting', [
'email' => $user->email,
'token' => $token,
'password' => '87538753',
'password_confirmation' => '87538753'
])
->assertStatus(200);
关于php - 解密 Laravel 密码重置 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41862370/