Sometimes, we'd like to separate users and admins in different 2 tables.
I think it is a good practice.
So I was looking if that is possible in Laravel 5.
After a quick search, I found many people having the same question as mine.
I found some answers, but I don't think any of them is good enough.
Therefore, I spent some time digging into the source code and finally find a way to achieve this.
I have created a laravel package where you can handle multiple authentication.
Step 1 : Composer require
Firstly, composer require the multiauth package
Step 2 : Replacing default auth service provider
Replace
with
in your config/app.php file
Step 3 : Modify auth.php
Modify your config/auth.php file to something like this
Thats it! Now you can try multiple authentication by passing the user as first parameter. For example
For more detailed documentation
http://sarav.co/blog/multiple-authentication-in-laravel/
http://sarav.co/blog/multiple-authentication-in-laravel-continued/
there are good packages for handling multi auth. check these links:
sboo multiauth package
mohamednagy/multiauth
Before reading the following, you are supposed to have basic knowledge on ServiceProvider, Facade and IoC in Laravel 5. Here we go.
According to the doc of Laravel, you could find the Facade 'Auth' is refering to the
Illuminate\Auth\AuthManager
, which has a magic __call(). You could see the major function is not in AuthManager, but inIlluminate\Auth\Guard
Guard has a Provider. This provider has a
$model
property, according to which theEloquentUserProvider
would create this model by"new $model"
. These are all we need to know. Here goes the code.1.We need to create a
AdminAuthServiceProvider
.2.Facade:
3. add the alias to Kernel:
Hope this could be helpful.