Courses

[Mini-Course] Laravel 11: Breeze with User Role Areas

Auto-Redirect to Correct Area after Login/Register

Next, let's work on redirecting users to the correct page after login or registration. For now, we have hard-coded a redirect to the student timetable page. Let's change it.


User Model: Redirect Method

We will create a method that returns a route name based on the user's role.

This method can be placed in various places, but I think it is best suited in the User Model. In the method, we can use the PHP match expressions to return a route name.

app/Models/User.php:

class User extends Authenticatable
{
use HasFactory, Notifiable;
 
// ...
 
public function getRedirectRouteName(): string
{
return match ((int) $this->role_id) {
1 => 'student.timetable',
2 => 'teacher.timetable',
};
}
}

Controllers: Redirect Based on Role

Now, we can use this method to redirect users to the proper page based on its role.

app/Http/Controllers/Auth/AuthenticatedSessionController.php:

class AuthenticatedSessionController extends Controller
{
// ...
 
public function store(LoginRequest $request): RedirectResponse
{
$request->authenticate();
 
$request->session()->regenerate();
 
return redirect()->intended(route('student.timetable', absolute: false));
return redirect()->intended(route(auth()->user()->getRedirectRouteName(), absolute: false));
}
 
// ...
}

app/Http/Controllers/Auth/RegisteredUserController.php:

class RegisteredUserController extends Controller
{
// ...
 
public function store(Request $request): RedirectResponse
{
// ...
 
return redirect(route('student.timetable', absolute: false));
return redirect(route(auth()->user()->getRedirectRouteName(), absolute: false));
}
}

After logging in or registering, the user is redirected to the correct page.

However, if someone knows the URL for the different roles pages, they can still access them. We will add restrictions in the next lesson.

Previous: Separating Teacher and Student Layouts
avatar

I setup the larastan plugin in this project and I am getting 1 error dealing with the (int) that we setup in hear

return match ((int)$this->role_id)

$ ./vendor/bin/phpstan analyse Note: Using configuration file C:\xampp\htdocs\awi_projects\project-1\phpstan.neon. 42/42 [============================] 100%


Line Models\User.php


49 Match expression does not handle remaining values: int<min, 0>|int<4, max>


[ERROR] Found 1 error

have a suggestion as to how to fix this ?

maby using 0,1,2 instead ?

avatar
You can use Markdown
avatar

I also had to add the redirection to that route in the 'RedirectIfAuthenticated' middleware.

avatar

just what did you add please give example

avatar

I am haveing NO issue when I am using laravel 10.8.0

avatar

Yes, sorry Im using laravel 9.19 So I had to add this line of code in 'RedirectIfAuthenticated':

foreach ($guards as $guard) { if (Auth::guard($guard)->check()) { //return redirect(RouteServiceProvider::HOME); return redirect()->route( auth()->user()->getRedirectRouteName() ); } }

avatar
You can use Markdown
avatar
You can use Markdown