我正在做一个银行应用程序的学校项目。用户可以转移资金,但是在他能够转移之前,他必须通过输入发送到他的电子邮件的令牌来验证转移。到目前为止,我已经能够生成令牌并发送到用户的电子邮件。但是,当用户输入令牌时,他将被重定向回到转移表单视图,在该视图中,他需要重新开始填写表单,然后单击“开始转移”即可将其发送出去。
问题:如何在他输入正确的令牌后立即进行检查。我不希望用户再次填写表单。这是我的令牌控制器
public function verifyTwoFactor(Request $request)
{
$request->validate([
'2fa' => 'required',
]);
if ($request->input('2fa') == Auth::user()->token_2fa) {
$user = Auth::user();
// $user->token_2fa_expiry = \Carbon\Carbon::now()->addMinutes(config('session.lifetime'));
$user->token_2fa_expiry = \Carbon\Carbon::now()->addMinutes(1);
$user->save();
return redirect('/ibank/dashboard/interbank-transfer');
} else {
return redirect('/2fa')->with('message', 'Incorrect code.');
}
}
public function verifyTwoFactor2(Request $request)
{
$request->validate([
'2fa' => 'required',
]);
if ($request->input('2fa') == Auth::user()->token_2fa) {
$user = Auth::user();
// $user->token_2fa_expiry = \Carbon\Carbon::now()->addMinutes(config('session.lifetime'));
$user->token_2fa_expiry = \Carbon\Carbon::now()->addMinutes(1);
$user->save();
return redirect('/ibank/dashboard/localbank-transfer');
} else {
return redirect('/2fa')->with('message', 'Incorrect code.');
}
}
public function verifyTwoFactor3(Request $request)
{
$request->validate([
'2fa' => 'required',
]);
if ($request->input('2fa') == Auth::user()->token_2fa) {
$user = Auth::user();
// $user->token_2fa_expiry = \Carbon\Carbon::now()->addMinutes(config('session.lifetime'));
$user->token_2fa_expiry = \Carbon\Carbon::now()->addMinutes(1);
$user->save();
return redirect('/ibank/dashboard/transfer');
} else {
return redirect('/2fa')->with('message', 'Incorrect code.');
}
}
public function showTwoFactorForm()
{
return view('auth.verify2fa');
}
我的中间件看起来像这样
$user = Auth::user();
if ($user->token_2fa_expiry > \Carbon\Carbon::now()) {
return $next($request);
}
$user->token_2fa = mt_rand(10000, 99999);
$user->save();
Mail::to($user->email)->send(new Verify2fa($user));
return redirect('/2fa');
}