Skip to main content
Tutorial Free

Redirect after Login or Register: Add your own Method

December 28, 2017
1 min read
In Laravel's Auth system you can customize a few most important things - one of them is a variable $redirectTo - where to take the user after login/registration. But there's even more to customize. By default, our app/Http/Controllers/Auth/RegisterController.php has this variable:
class RegisterController extends Controller
{
    protected $redirectTo = '/home';

    // ... More functions here
}
Which means - after registration, user is redirected to /home. And we can change it here, easily, by just changing this variable. But what if, in your case, it's more than a simple variable? What if you want to redirect user to somewhere, depending on their role or some other case? You can define it as a separate method with the same name.
class RegisterController extends Controller
{
    protected $redirectTo = '/home';

    protected function redirectTo()
    {
        if (auth()->user()->role_id == 1) {
            return '/admin';
        }
        return '/home';
    }

}
Basically, we can add any logic we want inside that method, and it will override the variable $redirectTo. We can do same thing with LoginController. You can read more about customizations in Auth - in the official documentation.

Enjoyed This Tutorial?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Recent Courses

Comments & Discussion

No comments yet…

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.