authentication - Redirect user after login in laravel 5.1 -


i trying implement feature where, after logging in, user gets redirected url depending on role. have roles part set up, i'm having trouble testing user's properties after login.

i followed instructions here create user login page. have authcontroller looks this:

namespace app\http\controllers\auth;  use app\user; use validator; use app\http\controllers\controller; use illuminate\foundation\auth\throttleslogins; use illuminate\foundation\auth\authenticatesandregistersusers;  class authcontroller extends controller {      use authenticatesandregistersusers, throttleslogins;     protected $redirectto = '/test';     ... } 

my __construct() function validates user, don't know how access user object after login. presently have:

 public function __construct() {      $this->middleware('guest', ['except' => 'getlogout']);       if ( \auth::check() ) {          $user = \auth::user();          if ( $user->admin() ) {          // admin              $this->redirectto = '/admin';          } else {          // it's client              $this->redirectto = '/client/dashboard';           }      }       $user = \auth::user();      if ( is_object($user) ) {      } else {          $this->redirectto = '/auth-not-object';      }  } 

when first attempt log in administrator account, path /auth-not-object, because there isn't authenticated user object @ point.

after having attempted log in, getting bad redirect, when revisit /login url, redirected /home, believe default $redirectto in traits class uses. means we've passed authcontroller __construct() method without having changed $redirectto, though there authenticated user.

i've found other questions, such how add logic on login condition in laravel 5.2 , laravel redirect url after login, don't understand how apply answers. instance, accepted answer second question shows new methods, getcredentials() , login(), don't exist in poster's original class. not sure in class add them, or call them from, in codebase.

other similar answers show radically different way of authenticating users, such this. seems that, use solution, need re-write code, , forgo use of traits, include bonus features login throttling , on.

is there way can redirect users based on role after login, while still using these built-in traits?

im not sure if 5.1 auth same 5.2 auth, if is, remove construct , add method:

    protected function handleuserwasauthenticated( request $request, $throttles, $guard )     {         if ($throttles) {             $this->clearloginattempts( $request );         }         if ( method_exists( $this, 'authenticated' ) ) {             return $this->authenticated( $request, auth::guard( $guard )->user() );         }         return redirect()->intended( $this->redirectto );     } 

this method determine redirect , have access user object.

edit

i take above back, add following controller;

protected function authenticated( $request, $user ) {     return redirect()->intended( $user->admin() ? '/admin' : '/client/dashboard' ); } 

that should work nicely


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -