是否可以确定 Laravel Fortify 中是否已经使用了恢复代码?
问问题
90 次
1 回答
0
我知道没有开箱即用的东西,但是,你可以自己实现一些东西。
一个基本的工作流程可能如下所示:
try {
// grab the codes for the currently authenticated user
$codes = json_decode(decrypt(auth()->user()->two_factor_recovery_codes), true);
// if the provided code exists in the array of codes stored in the DB for the authenticated user
if (in_array($code, $codes)) {
// remove the associated code and reindex the array
$codes = array_values(array_diff($codes, [$code]));
// optionally add a new recovery code
array_push($codes, Laravel\Fortify\RecoveryCode::generate());
// save the recovery codes back to the database
auth()->user()->two_factor_recovery_codes =
encrypt(json_encode(array_values($codes)));
auth()->user()->save();
}
} catch (DecryptException $e) {
echo $e->getMessage();
} catch (EncryptException $e) {
echo $e->getMessage();
}
于 2021-05-09T16:41:25.383 回答