Skip to content

Commit a45c3d3

Browse files
Merge pull request #73 from Laragear/feat/attempt-route
[1.x] Adds attempt redirection route.
2 parents 03bf114 + 27f52ec commit a45c3d3

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ You can further customize how to handle the 2FA code authentication procedure wi
234234
|-------------------|-----------------------------------------------------------------------------------|
235235
| guard($guard) | The guard to use for authentication. Defaults to the application default (`web`). |
236236
| view($view) | Return a custom view to handle the 2FA Code retry. |
237+
| redirect($route) | Redirect to a location to handle the 2FA Code retry. |
237238
| message($message) | Return a custom message when the 2FA code fails or is not present. |
238239
| input($input) | Sets the input where the TOTP code is in the request. Defaults to `2fa_code`. |
239240
| sessionKey($key) | The key used to flash the encrypted credentials. Defaults to `_2fa_login`. |

src/Facades/Auth2FA.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* @method static \Laragear\TwoFactor\TwoFactorLoginHelper input(string $input)
1313
* @method static \Laragear\TwoFactor\TwoFactorLoginHelper sessionKey(string $sessionKey)
1414
* @method static \Laragear\TwoFactor\TwoFactorLoginHelper guard(string $guard)
15+
* @method static \Laragear\TwoFactor\TwoFactorLoginHelper redirect(string $route)
1516
*
1617
* @see \Laragear\TwoFactor\TwoFactorLoginHelper
1718
*/

src/TwoFactorLoginHelper.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
use Illuminate\Support\Facades\Crypt;
1111
use InvalidArgumentException;
1212
use Laragear\TwoFactor\Exceptions\InvalidCodeException;
13-
1413
use function array_merge;
14+
use function redirect;
1515
use function response;
1616
use function view;
1717

@@ -50,6 +50,7 @@ public function __construct(
5050
protected string $sessionKey,
5151
protected bool $useFlash,
5252
protected string $input = '2fa_code',
53+
protected string $redirect = '',
5354
) {
5455
//
5556
}
@@ -119,6 +120,19 @@ public function guard(string $guard): static
119120
return $this;
120121
}
121122

123+
/**
124+
* Set the route to redirect the user on failed authentication.
125+
*
126+
* @param string $route
127+
* @return $this
128+
*/
129+
public function redirect(string $route): static
130+
{
131+
$this->redirect = $route;
132+
133+
return $this;
134+
}
135+
122136
/**
123137
* Attempt to authenticate a user using the given credentials.
124138
*
@@ -145,7 +159,7 @@ public function attempt(array $credentials = [], $remember = false): bool
145159
} catch (InvalidCodeException $e) {
146160
$this->flashData($credentials, $remember);
147161

148-
$this->throwConfirmView($this->input, $this->request->has($this->input) ? $e->errors() : []);
162+
$this->throwResponse($this->input, $this->request->has($this->input) ? $e->errors() : []);
149163
}
150164

151165
// @codeCoverageIgnoreStart
@@ -225,9 +239,13 @@ protected function flashData(array $credentials, bool $remember): void
225239
* @param array $errors
226240
* @return void
227241
*/
228-
protected function throwConfirmView(string $input, array $errors): void
242+
protected function throwResponse(string $input, array $errors): void
229243
{
230-
// @phpstan-ignore-next-line
231-
response(view($this->view, ['input' => $input])->withErrors($errors))->throwResponse();
244+
$response = $this->redirect
245+
? redirect($this->redirect)->withInput(['input' => $input])->withErrors($errors)
246+
// @phpstan-ignore-next-line
247+
: response(view($this->view, ['input' => $input])->withErrors($errors));
248+
249+
$response->throwResponse();
232250
}
233251
}

tests/TwoFactorLoginHelperTest.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Mockery;
2121
use Tests\Stubs\UserStub;
2222
use Tests\Stubs\UserTwoFactorStub;
23-
2423
use function app;
2524
use function config;
2625
use function get_class;
@@ -302,4 +301,42 @@ public function test_reflashes_credentials_if_2fa_code_fails(): void
302301

303302
$this->assertGuest();
304303
}
304+
305+
public function test_throws_redirection_on_failure(): void
306+
{
307+
$this->app->make('router')->post('login-with-redirect', function (Request $request) {
308+
try {
309+
return Auth2FA::redirect('foo')->attempt($request->only('email', 'password'))
310+
? 'is authenticated'
311+
: 'is unauthenticated';
312+
} catch (\Throwable $exception) {
313+
if (! $exception instanceof HttpResponseException) {
314+
var_dump([get_class($exception), $exception->getMessage()]);
315+
}
316+
317+
throw $exception;
318+
}
319+
});
320+
321+
$this->post('login-with-redirect', $this->credentials)
322+
->assertRedirect('foo')
323+
->assertSessionHasInput('input', '2fa_code')
324+
->assertSessionHas('_2fa_login.credentials.email', function (string $email): bool {
325+
static::assertSame($this->user->email, Crypt::decryptString($email));
326+
327+
return true;
328+
})
329+
->assertSessionHas('_2fa_login.credentials.password', static function (string $password): bool {
330+
static::assertSame('secret', Crypt::decryptString($password));
331+
332+
return true;
333+
})
334+
->assertSessionHas('_2fa_login.remember', static function ($remember) {
335+
static::assertFalse($remember);
336+
337+
return true;
338+
});
339+
340+
$this->assertGuest();
341+
}
305342
}

0 commit comments

Comments
 (0)