Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Add FailedTwoFactorLoginResponse contract #106

Merged
merged 1 commit into from
Oct 2, 2020
Merged

[1.x] Add FailedTwoFactorLoginResponse contract #106

merged 1 commit into from
Oct 2, 2020

Conversation

faustbrian
Copy link
Contributor

No description provided.

@driesvints driesvints changed the title add FailedTwoFactorLoginResponse contract [1.x] Add FailedTwoFactorLoginResponse contract Oct 2, 2020
@driesvints
Copy link
Member

Can you give some explanation on why this is needed?

@faustbrian
Copy link
Contributor Author

faustbrian commented Oct 2, 2020

Several applications I am working on have differing rules on how two-factor authentication failures are handled which diverge from the default of Fortify, which is to send the user back to the login, while I would prefer to show the user an error message.

<?php

namespace ARKEcosystem\Fortify\Responses;

use Illuminate\Contracts\Support\Responsable;
use Illuminate\Validation\ValidationException;

class FailedTwoFactorLoginResponse implements Responsable
{
    /**
     * Create an HTTP response that represents the object.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function toResponse($request)
    {
        $message = __('The provided two factor authentication code was invalid.');

        if ($request->wantsJson()) {
            throw ValidationException::withMessages([
                'code' => [$message],
            ]);
        }

        $request->session()->put([
            'login.id' => $request->session()->get('login.idFailure'),
        ]);

        return back()->withErrors(['code' => $message]);
    }
}

This would be an example of an alternative implementation to get rid of the redirect to the login page and also restoring the user ID to avoid the user being redirected to the login because session('login.id') would be empty.

@taylorotwell taylorotwell merged commit f1166db into laravel:1.x Oct 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants