11

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.

SK Toke
  • 314
  • 1
  • 3
  • 7
  • that method is only for returning the user, so you can't do any redirect in there .... you can return `null` or `false`, but you won't have any custom message – lagbox Sep 26 '20 at 05:06
  • but in case of returning message to user how i can do it here in fortify? – SK Toke Sep 26 '20 at 05:27

6 Answers6

9

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.

Chester M
  • 376
  • 2
  • 8
8

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

DinosaurHunter
  • 652
  • 2
  • 9
  • 23
6

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.

hatef
  • 5,491
  • 30
  • 43
  • 46
5

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');
        }
    });
Qaurusai
  • 51
  • 1
  • 2
1

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

'home' => '/dashboard',

0

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!

P. K. Tharindu
  • 2,565
  • 3
  • 17
  • 34