Laravel - Redirect authenticated users












0















In my Laravel application I have a registration system that uses the default scaffolding created via php artisan make:auth to register new users, but after logging in I wanted to take the user to another page called member-type so that they can select what type of member they'd like to be.



I utilitized protected function authenticated(Request $request, $user) which comes from AuthenticatesUsers to check that a user has successfully logged in, I then check whether a member type is set.



The method looks like this:



/**
* The user has been authenticated.
*
* @param IlluminateHttpRequest $request
* @param mixed $user
* @return mixed
*/
protected function authenticated(Request $request, $user)
{
Log::info("{$user->log_reference} logged in to their account with IP: {$request->getClientIp()}");

if(!$user->investor_type_selected){
// dd('I NOT SELECTED');
return redirect()->route('user.investor-type');
} elseif(!$user->member_type_selected){
dd('M NOT SELECTED');
return redirect()->route('user.member-type');
} else{
dd('BOTH SELECTED');
return redirect()->route('user.dashboard');
}
}


The methods member_type_selected andinvestor_type_selectedcome from myUser` model and they look like this:



/**
* Check whether this user has selected an investor type
*/
public function getInvestorTypeSelectedAttribute()
{
return !empty($this->investor_type) ? true : false;
}

/**
* Check whether this user has selected an investor type
*/
public function getMemberTypeSelectedAttribute()
{
return !empty($this->member_type) ? true : false;
}


Pretty simple stuff.



The dump and die was there to test whether the statements were executing.



Anyway, the issue I have is with the Middleware RedirectIfAuthenticated which looks like this as I'm using a custom guard:



/**
* Handle an incoming request.
*
* @param IlluminateHttpRequest $request
* @param Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
switch ($guard) {
case 'admin':
if (Auth::guard($guard)->check()) {
return redirect()->route('admin.dashboard');
}
break;
default:
if (Auth::guard($guard)->check()) {
return redirect()->route('user.dashboard');
}
break;
}
return $next($request);
}


Now, as soon as a user is authenticated this kicks in and bypasses my redirects with authenticated. I need this Middleware generally to make sure users get back to their dashboard, but is there a way to prevent this bypassing my redirects?










share|improve this question



























    0















    In my Laravel application I have a registration system that uses the default scaffolding created via php artisan make:auth to register new users, but after logging in I wanted to take the user to another page called member-type so that they can select what type of member they'd like to be.



    I utilitized protected function authenticated(Request $request, $user) which comes from AuthenticatesUsers to check that a user has successfully logged in, I then check whether a member type is set.



    The method looks like this:



    /**
    * The user has been authenticated.
    *
    * @param IlluminateHttpRequest $request
    * @param mixed $user
    * @return mixed
    */
    protected function authenticated(Request $request, $user)
    {
    Log::info("{$user->log_reference} logged in to their account with IP: {$request->getClientIp()}");

    if(!$user->investor_type_selected){
    // dd('I NOT SELECTED');
    return redirect()->route('user.investor-type');
    } elseif(!$user->member_type_selected){
    dd('M NOT SELECTED');
    return redirect()->route('user.member-type');
    } else{
    dd('BOTH SELECTED');
    return redirect()->route('user.dashboard');
    }
    }


    The methods member_type_selected andinvestor_type_selectedcome from myUser` model and they look like this:



    /**
    * Check whether this user has selected an investor type
    */
    public function getInvestorTypeSelectedAttribute()
    {
    return !empty($this->investor_type) ? true : false;
    }

    /**
    * Check whether this user has selected an investor type
    */
    public function getMemberTypeSelectedAttribute()
    {
    return !empty($this->member_type) ? true : false;
    }


    Pretty simple stuff.



    The dump and die was there to test whether the statements were executing.



    Anyway, the issue I have is with the Middleware RedirectIfAuthenticated which looks like this as I'm using a custom guard:



    /**
    * Handle an incoming request.
    *
    * @param IlluminateHttpRequest $request
    * @param Closure $next
    * @param string|null $guard
    * @return mixed
    */
    public function handle($request, Closure $next, $guard = null)
    {
    switch ($guard) {
    case 'admin':
    if (Auth::guard($guard)->check()) {
    return redirect()->route('admin.dashboard');
    }
    break;
    default:
    if (Auth::guard($guard)->check()) {
    return redirect()->route('user.dashboard');
    }
    break;
    }
    return $next($request);
    }


    Now, as soon as a user is authenticated this kicks in and bypasses my redirects with authenticated. I need this Middleware generally to make sure users get back to their dashboard, but is there a way to prevent this bypassing my redirects?










    share|improve this question

























      0












      0








      0








      In my Laravel application I have a registration system that uses the default scaffolding created via php artisan make:auth to register new users, but after logging in I wanted to take the user to another page called member-type so that they can select what type of member they'd like to be.



      I utilitized protected function authenticated(Request $request, $user) which comes from AuthenticatesUsers to check that a user has successfully logged in, I then check whether a member type is set.



      The method looks like this:



      /**
      * The user has been authenticated.
      *
      * @param IlluminateHttpRequest $request
      * @param mixed $user
      * @return mixed
      */
      protected function authenticated(Request $request, $user)
      {
      Log::info("{$user->log_reference} logged in to their account with IP: {$request->getClientIp()}");

      if(!$user->investor_type_selected){
      // dd('I NOT SELECTED');
      return redirect()->route('user.investor-type');
      } elseif(!$user->member_type_selected){
      dd('M NOT SELECTED');
      return redirect()->route('user.member-type');
      } else{
      dd('BOTH SELECTED');
      return redirect()->route('user.dashboard');
      }
      }


      The methods member_type_selected andinvestor_type_selectedcome from myUser` model and they look like this:



      /**
      * Check whether this user has selected an investor type
      */
      public function getInvestorTypeSelectedAttribute()
      {
      return !empty($this->investor_type) ? true : false;
      }

      /**
      * Check whether this user has selected an investor type
      */
      public function getMemberTypeSelectedAttribute()
      {
      return !empty($this->member_type) ? true : false;
      }


      Pretty simple stuff.



      The dump and die was there to test whether the statements were executing.



      Anyway, the issue I have is with the Middleware RedirectIfAuthenticated which looks like this as I'm using a custom guard:



      /**
      * Handle an incoming request.
      *
      * @param IlluminateHttpRequest $request
      * @param Closure $next
      * @param string|null $guard
      * @return mixed
      */
      public function handle($request, Closure $next, $guard = null)
      {
      switch ($guard) {
      case 'admin':
      if (Auth::guard($guard)->check()) {
      return redirect()->route('admin.dashboard');
      }
      break;
      default:
      if (Auth::guard($guard)->check()) {
      return redirect()->route('user.dashboard');
      }
      break;
      }
      return $next($request);
      }


      Now, as soon as a user is authenticated this kicks in and bypasses my redirects with authenticated. I need this Middleware generally to make sure users get back to their dashboard, but is there a way to prevent this bypassing my redirects?










      share|improve this question














      In my Laravel application I have a registration system that uses the default scaffolding created via php artisan make:auth to register new users, but after logging in I wanted to take the user to another page called member-type so that they can select what type of member they'd like to be.



      I utilitized protected function authenticated(Request $request, $user) which comes from AuthenticatesUsers to check that a user has successfully logged in, I then check whether a member type is set.



      The method looks like this:



      /**
      * The user has been authenticated.
      *
      * @param IlluminateHttpRequest $request
      * @param mixed $user
      * @return mixed
      */
      protected function authenticated(Request $request, $user)
      {
      Log::info("{$user->log_reference} logged in to their account with IP: {$request->getClientIp()}");

      if(!$user->investor_type_selected){
      // dd('I NOT SELECTED');
      return redirect()->route('user.investor-type');
      } elseif(!$user->member_type_selected){
      dd('M NOT SELECTED');
      return redirect()->route('user.member-type');
      } else{
      dd('BOTH SELECTED');
      return redirect()->route('user.dashboard');
      }
      }


      The methods member_type_selected andinvestor_type_selectedcome from myUser` model and they look like this:



      /**
      * Check whether this user has selected an investor type
      */
      public function getInvestorTypeSelectedAttribute()
      {
      return !empty($this->investor_type) ? true : false;
      }

      /**
      * Check whether this user has selected an investor type
      */
      public function getMemberTypeSelectedAttribute()
      {
      return !empty($this->member_type) ? true : false;
      }


      Pretty simple stuff.



      The dump and die was there to test whether the statements were executing.



      Anyway, the issue I have is with the Middleware RedirectIfAuthenticated which looks like this as I'm using a custom guard:



      /**
      * Handle an incoming request.
      *
      * @param IlluminateHttpRequest $request
      * @param Closure $next
      * @param string|null $guard
      * @return mixed
      */
      public function handle($request, Closure $next, $guard = null)
      {
      switch ($guard) {
      case 'admin':
      if (Auth::guard($guard)->check()) {
      return redirect()->route('admin.dashboard');
      }
      break;
      default:
      if (Auth::guard($guard)->check()) {
      return redirect()->route('user.dashboard');
      }
      break;
      }
      return $next($request);
      }


      Now, as soon as a user is authenticated this kicks in and bypasses my redirects with authenticated. I need this Middleware generally to make sure users get back to their dashboard, but is there a way to prevent this bypassing my redirects?







      php laravel






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 18:00









      Jesse OrangeJesse Orange

      604318




      604318
























          1 Answer
          1






          active

          oldest

          votes


















          1














          no need to override authenticated function , try this in your RedirectIfAuthenticated middleware



          public function handle($request, Closure $next, $guard = null)
          {
          switch ($guard) {
          case 'admin':
          if (Auth::guard($guard)->check()) {
          return redirect()->route('admin.dashboard');
          }
          break;
          default:
          if (Auth::guard($guard)->check()) {
          $user = Auth::guard($guard)->user();
          if(!$user->investor_type_selected){
          return redirect()->route('user.investor-type');
          } elseif(!$user->member_type_selected){
          return redirect()->route('user.member-type');
          } else{
          return redirect()->route('user.dashboard');
          }
          }
          break;
          }
          return $next($request);
          }





          share|improve this answer
























          • This works but I get too many redirects

            – Jesse Orange
            Nov 22 '18 at 20:08











          • in your LoginController , remove this $redirectTo and try to check if it is working or not ?

            – Saurabh Mistry
            Nov 23 '18 at 3:54













          • Only issue then, is that it uses the default redirect path in RedirectsUsers

            – Jesse Orange
            Nov 23 '18 at 9:12






          • 1





            What you have done to solve redirect issue ? Is it working proper ?

            – Saurabh Mistry
            Nov 23 '18 at 13:26













          • I'll update my question, you were right though

            – Jesse Orange
            Nov 23 '18 at 15:42











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436193%2flaravel-redirect-authenticated-users%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          no need to override authenticated function , try this in your RedirectIfAuthenticated middleware



          public function handle($request, Closure $next, $guard = null)
          {
          switch ($guard) {
          case 'admin':
          if (Auth::guard($guard)->check()) {
          return redirect()->route('admin.dashboard');
          }
          break;
          default:
          if (Auth::guard($guard)->check()) {
          $user = Auth::guard($guard)->user();
          if(!$user->investor_type_selected){
          return redirect()->route('user.investor-type');
          } elseif(!$user->member_type_selected){
          return redirect()->route('user.member-type');
          } else{
          return redirect()->route('user.dashboard');
          }
          }
          break;
          }
          return $next($request);
          }





          share|improve this answer
























          • This works but I get too many redirects

            – Jesse Orange
            Nov 22 '18 at 20:08











          • in your LoginController , remove this $redirectTo and try to check if it is working or not ?

            – Saurabh Mistry
            Nov 23 '18 at 3:54













          • Only issue then, is that it uses the default redirect path in RedirectsUsers

            – Jesse Orange
            Nov 23 '18 at 9:12






          • 1





            What you have done to solve redirect issue ? Is it working proper ?

            – Saurabh Mistry
            Nov 23 '18 at 13:26













          • I'll update my question, you were right though

            – Jesse Orange
            Nov 23 '18 at 15:42
















          1














          no need to override authenticated function , try this in your RedirectIfAuthenticated middleware



          public function handle($request, Closure $next, $guard = null)
          {
          switch ($guard) {
          case 'admin':
          if (Auth::guard($guard)->check()) {
          return redirect()->route('admin.dashboard');
          }
          break;
          default:
          if (Auth::guard($guard)->check()) {
          $user = Auth::guard($guard)->user();
          if(!$user->investor_type_selected){
          return redirect()->route('user.investor-type');
          } elseif(!$user->member_type_selected){
          return redirect()->route('user.member-type');
          } else{
          return redirect()->route('user.dashboard');
          }
          }
          break;
          }
          return $next($request);
          }





          share|improve this answer
























          • This works but I get too many redirects

            – Jesse Orange
            Nov 22 '18 at 20:08











          • in your LoginController , remove this $redirectTo and try to check if it is working or not ?

            – Saurabh Mistry
            Nov 23 '18 at 3:54













          • Only issue then, is that it uses the default redirect path in RedirectsUsers

            – Jesse Orange
            Nov 23 '18 at 9:12






          • 1





            What you have done to solve redirect issue ? Is it working proper ?

            – Saurabh Mistry
            Nov 23 '18 at 13:26













          • I'll update my question, you were right though

            – Jesse Orange
            Nov 23 '18 at 15:42














          1












          1








          1







          no need to override authenticated function , try this in your RedirectIfAuthenticated middleware



          public function handle($request, Closure $next, $guard = null)
          {
          switch ($guard) {
          case 'admin':
          if (Auth::guard($guard)->check()) {
          return redirect()->route('admin.dashboard');
          }
          break;
          default:
          if (Auth::guard($guard)->check()) {
          $user = Auth::guard($guard)->user();
          if(!$user->investor_type_selected){
          return redirect()->route('user.investor-type');
          } elseif(!$user->member_type_selected){
          return redirect()->route('user.member-type');
          } else{
          return redirect()->route('user.dashboard');
          }
          }
          break;
          }
          return $next($request);
          }





          share|improve this answer













          no need to override authenticated function , try this in your RedirectIfAuthenticated middleware



          public function handle($request, Closure $next, $guard = null)
          {
          switch ($guard) {
          case 'admin':
          if (Auth::guard($guard)->check()) {
          return redirect()->route('admin.dashboard');
          }
          break;
          default:
          if (Auth::guard($guard)->check()) {
          $user = Auth::guard($guard)->user();
          if(!$user->investor_type_selected){
          return redirect()->route('user.investor-type');
          } elseif(!$user->member_type_selected){
          return redirect()->route('user.member-type');
          } else{
          return redirect()->route('user.dashboard');
          }
          }
          break;
          }
          return $next($request);
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 18:47









          Saurabh MistrySaurabh Mistry

          4,16511030




          4,16511030













          • This works but I get too many redirects

            – Jesse Orange
            Nov 22 '18 at 20:08











          • in your LoginController , remove this $redirectTo and try to check if it is working or not ?

            – Saurabh Mistry
            Nov 23 '18 at 3:54













          • Only issue then, is that it uses the default redirect path in RedirectsUsers

            – Jesse Orange
            Nov 23 '18 at 9:12






          • 1





            What you have done to solve redirect issue ? Is it working proper ?

            – Saurabh Mistry
            Nov 23 '18 at 13:26













          • I'll update my question, you were right though

            – Jesse Orange
            Nov 23 '18 at 15:42



















          • This works but I get too many redirects

            – Jesse Orange
            Nov 22 '18 at 20:08











          • in your LoginController , remove this $redirectTo and try to check if it is working or not ?

            – Saurabh Mistry
            Nov 23 '18 at 3:54













          • Only issue then, is that it uses the default redirect path in RedirectsUsers

            – Jesse Orange
            Nov 23 '18 at 9:12






          • 1





            What you have done to solve redirect issue ? Is it working proper ?

            – Saurabh Mistry
            Nov 23 '18 at 13:26













          • I'll update my question, you were right though

            – Jesse Orange
            Nov 23 '18 at 15:42

















          This works but I get too many redirects

          – Jesse Orange
          Nov 22 '18 at 20:08





          This works but I get too many redirects

          – Jesse Orange
          Nov 22 '18 at 20:08













          in your LoginController , remove this $redirectTo and try to check if it is working or not ?

          – Saurabh Mistry
          Nov 23 '18 at 3:54







          in your LoginController , remove this $redirectTo and try to check if it is working or not ?

          – Saurabh Mistry
          Nov 23 '18 at 3:54















          Only issue then, is that it uses the default redirect path in RedirectsUsers

          – Jesse Orange
          Nov 23 '18 at 9:12





          Only issue then, is that it uses the default redirect path in RedirectsUsers

          – Jesse Orange
          Nov 23 '18 at 9:12




          1




          1





          What you have done to solve redirect issue ? Is it working proper ?

          – Saurabh Mistry
          Nov 23 '18 at 13:26







          What you have done to solve redirect issue ? Is it working proper ?

          – Saurabh Mistry
          Nov 23 '18 at 13:26















          I'll update my question, you were right though

          – Jesse Orange
          Nov 23 '18 at 15:42





          I'll update my question, you were right though

          – Jesse Orange
          Nov 23 '18 at 15:42




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436193%2flaravel-redirect-authenticated-users%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()