laravelinertiajstwo-factor-authenticationlaravel-fortify

route problem when confirming two factor with laravel fortify and inertiajs


I have a TwoFactorConfirm page, where an OTP code input is expected after the 2fa device is set, and a POST request is made to route('two-factor.confirm') provided by Fortify like this:

const { data, setData, post, processing, errors, reset } = useForm({
    code: '',
});

const submit = (e) => {
    router.visit(route('two-factor.confirm'), {
        method: 'post',
        data: data,
        onSuccess: (page) => {
            router.visit(route('somewhere'))
        }
    });
}

After the confirmation is done I want to redirect the user to another page, but here is a problem: the router.visit(route('somewhere')) never get to run, because after the request is processed (db changed so it's ok to this step), I got a GET request to /thispage?code=xxxxxx instead of route('somewhere'). This part I don't really understands.


Solution

  • The Inertia router follows the redirect returned from the route two-factor.confirm

    You can modify these redirects, explained in the docs here: https://laravel.com/docs/10.x/fortify#customizing-authentication-redirects

    $this->app->instance(TwoFactorConfirmedResponse::class, new class implements Responsable {
        public function toResponse($request)
        {
            return redirect('/somewhere');
        }
    });