I learned laravel in september 2017 I learned it from your channel following your how to make a blog with laravel 5.4, It's october 2018 and I have a job (developer in an organisation) It all happened because of you Alex, I thank you for helping the guys like me out in being a laravel developer, You helped me change my life and get a job, you are actually changing lives sir, Thank you so much for this
This is a great tut. To solve newer versions (5.6 tested) with missing "function unauthenticated" in the "Handler", just create the function. Works fine!
Awesome guide. In the RedirectIfAuthenticated middleware, I actually did the IF statement first, then the SWITCH statement inside the IF. Thanks, this is what I needed.
Awesome tutorial and very well explained as usual. I have a request for a you. Can you please make a series on User Role/Authorization app in Laravel? that would be really helpful.
The best tutorial on laravel multi-authentication. Previously I relied on Sentinel package for this. But, as you said, had problems when migration to the next laravel version.Thanks a million!
I saw that some people are trying to use this with new laravel version (5.5 and 5.6) without success, I got it doing this: 1 - Add to header of "Handler.php" the following: use Illuminate\Auth\AuthenticationException; 2 - At the end of file, add the following method: protected function unauthenticated($request, AuthenticationException $exception) { if($request->expectsJson()){ return response()->json(['message' => $exception->getMessage()], 401); }
$guard = array_get($exception->guards(), 0); switch ($guard){ case 'admin': $login = 'admin.login'; break; default: $login = 'login'; break; } return redirect()->guest(route($login)); } It's working for me.
thanks alot mate for your comment. it definetly helped alot, but still i can get to user login view while admin is already logged in, which allow my user to get logged in as well, thus i can have both admin and user logged in at same time. Do you have any idea why is this happening!
I am on Laravel 5.7 and can't get this to work. If I dd($exception), I get the following: AuthenticationException {#499 ▼ #guards: array:1 [▼ 0 => null ] #redirectTo: "127.0.0.1:8000/login" #message: "Unauthenticated." #code: 0 #file: "C:\Users\Spare\OneDrive\Werner\Sites\multiauth\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authenticate.php" #line: 67 trace: {▶} } I am logged in with my admin user. I have tried restarting the server, but no success.
Great, this is just awesome! Great Tutorials...... clear and complete explanation for most of the things. Just a quick suggestion for viewers who are using laravel 6.0 (as well as me) is that you may face problems with handler.php. No worries, just follow stackoverflow instantly to solve and understand them
Nice! Learning great stuff from you as a Laravel noob. You make the best Laravel tutorials, great explanations. Can't wait for the advanced cms series to come out.
For anyone doing this tutorial in Laravel 5.5 or newer, you'll need to pull the exception from the framework into your project's exception hander as explained here: stackoverflow.com/questions/45340855/laravel-5-5-change-unauthenticated-login-redirect-url Great tutorial Alex, love your work mate. :-)
For Laravel 6+ and if you only have 2 type of users you can use the following: public function render($request, Exception $exception) { if($exception instanceof AuthenticationException){ $guard = Arr::get($exception->guards(), 0); return redirect(route( ($guard == 'seller')?'seller.login':'login' )); } return parent::render($request, $exception); } include this in the top: use Illuminate\Auth\AuthenticationException; use Illuminate\Support\Arr;
Great tutorials Alex, thank you very much!! Did you notice that: - if you login as user in multiauth.dev/admin/login you don't get "These credentials do not match our records." - if you login as admin in multiauth.dev/login you correctly get "These credentials do not match our records."
U can use session object to display session()->flash('message', 'These credentials do not match our records.'); then in blade template check if session exists @if($flash = session('message')) {{ $flash }} @endif
For Laravel 6. + , you can do a little bit of change: use Illuminate\Auth\AuthenticationException; use Illuminate\Support\Arr; public function render($request, Exception $exception) { if($exception instanceof AuthenticationException){ $guard = Arr::get($exception->guards(), 0); switch($guard){ case 'admin': return redirect(route('admin.login')); break; default: return redirect(route('login')); break; } } return parent::render($request, $exception); }
Great tutorial! However, there is one major problem: when you logout of Admin, L5 sends you to the hard coded default of "/" which will refer to the users login, not the Admin login. This is going to be hugely confusing for Admins who log out, then wish to log back in, and the system will advise them that their username / password is incorrect. Ideally, you need to have a single login page under "/" for both users and Admins, and so in this way it will not matter if L5 sends you back to that. I think this could be achievable if when an admin / user logs in, you check the users DB table where you have a column denoting if they are an Admin or user. If the user is an Admin, then you will be sent via the admin guard to the Admin login controller. This way you will still have admin and user guards protecting their own pages, and a single login page.
Thank you so much for your videos, I really feel like I'm learning and not just guessing whats going on without an explication. greetings from Chile ;D
Hey man, great tutorial. I had a question regarding the nav blade for admin. There is a line where it shows the logged in user Auth::user()->email. I am confused as to how to use admin()->email.. am I missing something?
Hi Alex, Thnx for the awesome vids! I really like how you explain things thoroughly and clear. I wanted to ask you when you are planning to add the last video in this series? I really want to learn about the 'Forgot your password' part.
This tutorial is simply GREAT! :-) Just a couple of things, please... 1. In the RedirectifAuthenticated middleware, can I replace your code with this? (the "switch" is inside the "if" so I don't repeat the "if (Auth::guard($guard)->check())" part) if (Auth::guard($guard)->check()) { switch ($guard) { case 'admin': return redirect(route('admin.dashboard')); break; default: return redirect(route('home')); } } seems that it work, but is there any side effect? 2. Can I "oversimplify" all with the following code? And again, are there some side effects? if (Auth::guard($guard)->check()) { return redirect()->back(); } In this way I'm always redirected to the previous page, that I know for sure it works (because I'm coming from there) Thanks a lot, again :-)
for those newbie or beginner like me that tried this on laravel 5.5. to make it work try to see this link below stackoverflow.com/questions/45340855/laravel-5-5-change-unauthenticated-login-redirect-url
@@GursewakSingh-gx6ev i have a problem.. in laravel 7 i cant find exception handler but authenticate middleware is present and the function doing returning the user to the respective page only takes the request and i cant find how to pull guard from the request... im not sure if it even exists
@@norbertseho Oh okay I understand your problem, so much have changed since this video was uploaded. Drop me a message on linkedIn we will discuss it over there. www.linkedin.com/in/gursewak-singh-ai-2019/
I really loves your laravel series it helps me a lot, could you come up with series explaining untouched part of laravel like containers, event, listeners, providers, gates with a project?
For Laravel 6 go to app/Exeptions/Handler.php ================================================ public function render($request, Exception $exception) { if($exception instanceof AuthenticationException){ $guard = Arr::get($exception->guards(), 0); switch($guard){ case 'admin': return redirect(route('admin.login')); break; default: return redirect(route('login')); break; } } return parent::render($request, $exception); } } ================================================= Also dont forget to include at the top of the page: use Arr; use Illuminate\Auth\AuthenticationException;
Cool video but honestly a prefer to manage this case as a role case and manage everything as users permissions roles to give acces and view different ui with something like spattie permissions package its well tested and its code works with guards as your code does so it guarantee compatibilty and not remake the wheel but thanks this series is very educational thanks
Hello Alex, First of all I would like to thank you for all the effort you are doing with all the lessons. I want to ask you a favour if you may please. Could you make video lessons about making an ACL or Role Auth System? Like when you want to create a website where Admins have multi types as you said. Thanks :)
this tutorial is great. I would like to ask if there is a logout for both admin and casual users? for example if the admin and casual user are both logged in, if the admin click the logout, is casual user still logged in? Thank you so much for this tutorial +
You can do like this in 5.5 or above . Just include these three lines below namespace use Request; use Illuminate\Auth\AuthenticationException; use Response; After that, Upgrade this function public function render($request, Exception $exception) { $class = get_class($exception); switch($class) { case 'Illuminate\Auth\AuthenticationException': $guard = array_get($exception->guards(), 0); switch ($guard) { case 'admin': $login = 'admin.login'; break; default: $login = 'login'; break; } return redirect()->route($login); } return parent::render($request, $exception); }
Shorthand versions., this works for me in 5.5: For the auth middleware... $login = (array_get($exception->guards(), 0) == 'admin') ? 'admin.login' : 'login'; return redirect()->guest(route($login)); For the guest middleware... $home = ($guard == 'admin') ? 'admin.dashboard' : 'home'; return redirect(route($home));
In truth it is an excellent tutorial and you are a very good teacher, you helped me a lot. Thank you very much ... if possible I would like you to make a notifications tutorial in laravel
When I use all things which you provide in this video all i change but when i going to login in error come in verifyCsrfToken.php (trying to get property 'headers' of non-object; in laravel 5.5 /admin/login
11:00 - it's possible that guard array is empty. 14:00 - it's possible that $guars is null. Both scenarios happened to me if the guard was default. It wasn't "web" but null. Your code is working because you use "default" in switch statement. It could stop working if you did "case 'web':" instead.
In Laravel 5.5 Handler.php has been changed. So how can we implement this functionality? Do I need to change in RedirectIfAuthenticated Class ? check in handle function and redirect accordingly ?.
Going to be working on an updated video of this soon. I have been busy with other projects, but I am coming back to update it to 5.5 with the new handler methods.
stackoverflow.com/a/45994994 hi, i tried this workaround on stackoverflow and it worked for me. perhaps you can try it as well until Alex post the L5.5 update for this video. Cheers.
Good tutorial :D but still confused, about Which file Laravel detected if the guard is admin or it was user ?. so that If I make more Auth example I had 5 User Level, such as "super-admin", "admin", "manager", "user", "user2" I can modified it. Should I make more prefix on route (web.php) ? or what. thanks
In 5.8 the Exception handler function unauthenticated has been moved to the vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php So in the app/Exceptions/handler.php the function doesn't exist. Al you have to do is add it to override the base function in the vendor directory. Remember to add the use statements
Hi Curtis, i just wanted to say that i already created something very similar but kind of another way around....i have created two different register forms and there in register controller i set the user if he is an admin or a regular user (depends on which register form they have used) so the login form is the same for any kind of users. And the users table is also where the admins are stored. I would like you to give me a feedback on this...want to hear your opinion on this. Is it good or bad practice?
In case if anyone don't want the logged in user access the login form of admin, go to your AdminLoginController then change the showLoginForm function to this public function showLoginForm() { if(Auth::check()){ return redirect('/home'); } else { return view('auth.admin-login'); } }
Great tutorial. Hello alex. I can login on admin page. Also, validation is okay. But, for the Auth::guard('admin')->attempt, it does not display `Email and password mismtached` error. Please help. thanks
Hi, thank you for all awesome series. I am trying this with laravel 5.5.*. And in this video i override the method unauthenticated and its works fine. is this right way to do unauthenticated redirects (in 5.5.*)??? if any other way to do it i am glad to know that.
Why Auth::check(); not working.. Always return false, even though I have login as someone from the above ? I'm implemented this method (part 1 - part 3) to Laravel 5.6 Thx
Its true that Handler.php is shrinken but there is no reduction in its functionality, meaning you can always override the functions you were using before. Like *unauthenticated* is not there anymore but you can define it and use it to your will !
I think that after clicking logout, it should return to the login page judged by user type. That is, admin returns to /admin/login and user returns to /home or /login page. And how to do this?
laravel 5.6: the app\Exceptions\Handler.php has been shortened but the "unauthenticated" method is still available in the "Illuminate\Foundation\Exceptions\Handler". as stated in laravel.com/docs/5.6/authentication#protecting-routes under "Redirecting Unauthenticated Users" you have to add the namespace "use Illuminate\Auth\AuthenticationException;" to your Handler.php and then create the "unauthenticated" method. The documentation shows the redirect with a ternary operator. I recommend you comment that one out and replace it with the code from alex' video. That's it.
Hey guys, if you download the source code on Github don't forget to add this code to AppServiceProvider.php: use Illuminate\Support\Facades\Schema; public function boot() { Schema::defaultStringLength(191); } // Do it before running the migrations.
Alex 11:30 admin.login didn't work for me i had to add route('admin.login') instead and I made the switch statement much simpler: $login = $guard == 'admin' ? route('admin.login') : 'login';
As far as I understand, when I click "logout" on /home or on /admin I log out of both my accounts (if I was logged in as user and as admin at the same time)?
I love your tutorial. I have it completely working. My question is in my user table I created role. I have three different types of users in that table. I want to be able to have user1 redirect to user1.blade.php user2 to redirect to user2.blade.php and user3 to redirect to user3.blade.php upon logging in based on that field called role. How difficult would it be to modify this tutorial to make it do that?
Hi, Could you please help me.. I have installed Laravel 5.5 and try to do what you do in this tutorial series. But after doing all things, it doesn't redirect to the intended page when I use construct method with 'auth:admin' middleware in AdminConroller. What was happend there..? thanks.
if you are using version 5.5 or 5.6 use this instead in your app/Exceptions/Handler.php public function render($request, Exception $exception) { //gets the class exception $class = get_class($exception); switch($class) { case 'Illuminate\Auth\AuthenticationException': $guard = array_get($exception->guards(), 0); switch ($guard) { case 'admin': $login = 'admin.login'; break; default: $login = 'login'; break; } return redirect()->route($login); } return parent::render($request, $exception); }
hello ,i have a question with "protected function unauthenticated" on laravel 5.8 link address have change to vendor/foundation/exceptions/handler.php , if i do rewrite it every time i use composer update will replace the file any idea or anyway can solve this problem?
I learned laravel in september 2017 I learned it from your channel following your how to make a blog with laravel 5.4, It's october 2018 and I have a job (developer in an organisation) It all happened because of you Alex, I thank you for helping the guys like me out in being a laravel developer, You helped me change my life and get a job, you are actually changing lives sir, Thank you so much for this
This is a great tut. To solve newer versions (5.6 tested) with missing "function unauthenticated" in the "Handler", just create the function. Works fine!
Thank You so much.. I using laravel 7x, and i also thanks for others who commenting suggestions for updated laravel.. Appreciated :)
Awesome guide. In the RedirectIfAuthenticated middleware, I actually did the IF statement first, then the SWITCH statement inside the IF. Thanks, this is what I needed.
Awesome tutorial and very well explained as usual.
I have a request for a you. Can you please make a series on User Role/Authorization app in Laravel? that would be really helpful.
yes it will be great
yes it wd be nice
The best tutorial on laravel multi-authentication. Previously I relied on Sentinel package for this. But, as you said, had problems when migration to the next laravel version.Thanks a million!
I saw that some people are trying to use this with new laravel version (5.5 and 5.6) without success, I got it doing this:
1 - Add to header of "Handler.php" the following:
use Illuminate\Auth\AuthenticationException;
2 - At the end of file, add the following method:
protected function unauthenticated($request, AuthenticationException $exception)
{
if($request->expectsJson()){
return response()->json(['message' => $exception->getMessage()], 401);
}
$guard = array_get($exception->guards(), 0);
switch ($guard){
case 'admin':
$login = 'admin.login';
break;
default:
$login = 'login';
break;
}
return redirect()->guest(route($login));
}
It's working for me.
thanks alot mate for your comment. it definetly helped alot, but still i can get to user login view while admin is already logged in, which allow my user to get logged in as well, thus i can have both admin and user logged in at same time. Do you have any idea why is this happening!
I thought about first point, but you save me in the second - in the if statement. Thank you my man!
el papu de los papus gracias men
I am on Laravel 5.7 and can't get this to work. If I dd($exception), I get the following:
AuthenticationException {#499 ▼
#guards: array:1 [▼
0 => null
]
#redirectTo: "127.0.0.1:8000/login"
#message: "Unauthenticated."
#code: 0
#file: "C:\Users\Spare\OneDrive\Werner\Sites\multiauth\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authenticate.php"
#line: 67
trace: {▶}
}
I am logged in with my admin user. I have tried restarting the server, but no success.
Obrigado senhor Eduardo Cintra.
Great, this is just awesome! Great Tutorials...... clear and complete explanation for most of the things. Just a quick suggestion for viewers who are using laravel 6.0 (as well as me) is that you may face problems with handler.php. No worries, just follow stackoverflow instantly to solve and understand them
Can you tell me the solution?
sure, tell me where you are facing the problem
@@FarhanKhan-cp2kw I'm having problem with redirecting the unauthenticated users. . . Can you give me the code for that? I'm using laravel 6.
Nice! Learning great stuff from you as a Laravel noob. You make the best Laravel tutorials, great explanations. Can't wait for the advanced cms series to come out.
It seems like you're a real teacher, like professionally
Hey Man, thank you so much to put these together. Clear and nice tutorial! (Crazy to see some commenter just complaining.)
Excellent! little changes needed in laravel 6 , described below.
I loved the way every thing has been instructed. Just followed step by step and I am done. Thanks
For anyone doing this tutorial in Laravel 5.5 or newer, you'll need to pull the exception from the framework into your project's exception hander as explained here:
stackoverflow.com/questions/45340855/laravel-5-5-change-unauthenticated-login-redirect-url
Great tutorial Alex, love your work mate. :-)
Thank you very much! You're a good person
For Laravel 6+ and if you only have 2 type of users you can use the following:
public function render($request, Exception $exception)
{
if($exception instanceof AuthenticationException){
$guard = Arr::get($exception->guards(), 0);
return redirect(route( ($guard == 'seller')?'seller.login':'login' ));
}
return parent::render($request, $exception);
}
include this in the top:
use Illuminate\Auth\AuthenticationException;
use Illuminate\Support\Arr;
Awesome tutorial!! Learnt a lot really. Btw the link for Part 1 and 2 are the same in the description. Thank you for sharing your knowledge with us!
Awesome tutorial, waiting for part 4.
Great tutorials Alex, thank you very much!!
Did you notice that:
- if you login as user in multiauth.dev/admin/login you don't get "These credentials do not match our records."
- if you login as admin in multiauth.dev/login you correctly get "These credentials do not match our records."
U can use session object to display
session()->flash('message', 'These credentials do not match our records.');
then in blade template check if session exists
@if($flash = session('message'))
{{ $flash }}
@endif
That was pretty confusing at the beginning, but you made it clear! Thank you very much and congrats for the amazing tutorial!
still waiting for the final slice of multi auth ... grt work bytheway ...loved it and enjoyed a ton
I owe a lot for you , I am eagerly waiting for what coming ... thanks ton .
thank you so much Alexander . you saved me. I wish you all the best
You are amazing man, you remind me of jeffrey way. You two are my favorite laravel teachers
For Laravel 6. + , you can do a little bit of change:
use Illuminate\Auth\AuthenticationException;
use Illuminate\Support\Arr;
public function render($request, Exception $exception)
{
if($exception instanceof AuthenticationException){
$guard = Arr::get($exception->guards(), 0);
switch($guard){
case 'admin':
return redirect(route('admin.login'));
break;
default:
return redirect(route('login'));
break;
}
}
return parent::render($request, $exception);
}
Great tutorials!
Laravel 8 does not writes this "unauthenticated" function for us, but we can add it ourselves, works!
This is my Handler.php file
best tutorial for laravel multi authentication !!! cheers!!!
Great tutorial! However, there is one major problem: when you logout of Admin, L5 sends you to the hard coded default of "/" which will refer to the users login, not the Admin login. This is going to be hugely confusing for Admins who log out, then wish to log back in, and the system will advise them that their username / password is incorrect.
Ideally, you need to have a single login page under "/" for both users and Admins, and so in this way it will not matter if L5 sends you back to that. I think this could be achievable if when an admin / user logs in, you check the users DB table where you have a column denoting if they are an Admin or user. If the user is an Admin, then you will be sent via the admin guard to the Admin login controller. This way you will still have admin and user guards protecting their own pages, and a single login page.
finally after searching and trying a lot of solution I found this amazing tutorial I would like to make 1000 like on this video thank you
Hey man, I really appreciate what you did here, it helped me a lot, thank you.
Excellent tutorial !! just waiting for Part-4.
Thank you so much for your videos, I really feel like I'm learning and not just guessing whats going on without an explication. greetings from Chile ;D
Waiting for the updated version of this :)
Awesome very straightforward. Thanks!
Hey man, great tutorial. I had a question regarding the nav blade for admin. There is a line where it shows the logged in user Auth::user()->email. I am confused as to how to use admin()->email.. am I missing something?
Hi Alex,
Thnx for the awesome vids! I really like how you explain things thoroughly and clear.
I wanted to ask you when you are planning to add the last video in this series? I really want to learn about the 'Forgot your password' part.
Awesome... very well xplained.. show u working demo soon on 5.8
Just one word for you "Awesome" ..... Keep it up man :)
This tutorial is simply GREAT! :-)
Just a couple of things, please...
1. In the RedirectifAuthenticated middleware, can I replace your code with this? (the "switch" is inside the "if" so I don't repeat the "if (Auth::guard($guard)->check())" part)
if (Auth::guard($guard)->check()) {
switch ($guard) {
case 'admin':
return redirect(route('admin.dashboard'));
break;
default:
return redirect(route('home'));
}
}
seems that it work, but is there any side effect?
2. Can I "oversimplify" all with the following code? And again, are there some side effects?
if (Auth::guard($guard)->check()) {
return redirect()->back();
}
In this way I'm always redirected to the previous page, that I know for sure it works (because I'm coming from there)
Thanks a lot, again :-)
gr8 thanks a tons loves it love it.. nicely explained thanks a ton keep up the goooood work alex
Hello Alex, a question how can I make admins and users login in a single login both on different models?
for those newbie or beginner like me that tried this on laravel 5.5.
to make it work try to see this link below
stackoverflow.com/questions/45340855/laravel-5-5-change-unauthenticated-login-redirect-url
thank you, my friend :D
Ahh i've just spent 1 hour instead of scrolling down :D THX
But how do you write the switch case in this?
what switch case ? switch case for what dude ? please have specific example
Thanks!!! Your comment really helped me
if you want to acces array helpers you need to run : composer require laravel/helpers
then you can use array_get()
great
in latest version array_get() is deprecated use Arr::get()
@@GursewakSingh-gx6ev i have a problem.. in laravel 7 i cant find exception handler but authenticate middleware is present and the function doing returning the user to the respective page only takes the request and i cant find how to pull guard from the request... im not sure if it even exists
@@norbertseho Oh okay I understand your problem, so much have changed since this video was uploaded. Drop me a message on linkedIn we will discuss it over there. www.linkedin.com/in/gursewak-singh-ai-2019/
wow,, awesome tutorial as usual,,
seems you have huge knowledge in laravel
I really loves your laravel series it helps me a lot, could you come up with series explaining untouched part of laravel like containers, event, listeners, providers, gates with a project?
Great Tutorial!! THANK YOU SO MUCH From INDONESIA~
great job Alex. still waiting for the fourth video in this series
For Laravel 6 go to app/Exeptions/Handler.php
================================================
public function render($request, Exception $exception)
{
if($exception instanceof AuthenticationException){
$guard = Arr::get($exception->guards(), 0);
switch($guard){
case 'admin':
return redirect(route('admin.login'));
break;
default:
return redirect(route('login'));
break;
}
}
return parent::render($request, $exception);
}
}
=================================================
Also dont forget to include at the top of the page:
use Arr;
use Illuminate\Auth\AuthenticationException;
Thanks.. you saved me a ton of time!!
Cool video but honestly a prefer to manage this case as a role case and manage everything as users permissions roles to give acces and view different ui with something like spattie permissions package its well tested and its code works with guards as your code does so it guarantee compatibilty and not remake the wheel but thanks this series is very educational thanks
Hello Alex,
First of all I would like to thank you for all the effort you are doing with all the lessons.
I want to ask you a favour if you may please. Could you make video lessons about making an ACL or Role Auth System? Like when you want to create a website where Admins have multi types as you said.
Thanks :)
this tutorial is great. I would like to ask if there is a logout for both admin and casual users? for example if the admin and casual user are both logged in, if the admin click the logout, is casual user still logged in? Thank you so much for this tutorial
+
Great tutorial bro. Waiting for your next one.
You can do like this in 5.5 or above .
Just include these three lines below namespace
use Request;
use Illuminate\Auth\AuthenticationException;
use Response;
After that, Upgrade this function
public function render($request, Exception $exception)
{
$class = get_class($exception);
switch($class) {
case 'Illuminate\Auth\AuthenticationException':
$guard = array_get($exception->guards(), 0);
switch ($guard) {
case 'admin':
$login = 'admin.login';
break;
default:
$login = 'login';
break;
}
return redirect()->route($login);
}
return parent::render($request, $exception);
}
Thank you. Awesome tutorial.
Shorthand versions., this works for me in 5.5:
For the auth middleware...
$login = (array_get($exception->guards(), 0) == 'admin') ? 'admin.login' : 'login';
return redirect()->guest(route($login));
For the guest middleware...
$home = ($guard == 'admin') ? 'admin.dashboard' : 'home';
return redirect(route($home));
In truth it is an excellent tutorial and you are a very good teacher, you helped me a lot. Thank you very much ... if possible I would like you to make a notifications tutorial in laravel
hello Alex
can you post a video that show us how we manage the users authentification when he forget his password with the server SMTP ?
When I use all things which you provide in this video all i change but when i going to login in error come in verifyCsrfToken.php (trying to get property 'headers' of non-object; in laravel 5.5 /admin/login
Its work. Awesome tutorial
11:00 - it's possible that guard array is empty.
14:00 - it's possible that $guars is null.
Both scenarios happened to me if the guard was default. It wasn't "web" but null. Your code is working because you use "default" in switch statement. It could stop working if you did "case 'web':" instead.
Laravel 5.5 Exceptional Handler is very much different than this one. Please suggest where to make these changes
just write the whole unauthenticated method in Handler.php. that's what "overriding" is
Awesome tutorial, thanks!
Hi Alex,
Lots of people are waiting for part 4 here. Any idea when?
Hi Alex,
Any news on this please >> "Part 4: Forgot My Password Functionality across Multiple User Types
Coming Soon."
Thank you Very very much sir!!!!!!
hi, thanks so much for this series :D
Nice video , waiting for Part # 4 :)
Thank you for this tutorial, really this video is very helpful :)
thanks for this serie
In Laravel 5.5 Handler.php has been changed. So how can we implement this functionality?
Do I need to change in RedirectIfAuthenticated Class ? check in handle function and redirect accordingly ?.
Going to be working on an updated video of this soon. I have been busy with other projects, but I am coming back to update it to 5.5 with the new handler methods.
DevMarketer Thank u. Waiting for the updated video
Please do this
stackoverflow.com/a/45994994 hi, i tried this workaround on stackoverflow and it worked for me. perhaps you can try it as well until Alex post the L5.5 update for this video. Cheers.
Do this changes in App\Exceptions\Handler.php...
use Illuminate\Auth\AuthenticationException;
/**
* Convert an authentication exception into a response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
* @return \Illuminate\Http\Response
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
$guard = array_get($exception->guards(), 0);
if ($guard === 'admin') {
return redirect()->guest(route('admin.login'));
}
return redirect()->guest(route('login'));
}
Good tutorial :D
but still confused, about Which file Laravel detected if the guard is admin or it was user ?.
so that If I make more Auth example I had 5 User Level, such as "super-admin", "admin", "manager", "user", "user2"
I can modified it.
Should I make more prefix on route (web.php) ?
or what.
thanks
thank you so much , awesome tutorial.
In 5.8 the Exception handler function unauthenticated has been moved to the vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php
So in the app/Exceptions/handler.php the function doesn't exist.
Al you have to do is add it to override the base function in the vendor directory.
Remember to add the use statements
Hi Curtis, i just wanted to say that i already created something very similar but kind of another way around....i have created two different register forms and there in register controller i set the user if he is an admin or a regular user (depends on which register form they have used) so the login form is the same for any kind of users. And the users table is also where the admins are stored. I would like you to give me a feedback on this...want to hear your opinion on this. Is it good or bad practice?
In case if anyone don't want the logged in user access the login form of admin, go to your AdminLoginController then change the showLoginForm function to this
public function showLoginForm()
{
if(Auth::check()){
return redirect('/home');
}
else {
return view('auth.admin-login');
}
}
Great tutorial. Hello alex. I can login on admin page. Also, validation is okay.
But, for the Auth::guard('admin')->attempt, it does not display `Email and password mismtached` error. Please help. thanks
Thank you vary much for excellent lessons! What about Part 4?
Hi, thank you for all awesome series. I am trying this with laravel 5.5.*. And in this video i override the method unauthenticated and its works fine. is this right way to do unauthenticated redirects (in 5.5.*)??? if any other way to do it i am glad to know that.
Fantastic tutorial
Greaat tutorial man!! thank u so much , that's helped me a lot
Why Auth::check();
not working.. Always return false, even though I have login as someone from the above ?
I'm implemented this method (part 1 - part 3) to Laravel 5.6
Thx
me too user can check but admin/login not working go to admin/login pasge
app\Exceptions\Handler.php has shrinken ALOT since this video . Any tip how do this?
Its true that Handler.php is shrinken but there is no reduction in its functionality, meaning you can always override the functions you were using before. Like *unauthenticated* is not there anymore but you can define it and use it to your will !
I think that after clicking logout, it should return to the login page judged by user type. That is, admin returns to /admin/login and user returns to /home or /login page. And how to do this?
did you solve it?
Awesome tutorial and very well explained as usual. I have a request for a you. can you explain about after logout redirection ?
Alex, how did you learn these in the first place? Laravel is like a black box to me, without your videos, I have no idea how to dig in these files.
laravel 5.6:
the app\Exceptions\Handler.php has been shortened but the "unauthenticated" method is still available in the "Illuminate\Foundation\Exceptions\Handler".
as stated in laravel.com/docs/5.6/authentication#protecting-routes under "Redirecting Unauthenticated Users" you have to add the namespace "use Illuminate\Auth\AuthenticationException;" to your Handler.php and then create the "unauthenticated" method. The documentation shows the redirect with a ternary operator. I recommend you comment that one out and replace it with the code from alex' video.
That's it.
Excellent video
Hey guys, if you download the source code on Github don't forget to add this code to AppServiceProvider.php:
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
// Do it before running the migrations.
nice Tutorial.
BTW this sound Horror, little bit Scared, 19:00
Alex 11:30 admin.login didn't work for me i had to add route('admin.login') instead and I made the switch statement much simpler: $login = $guard == 'admin' ? route('admin.login') : 'login';
where did you put this switch statement??
It didnt work for me as well
Didnt work for me as well. I dont understand why. Which line attempt to block that? Any suggestion to fix it?
As far as I understand, when I click "logout" on /home or on /admin I log out of both my accounts (if I was logged in as user and as admin at the same time)?
awesome tutorial
Hi your videos are helpful, but I can't find your written instruction. Where i can find written instructions??
can you show how you handle multiple roles and permission?
Could you please finish and project at the time? Why don't you finish the blog website in Laravel 5.4 before you start creating other series?
I love your tutorial. I have it completely working. My question is in my user table I created role. I have three different types of users in that table. I want to be able to have user1 redirect to user1.blade.php user2 to redirect to user2.blade.php and user3 to redirect to user3.blade.php upon logging in based on that field called role. How difficult would it be to modify this tutorial to make it do that?
Hi,
Could you please help me..
I have installed Laravel 5.5 and try to do what you do in this tutorial series. But after doing all things, it doesn't redirect to the intended page when I use construct method with 'auth:admin' middleware in AdminConroller. What was happend there..?
thanks.
Alex have you done a tutorial on setting up a role based auth?
if you are using version 5.5 or 5.6 use this instead in your app/Exceptions/Handler.php
public function render($request, Exception $exception)
{
//gets the class exception
$class = get_class($exception);
switch($class) {
case 'Illuminate\Auth\AuthenticationException':
$guard = array_get($exception->guards(), 0);
switch ($guard) {
case 'admin':
$login = 'admin.login';
break;
default:
$login = 'login';
break;
}
return redirect()->route($login);
}
return parent::render($request, $exception);
}
I Do love you and thank you guy
hello ,i have a question with "protected function unauthenticated" on laravel 5.8 link address have change to vendor/foundation/exceptions/handler.php , if i do rewrite it every time i use composer update will replace the file any idea or anyway can solve this problem?