Laravel 5.4: redirect user to his account when user is login and try to go to login page
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
add a comment |
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
2
add this line into default casedd(Auth::guard( $guard )->check() );
for debugging and check or share the output
– Bilal Ahmed
Nov 13 '18 at 10:55
@BilalAhmed I gotfalse
– Yousef Altaf
Nov 13 '18 at 10:59
@BilalAhmed Thanks a lot I got it I just replaced theif ( Auth::guard( $guard )->check() )
withif ( 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
add a comment |
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
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
php laravel authentication laravel-5.4
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 casedd(Auth::guard( $guard )->check() );
for debugging and check or share the output
– Bilal Ahmed
Nov 13 '18 at 10:55
@BilalAhmed I gotfalse
– Yousef Altaf
Nov 13 '18 at 10:59
@BilalAhmed Thanks a lot I got it I just replaced theif ( Auth::guard( $guard )->check() )
withif ( 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
add a comment |
2
add this line into default casedd(Auth::guard( $guard )->check() );
for debugging and check or share the output
– Bilal Ahmed
Nov 13 '18 at 10:55
@BilalAhmed I gotfalse
– Yousef Altaf
Nov 13 '18 at 10:59
@BilalAhmed Thanks a lot I got it I just replaced theif ( Auth::guard( $guard )->check() )
withif ( 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
add a comment |
1 Answer
1
active
oldest
votes
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
1
Thanks will read this article, aboutconfig/auth.php
yes of course I have in myguards
array something like this'web' => [ 'driver' => 'session', 'provider' => 'users', ],
and theproviders
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
1
Thanks will read this article, aboutconfig/auth.php
yes of course I have in myguards
array something like this'web' => [ 'driver' => 'session', 'provider' => 'users', ],
and theproviders
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
add a comment |
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
1
Thanks will read this article, aboutconfig/auth.php
yes of course I have in myguards
array something like this'web' => [ 'driver' => 'session', 'provider' => 'users', ],
and theproviders
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
add a comment |
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
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
answered Nov 13 '18 at 11:23
Bilal AhmedBilal Ahmed
3,25431435
3,25431435
1
Thanks will read this article, aboutconfig/auth.php
yes of course I have in myguards
array something like this'web' => [ 'driver' => 'session', 'provider' => 'users', ],
and theproviders
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
add a comment |
1
Thanks will read this article, aboutconfig/auth.php
yes of course I have in myguards
array something like this'web' => [ 'driver' => 'session', 'provider' => 'users', ],
and theproviders
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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() )
withif ( 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