I have two types of users: "vendors" and "clients". And I'm currently using Laravel's built-in Auth Controllers
(from the make:auth
command) to do my client user authentication.
And since I have two kinds of users, I have changed the $redirectTo
property on the LoginController
, RegisterController
, and ResetPasswordController
to /client/home
.
Here is proof:
RegisterController
LoginController
Now, it redirects to /client/home
every time I successfully do registration, login, and password reset.
But the problem is when I'm in mysite.com/client/home
already, whenever I would try to go to mysite.com/register
or mysite.com/login
via the address bar, it would redirect to mysite.com/home
instead of mysite.com/client/home
...
How can I make it redirect to mysite.com/client/home
whenever an authenticated user tries to go to /login
or /register
?
The simplest option is to create separate controllers for both of your login areas. It will be easier to manage later on, and you can customise the behaviour a bit better.
The default folder structure looks like this:
app
|__Http
|__Controllers
|__Auth
|__ForgotPasswordController.php
|__LoginController.php
|__RegisterController.php
|__ResetPasswordController.php
You could create an additional folder for your client controllers, like so:
app
|__Http
|__Controllers
|__Auth
| |__ForgotPasswordController.php
| |__LoginController.php
| |__RegisterController.php
| |__ResetPasswordController.php
|__Client
|__Auth
|__ForgotPasswordController.php
|__LoginController.php
|__RegisterController.php
|__ResetPasswordController.php
This way you can customise the $redirectTo
properties of each controllers individually.
As an alternative solution, you could overwrite the redirectPath
of the RedirectsUsers trait, by creating a redirectPath
method in your respective controllers, and return the URL you'd like:
public function redirectPath()
{
if (\Request::is('client/*'))
{
return url('client/home');
}
return url('home');
}
The advantage of this second solution is that you can return controller actions and named routes as well. I personally don't like routing to URLs, as if I ever decide to change them, then I'll have to change them everywhere. Using controller actions seems like a better idea, but you could run into the same problem if you refactor your code later on. I prefer using named routes, as I can give them a sensible name, and never change them again, yet still keep all my redirects in a working order.