php - Laravel 5.4: redirect user to his account when user is login and try to go to login page
Get the solution ↓↓↓I have tree Auth guard (Customer, supplier, admin) and here is theRedirectIfAuthenticated.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 rightroute
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 wasfalse
then I try( Auth::guard( 'web' )->check() )
and this worked fine for me.
Answer
Solution:
Laravel (authenticate middleware) have two types of auth guard,web
&api
. for custom you must define in authenticate model likeprotected $guard = 'customer';
By default laravel usedweb
likeAuth::guard('web')->user()
. it's similar toAuth::user()
. also you can check default guard inconfig/auth.php
file
when execute this lineif ( Auth::guard( $guard )->check() )
that's meanAuth::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 solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: you must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.