samedi 20 août 2016

Dynamic redirect after not working in Laravel 5.1

I've followed the instructions here to make a login page. It's working; however I'm having trouble making the redirect dynamic. What I mean is that, I want to redirect the user to different URLs, based on their role (custom models that I've already defined).

Here's my AuthController (I've removed boilerplate):

 use AuthenticatesAndRegistersUsers, ThrottlesLogins;
 private $redirectTo = '/test';

 public function __construct()
 {
     $this->middleware('guest', ['except' => 'getLogout']);

     $this->redirectTo = '/dashboard';
     $user = \Auth::user();
     if ( ($user->admin() ) {
     // an admin
         $this->redirectTo = '/admin';
     } else {
     // it's a client
         $this->redirectTo = '/client/dashboard';
     }
 }

 protected function validator(array $data)
 {
     return Validator::make($data, [
         'name' => 'required|max:255',
         'email' => 'required|email|max:255|unique:users',
         'password' => 'required|confirmed|min:6',
     ]);
 }

However, it still redirects everyone to /home. I've dd($this->redirectTo) and it shows the expected value.

How do I dynamically set the redirect path after a user has authenticated?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire