Laravel 8 Fortify Redirect based on roles without jetstream
I am looking for a good tutorial or advice on how to redirect after authenticated based on roles in laravel 8 fortify without jetstream. I have found one that says to create a new LoginResonse.php in App\Http\Responses\loginresponse.php which I did but I am not sure where to register this new response because the tutorial I am using says to do it in Jetstreamserviceprovider but I am not using Jetstream. Any ideas?
So I did that, thank you. But it still logs in as /home. This is what I am trying to do:
AppServiceProvider
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
$this->app->singleton(
\App\Http\Contracts\Contracts\LoginResponse::class,
\App\Http\Responses\LoginResponse::class
);
}
}
LoginResonse.php
<?php
namespace App\Http\Responses;
use App\Http\Contracts\LoginResponse as LoginResponseContract;
class LoginResponse implements LoginResponseContract
{
/**
* Create an HTTP response that represents the object.
*
* @param \Illuminate\Http\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function toResponse($request)
{
// here i am checking if the currently logout in users has a role_id of 2 which make him a regular user and then redirect to the users dashboard else the admin dashboard
if (auth()->user()->hasRole('admin')) {
return redirect()->intended(config('admin.dashboard'));
}
if (auth()->user()->hasRole('blog')) {
return redirect()->intended(config('blog.dashboard'));
}
if (auth()->user()->hasRole('editor')) {
return redirect()->intended(config('editor.dashboard'));
}
}
}
It seems like Laravel isn't recognizing my new LoginResponse.php. I added it to the AppServiceProvider but it still is logging in to /home regardless of what I have in the LoginResponse. Is there somewhere else I need to add it to?
OK I changed the LoginResponse and took out intended. Here it is. Still redirects only to /home, If I delete the vendor copy of LoginResponse, it gives an error so it tells me somewhere it is not recognizing my new version of LoginResponse but I am not sure where to make the change.
<?php
namespace App\Http\Responses;
use App\Http\Contracts\LoginResponse as LoginResponseContract;
class LoginResponse implements LoginResponseContract
{
/**
* Create an HTTP response that represents the object.
*
* @param \Illuminate\Http\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function toResponse($request)
{
if (auth()->user()->hasRole('admin')) {
return redirect()->route('admin.dashboard');
if (auth()->user()->hasRole('blog')) {
return redirect()->route('blog.dashboard');
}
if (auth()->user()->hasRole('editor')) {
return redirect()->route('editor.dashboard');
}
}
}
<?php
namespace App\Http\Controllers\Registrar;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class RegistrarController extends Controller
{
public function getDashboard()
{
return view('registrar.dashboard');
}
}