Laravel Fortify Customize Authentication Redirect

23.1k views Asked by At

In Laravel fortify on customization authentication process, i can not able to redirect to login page with error message which we were able to do in Auth. Here is the customization documentation link: https://jetstream.laravel.com/1.x/features/authentication.html#customizing-the-authentication-process

        if ($user && Hash::check($request->password, $user->password) && $user->status == 'active') {
            return $user;
        } elseif ($user->status == 'inactive') {
            //redirect with some error message to login blade
        } elseif ($user->status == 'blocked') {
            //redirect with some error message to login blade
        }

Please help me out on this.

6

There are 6 answers

0
P. K. Tharindu On

Currently, Fortify doesn't have a way to customize redirects.

There is an open issue requesting this bahavior here: https://github.com/laravel/fortify/issues/77

There is a good chance this will be added soon!

1
DinosaurHunter On

I understand your frustration with the documentation (or lack thereof). I had a similar problem and this is how I managed to do it:

if ($user && in_array($user->status_id, [1,2,3])) {
    if (Hash::check($request->password, $user->password)) {
        return $user;
    }
}
else {
    throw ValidationException::withMessages([
        Fortify::username() => "Username not found or account is inactive. Please check your username.",
    ]);
}

https://github.com/laravel/fortify/issues/94#issuecomment-700777994

0
Chester M On

For those coming from google search who use Laravel Jetstream (which uses Fortify): Snapey of Laracasts answered this question and made a tutorial which uses your own LoginResponse to override the default login behavior.

I made mine like this but of course you should create your own according to your needs.

// app/Http/Responses/LoginResponse.php
namespace App\Http\Responses;

use Illuminate\Support\Facades\Auth;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;

class LoginResponse implements LoginResponseContract
{

    /**
     * @param  $request
     * @return mixed
     */
    public function toResponse($request)
    {
        // replace this with your own code
        // the user can be located with Auth facade
        
        $home = Auth::user()->is_admin ? config('fortify.dashboard') : config('fortify.home');
            
        return $request->wantsJson()
            ? response()->json(['two_factor' => false])
            : redirect($home);
    }

}

The next step it to modify JetstreamServiceProvider to use your LoginReponse

public function boot()
{
    $this->configurePermissions();

    Jetstream::deleteUsersUsing(DeleteUser::class);

    // register new LoginResponse
    $this->app->singleton(
        \Laravel\Fortify\Contracts\LoginResponse::class,
        \App\Http\Responses\LoginResponse::class
    );
}

Hope it saves you time.

0
hatef On

For SPA applications that only want a custom JSON response rather than the default {two_factor: false}.

Create a custom response that implements LoginResponse contract of Fortify (in this case I'm returning the user object):

<?php

declare(strict_types=1);

namespace App\Http\Responses;

use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
use Symfony\Component\HttpFoundation\Response;

class LoginResponse implements LoginResponseContract
{
    public function toResponse($request): Response
    {
        return response()->json(auth()->user());
    }
}

Add this line to the boot method of FortifyServiceProvider:

$this->app->singleton(LoginResponseContract::class, LoginResponse::class);

Making sure you've imported the namespaces correctly:

use App\Http\Responses\LoginResponse;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;

If you are still not getting a JSON from the server, make sure your request has the right headers. It should be an XHR request that accepts application/json.

Inspired by this article from Laravel News.

0
ass-king some questions On

goto \config\fortify.php at line 66, change home value to any path you want

'home' => '/dashboard',

2
Qaurusai On

for example - custom redirect after login/logout/register. By request parameter - alias

In FortifyServiceProvider class

Add.

use Laravel\Fortify\Contracts\LoginResponse;
use Laravel\Fortify\Contracts\LogoutResponse;
use Laravel\Fortify\Contracts\RegisterResponse;

And then in register method.

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

    $this->app->instance(LoginResponse::class, new class implements LoginResponse {
        public function toResponse($request)
        {
            return redirect()->intended('/'.$request->alias.'/admin/dashboard');
        }
    });

    $this->app->instance(RegisterResponse::class, new class implements RegisterResponse {
        public function toResponse($request)
        {
            return redirect('/'.$request->alias.'/admin/dashboard');
        }
    });