diff --git a/README.md b/README.md index e78cd12..4c8c17e 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ public function destroy(Request $request, DisableTwoFactorAuthentication $disabl } ``` -Once the user has confirmed enabling two-factor authentication, each time they log in, they will be redirected to a page where they will be asked to enter a one-time password generated by their authenticator app (or sent to them via SMS/email). +Once the user has confirmed enabling two-factor authentication, each time they log in, they will be redirected to a page where they will be asked to enter a one-time password generated by their authenticator app. ```php use Emargareten\TwoFactor\Actions\TwoFactorRedirector; @@ -165,6 +165,18 @@ public function login(Request $request, TwoFactorRedirector $redirector): Respon } ``` +or if the user should retrieve the OTP via other methods: + +```php + $redirect = $redirector->redirect($request); + + if ($redirector->isRedirectingToChallenge()) { + $request->user()->notify(new SendOTP); + } + + return $redirect; +``` + This will redirect the user to the `two-factor-challenge.create` route. You will need to provide a view for this route. This view should contain a form where the user can enter the one-time password, you should bind the view to the `TwoFactorChallengeViewResponse::class` in the `register` method of your `AppServiceProvider` by calling the `TwoFactor::challengeView()` method: diff --git a/src/Actions/TwoFactorRedirector.php b/src/Actions/TwoFactorRedirector.php index 3001e3b..10deeb0 100644 --- a/src/Actions/TwoFactorRedirector.php +++ b/src/Actions/TwoFactorRedirector.php @@ -9,6 +9,8 @@ class TwoFactorRedirector { + protected bool $isRedirectingToChallenge = false; + /** * Redirect to two-factor view. */ @@ -20,6 +22,8 @@ public function redirect(Request $request): ?Response return redirect()->intended(config('two-factor.home')); } + $this->isRedirectingToChallenge = true; + return $this->twoFactorChallengeResponse($request, $user); } @@ -43,4 +47,12 @@ protected function twoFactorChallengeResponse(Request $request, $user): Response ? response()->json(['two_factor' => true]) : redirect()->route('two-factor-challenge.create'); } + + /** + * Check whether redirect is redirecting to challenge + */ + public function isRedirectingToChallenge(): bool + { + return $this->isRedirectingToChallenge; + } }