Skip to content

Commit

Permalink
add isRedirectingToChallenge method
Browse files Browse the repository at this point in the history
  • Loading branch information
emargareten committed Mar 1, 2023
1 parent 8dd39ee commit 9fc05fd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions src/Actions/TwoFactorRedirector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

class TwoFactorRedirector
{
protected bool $isRedirectingToChallenge = false;

/**
* Redirect to two-factor view.
*/
Expand All @@ -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);
}

Expand All @@ -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;
}
}

0 comments on commit 9fc05fd

Please sign in to comment.