Laravel Fortify 2FA恢复码登录不起作用

huangapple go评论68阅读模式
英文:

Laravel Fortify 2FA Recovery Code Login Not Working

问题

我在我的应用程序中使用二次验证(2FA)的QR码功能,但恢复码路由出了一些问题。我总是收到以下错误消息:

提供的二次验证代码无效。

我不知道是我的表单使用的路由有问题,还是其他什么原因。这是我的表单:

<div class="col-md-12 ps-md-0" x-data="{ showRecovery: false, showCode: true }" x-cloak>
	<div class="auth-form-wrapper px-4 py-5">
		<form class="reset-password-form" method="POST" action="{{ route('two-factor.login') }}">
			@csrf
			<div x-show="showCode">
				<h5 class="text-muted fw-normal mb-4 mt-4">输入验证代码</h5>
				<div class="mb-3">
					<input type="password" class="form-control" id="password" placeholder="代码" name="code">
					@error('code')
						<span class="invalid-feedback" style="display: block;" role="alert"><strong>{{ $message }}</strong></span>
					@enderror
				</div>
				<div class="d-flex justify-content-start">
					<button class="btn btn-primary me-2 mb-2 mb-md-0" type="submit">提交</button>
				</div>
				<a role="button" x-on:click="showRecovery = true, showCode = false" class="d-block mt-3 text-muted">使用恢复码</a>
			</div>

			<div x-show="showRecovery">
				@csrf
				<h5 class="text-muted fw-normal mb-4 mt-4">输入恢复码</h5>
				<div class="mb-3">
					<input type="text" class="form-control" id="recovery_code" placeholder="恢复码" name="recovery_code">
					@error('recovery-code')
						<span class="invalid-feedback" style="display: block;" role="alert"><strong>{{ $message }}</strong></span>
					@enderror
				</div>
				<div class="d-flex justify-content-start">
					<button class="btn btn-primary me-2 mb-2 mb-md-0" type="submit">提交</button>
				</div>
				<a role="button" x-on:click="showRecovery = false, showCode = true" class="d-block mt-3 text-muted">使用验证代码</a>
			</div>
		</form>
	</div>
</div>

当我运行 php artisan route:list 时,我看不到名为 two-factor.login 的路由,但使用验证代码时它能够正常工作。我假设使用恢复码时应该使用相同的路由。

我还尝试过将表单操作设置为 /two-factor-challenge,但结果相同。

英文:

I've got 2FA working in my app with the qr code, but the recovery code route isn't working for some reason. I always get the error:

The provided two factor authentication code was invalid.

I don't know if it's the route I'm using in my form, or what. Here's the form:

&lt;div class=&quot;col-md-12 ps-md-0&quot; x-data=&quot;{ showRecovery: false, showCode: true }&quot; x-cloak&gt;
&lt;div class=&quot;auth-form-wrapper px-4 py-5&quot;&gt;
&lt;form class=&quot;reset-password-form&quot; method=&quot;POST&quot; action=&quot;{{ route(&#39;two-factor.login&#39;) }}&quot;&gt;
@csrf
&lt;div x-show=&quot;showCode&quot;&gt;
&lt;h5 class=&quot;text-muted fw-normal mb-4 mt-4&quot;&gt;Enter Authentication Code&lt;/h5&gt;
&lt;div class=&quot;mb-3&quot;&gt;
&lt;input type=&quot;password&quot; class=&quot;form-control&quot; id=&quot;password&quot; placeholder=&quot;Code&quot; name=&quot;code&quot;&gt;
@error(&#39;code&#39;)
&lt;span class=&quot;invalid-feedback&quot; style=&quot;display: block;&quot; role=&quot;alert&quot;&gt;&lt;strong&gt;{{ $message }} &lt;/strong&gt;&lt;/span&gt;
@enderror
&lt;/div&gt;
&lt;div class=&quot;d-flex justify-content-start&quot;&gt;
&lt;button class=&quot;btn btn-primary me-2 mb-2 mb-md-0&quot; type=&quot;submit&quot;&gt;Submit&lt;/button&gt;
&lt;/div&gt;
&lt;a role=&quot;button&quot; x-on:click=&quot;showRecovery = true, showCode = false&quot; class=&quot;d-block mt-3 text-muted&quot;&gt;Use Recovery Code Instead&lt;/a&gt;
&lt;/div&gt;
&lt;div x-show=&quot;showRecovery&quot;&gt;
@csrf
&lt;h5 class=&quot;text-muted fw-normal mb-4 mt-4&quot;&gt;Enter Recovery Code&lt;/h5&gt;
&lt;div class=&quot;mb-3&quot;&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;recovery_code &quot; placeholder=&quot;Recovery Code&quot; name=&quot;recovery_code &quot;&gt;
@error(&#39;recovery-code&#39;)
&lt;span class=&quot;invalid-feedback&quot; style=&quot;display: block;&quot; role=&quot;alert&quot;&gt;&lt;strong&gt;{{ $message }} &lt;/strong&gt;&lt;/span&gt;
@enderror
&lt;/div&gt;
&lt;div class=&quot;d-flex justify-content-start&quot;&gt;
&lt;button class=&quot;btn btn-primary me-2 mb-2 mb-md-0&quot; type=&quot;submit&quot;&gt;Submit&lt;/button&gt;
&lt;/div&gt;
&lt;a role=&quot;button&quot; x-on:click=&quot;showRecovery = false, showCode = true&quot; class=&quot;d-block mt-3 text-muted&quot;&gt;Use Authentication Code Instead&lt;/a&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;

When I do php artisan route:list I don't see a route for two-factor.login, but it works with the authentication code. I'm assuming it should be the same route for using the recovery code as well.

I've also tried using /two-factor-challenge as the form action but I get the same result.

答案1

得分: 0

在恢复代码输入字段中,name属性中多了一个空格,可能会导致问题。请将此行更改为:

&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;recovery_code&quot; placeholder=&quot;Recovery Code&quot; name=&quot;recovery_code&quot;&gt;
英文:

@hyphen

In the recovery code input field, there's an extra space in the name attribute, which might cause issues. Change this line:

&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;recovery_code &quot; placeholder=&quot;Recovery Code&quot; name=&quot;recovery_code &quot;&gt;

TO

&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;recovery_code&quot; placeholder=&quot;Recovery Code&quot; name=&quot;recovery_code&quot;&gt;

huangapple
  • 本文由 发表于 2023年7月27日 20:27:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定