1010use Illuminate \Support \Facades \Crypt ;
1111use InvalidArgumentException ;
1212use Laragear \TwoFactor \Exceptions \InvalidCodeException ;
13-
1413use function array_merge ;
14+ use function redirect ;
1515use function response ;
1616use 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}
0 commit comments