Laravel 5.4: redirect user to his account when user is login and try to go to login page












1















I have tree Auth guard (Customer, supplier, admin) and here is the RedirectIfAuthenticated.php looks like



public function handle( $request, Closure $next, $guard = null ) {
switch ( $guard ) {
case 'supplier':
{
if ( Auth::guard( $guard )->check() ) {
return redirect()->route( 'supplier-dashboard' );
}
break;
}
case 'admin':
{
if ( Auth::guard( $guard )->check() ) {
return redirect()->route( 'admin-dashboard' );
}
break;
}

default :
{
if ( Auth::guard( $guard )->check() ) {
return redirect( '/my-account' );
}
}
}

return $next( $request );
}


when I am logged in as supplier or admin and try to go to login page it takes me to the right route



but comes to the customer it takes me to the login page which is wrong here as I defined the default case to redirect to /my-account



default :
{
if ( Auth::guard( $guard )->check() ) {
return redirect( '/my-account' );
}
}


What I am missing here?




Solution




As @BilalAhmed suggest I debugged the return of ( Auth::guard( $guard )->check() ) and the result was false then I try ( Auth::guard( 'web' )->check() ) and this worked fine for me.










share|improve this question




















  • 2





    add this line into default case dd(Auth::guard( $guard )->check() ); for debugging and check or share the output

    – Bilal Ahmed
    Nov 13 '18 at 10:55











  • @BilalAhmed I got false

    – Yousef Altaf
    Nov 13 '18 at 10:59











  • @BilalAhmed Thanks a lot I got it I just replaced the if ( Auth::guard( $guard )->check() ) with if ( Auth::guard( 'web' )->check() ) and it worked fine.

    – Yousef Altaf
    Nov 13 '18 at 11:08











  • i have add answer with complete details about guard.. i hope you will understand

    – Bilal Ahmed
    Nov 13 '18 at 11:24
















1















I have tree Auth guard (Customer, supplier, admin) and here is the RedirectIfAuthenticated.php looks like



public function handle( $request, Closure $next, $guard = null ) {
switch ( $guard ) {
case 'supplier':
{
if ( Auth::guard( $guard )->check() ) {
return redirect()->route( 'supplier-dashboard' );
}
break;
}
case 'admin':
{
if ( Auth::guard( $guard )->check() ) {
return redirect()->route( 'admin-dashboard' );
}
break;
}

default :
{
if ( Auth::guard( $guard )->check() ) {
return redirect( '/my-account' );
}
}
}

return $next( $request );
}


when I am logged in as supplier or admin and try to go to login page it takes me to the right route



but comes to the customer it takes me to the login page which is wrong here as I defined the default case to redirect to /my-account



default :
{
if ( Auth::guard( $guard )->check() ) {
return redirect( '/my-account' );
}
}


What I am missing here?




Solution




As @BilalAhmed suggest I debugged the return of ( Auth::guard( $guard )->check() ) and the result was false then I try ( Auth::guard( 'web' )->check() ) and this worked fine for me.










share|improve this question




















  • 2





    add this line into default case dd(Auth::guard( $guard )->check() ); for debugging and check or share the output

    – Bilal Ahmed
    Nov 13 '18 at 10:55











  • @BilalAhmed I got false

    – Yousef Altaf
    Nov 13 '18 at 10:59











  • @BilalAhmed Thanks a lot I got it I just replaced the if ( Auth::guard( $guard )->check() ) with if ( Auth::guard( 'web' )->check() ) and it worked fine.

    – Yousef Altaf
    Nov 13 '18 at 11:08











  • i have add answer with complete details about guard.. i hope you will understand

    – Bilal Ahmed
    Nov 13 '18 at 11:24














1












1








1








I have tree Auth guard (Customer, supplier, admin) and here is the RedirectIfAuthenticated.php looks like



public function handle( $request, Closure $next, $guard = null ) {
switch ( $guard ) {
case 'supplier':
{
if ( Auth::guard( $guard )->check() ) {
return redirect()->route( 'supplier-dashboard' );
}
break;
}
case 'admin':
{
if ( Auth::guard( $guard )->check() ) {
return redirect()->route( 'admin-dashboard' );
}
break;
}

default :
{
if ( Auth::guard( $guard )->check() ) {
return redirect( '/my-account' );
}
}
}

return $next( $request );
}


when I am logged in as supplier or admin and try to go to login page it takes me to the right route



but comes to the customer it takes me to the login page which is wrong here as I defined the default case to redirect to /my-account



default :
{
if ( Auth::guard( $guard )->check() ) {
return redirect( '/my-account' );
}
}


What I am missing here?




Solution




As @BilalAhmed suggest I debugged the return of ( Auth::guard( $guard )->check() ) and the result was false then I try ( Auth::guard( 'web' )->check() ) and this worked fine for me.










share|improve this question
















I have tree Auth guard (Customer, supplier, admin) and here is the RedirectIfAuthenticated.php looks like



public function handle( $request, Closure $next, $guard = null ) {
switch ( $guard ) {
case 'supplier':
{
if ( Auth::guard( $guard )->check() ) {
return redirect()->route( 'supplier-dashboard' );
}
break;
}
case 'admin':
{
if ( Auth::guard( $guard )->check() ) {
return redirect()->route( 'admin-dashboard' );
}
break;
}

default :
{
if ( Auth::guard( $guard )->check() ) {
return redirect( '/my-account' );
}
}
}

return $next( $request );
}


when I am logged in as supplier or admin and try to go to login page it takes me to the right route



but comes to the customer it takes me to the login page which is wrong here as I defined the default case to redirect to /my-account



default :
{
if ( Auth::guard( $guard )->check() ) {
return redirect( '/my-account' );
}
}


What I am missing here?




Solution




As @BilalAhmed suggest I debugged the return of ( Auth::guard( $guard )->check() ) and the result was false then I try ( Auth::guard( 'web' )->check() ) and this worked fine for me.







php laravel authentication laravel-5.4






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 11:11







Yousef Altaf

















asked Nov 13 '18 at 10:52









Yousef AltafYousef Altaf

1,63922441




1,63922441








  • 2





    add this line into default case dd(Auth::guard( $guard )->check() ); for debugging and check or share the output

    – Bilal Ahmed
    Nov 13 '18 at 10:55











  • @BilalAhmed I got false

    – Yousef Altaf
    Nov 13 '18 at 10:59











  • @BilalAhmed Thanks a lot I got it I just replaced the if ( Auth::guard( $guard )->check() ) with if ( Auth::guard( 'web' )->check() ) and it worked fine.

    – Yousef Altaf
    Nov 13 '18 at 11:08











  • i have add answer with complete details about guard.. i hope you will understand

    – Bilal Ahmed
    Nov 13 '18 at 11:24














  • 2





    add this line into default case dd(Auth::guard( $guard )->check() ); for debugging and check or share the output

    – Bilal Ahmed
    Nov 13 '18 at 10:55











  • @BilalAhmed I got false

    – Yousef Altaf
    Nov 13 '18 at 10:59











  • @BilalAhmed Thanks a lot I got it I just replaced the if ( Auth::guard( $guard )->check() ) with if ( Auth::guard( 'web' )->check() ) and it worked fine.

    – Yousef Altaf
    Nov 13 '18 at 11:08











  • i have add answer with complete details about guard.. i hope you will understand

    – Bilal Ahmed
    Nov 13 '18 at 11:24








2




2





add this line into default case dd(Auth::guard( $guard )->check() ); for debugging and check or share the output

– Bilal Ahmed
Nov 13 '18 at 10:55





add this line into default case dd(Auth::guard( $guard )->check() ); for debugging and check or share the output

– Bilal Ahmed
Nov 13 '18 at 10:55













@BilalAhmed I got false

– Yousef Altaf
Nov 13 '18 at 10:59





@BilalAhmed I got false

– Yousef Altaf
Nov 13 '18 at 10:59













@BilalAhmed Thanks a lot I got it I just replaced the if ( Auth::guard( $guard )->check() ) with if ( Auth::guard( 'web' )->check() ) and it worked fine.

– Yousef Altaf
Nov 13 '18 at 11:08





@BilalAhmed Thanks a lot I got it I just replaced the if ( Auth::guard( $guard )->check() ) with if ( Auth::guard( 'web' )->check() ) and it worked fine.

– Yousef Altaf
Nov 13 '18 at 11:08













i have add answer with complete details about guard.. i hope you will understand

– Bilal Ahmed
Nov 13 '18 at 11:24





i have add answer with complete details about guard.. i hope you will understand

– Bilal Ahmed
Nov 13 '18 at 11:24












1 Answer
1






active

oldest

votes


















0














Laravel (authenticate middleware) have two types of auth guard, web & api. for custom you must define in authenticate model like protected $guard = 'customer';



By default laravel used web like Auth::guard('web')->user(). it's similar to Auth::user(). also you can check default guard in config/auth.php file



when execute this line if ( Auth::guard( $guard )->check() ) that's mean Auth::guard( 'customer' )->check() but there is no customer guard define in auth.php file



if you want to create custom guard then read this articles






share|improve this answer



















  • 1





    Thanks will read this article, about config/auth.php yes of course I have in my guards array something like this 'web' => [ 'driver' => 'session', 'provider' => 'users', ], and the providers array I have this 'users' => [ 'driver' => 'eloquent', 'model' => AppModelsUser::class, ], will check and read the article again, thanks dear

    – Yousef Altaf
    Nov 13 '18 at 11:31











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%2f53279393%2flaravel-5-4-redirect-user-to-his-account-when-user-is-login-and-try-to-go-to-lo%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









0














Laravel (authenticate middleware) have two types of auth guard, web & api. for custom you must define in authenticate model like protected $guard = 'customer';



By default laravel used web like Auth::guard('web')->user(). it's similar to Auth::user(). also you can check default guard in config/auth.php file



when execute this line if ( Auth::guard( $guard )->check() ) that's mean Auth::guard( 'customer' )->check() but there is no customer guard define in auth.php file



if you want to create custom guard then read this articles






share|improve this answer



















  • 1





    Thanks will read this article, about config/auth.php yes of course I have in my guards array something like this 'web' => [ 'driver' => 'session', 'provider' => 'users', ], and the providers array I have this 'users' => [ 'driver' => 'eloquent', 'model' => AppModelsUser::class, ], will check and read the article again, thanks dear

    – Yousef Altaf
    Nov 13 '18 at 11:31
















0














Laravel (authenticate middleware) have two types of auth guard, web & api. for custom you must define in authenticate model like protected $guard = 'customer';



By default laravel used web like Auth::guard('web')->user(). it's similar to Auth::user(). also you can check default guard in config/auth.php file



when execute this line if ( Auth::guard( $guard )->check() ) that's mean Auth::guard( 'customer' )->check() but there is no customer guard define in auth.php file



if you want to create custom guard then read this articles






share|improve this answer



















  • 1





    Thanks will read this article, about config/auth.php yes of course I have in my guards array something like this 'web' => [ 'driver' => 'session', 'provider' => 'users', ], and the providers array I have this 'users' => [ 'driver' => 'eloquent', 'model' => AppModelsUser::class, ], will check and read the article again, thanks dear

    – Yousef Altaf
    Nov 13 '18 at 11:31














0












0








0







Laravel (authenticate middleware) have two types of auth guard, web & api. for custom you must define in authenticate model like protected $guard = 'customer';



By default laravel used web like Auth::guard('web')->user(). it's similar to Auth::user(). also you can check default guard in config/auth.php file



when execute this line if ( Auth::guard( $guard )->check() ) that's mean Auth::guard( 'customer' )->check() but there is no customer guard define in auth.php file



if you want to create custom guard then read this articles






share|improve this answer













Laravel (authenticate middleware) have two types of auth guard, web & api. for custom you must define in authenticate model like protected $guard = 'customer';



By default laravel used web like Auth::guard('web')->user(). it's similar to Auth::user(). also you can check default guard in config/auth.php file



when execute this line if ( Auth::guard( $guard )->check() ) that's mean Auth::guard( 'customer' )->check() but there is no customer guard define in auth.php file



if you want to create custom guard then read this articles







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 11:23









Bilal AhmedBilal Ahmed

3,25431435




3,25431435








  • 1





    Thanks will read this article, about config/auth.php yes of course I have in my guards array something like this 'web' => [ 'driver' => 'session', 'provider' => 'users', ], and the providers array I have this 'users' => [ 'driver' => 'eloquent', 'model' => AppModelsUser::class, ], will check and read the article again, thanks dear

    – Yousef Altaf
    Nov 13 '18 at 11:31














  • 1





    Thanks will read this article, about config/auth.php yes of course I have in my guards array something like this 'web' => [ 'driver' => 'session', 'provider' => 'users', ], and the providers array I have this 'users' => [ 'driver' => 'eloquent', 'model' => AppModelsUser::class, ], will check and read the article again, thanks dear

    – Yousef Altaf
    Nov 13 '18 at 11:31








1




1





Thanks will read this article, about config/auth.php yes of course I have in my guards array something like this 'web' => [ 'driver' => 'session', 'provider' => 'users', ], and the providers array I have this 'users' => [ 'driver' => 'eloquent', 'model' => AppModelsUser::class, ], will check and read the article again, thanks dear

– Yousef Altaf
Nov 13 '18 at 11:31





Thanks will read this article, about config/auth.php yes of course I have in my guards array something like this 'web' => [ 'driver' => 'session', 'provider' => 'users', ], and the providers array I have this 'users' => [ 'driver' => 'eloquent', 'model' => AppModelsUser::class, ], will check and read the article again, thanks dear

– Yousef Altaf
Nov 13 '18 at 11:31


















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%2f53279393%2flaravel-5-4-redirect-user-to-his-account-when-user-is-login-and-try-to-go-to-lo%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







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values