How to use multi Auth in laravel 5.2
You need two tables users
and admins
Run command following command to create built in auth
php artisan make:auth
Two models Users(Already exist) and Admin
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable{ }
Can Anyone Explain Laravel 5.2 Multi Auth with Example
After lots of digging and lots of questions & answers I have finally managed to work Laravel 5.2 Multi Auth with two table, So I'm writing Answer of my own Question.
How to implement Multi Auth in Laravel 5.2
As Mentioned above.
Two table admin
and users
Laravel 5.2 has a new artisan
command.
php artisan make:auth
it will generate basic login/register route
, view
and controller
for user
table.
Make a admin
table as users
table for simplicity.
Controller For Adminapp/Http/Controllers/AdminAuth/AuthController
app/Http/Controllers/AdminAuth/PasswordController
(note: I just copied these files from app/Http/Controllers/Auth/AuthController
here)
config/auth.php
//Authenticating guards
'guards' => [
'user' =>[
'driver' => 'session',
'provider' => 'user',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
],
//User Providers
'providers' => [
'user' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
]
],
//Resetting Password
'passwords' => [
'clients' => [
'provider' => 'client',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admin',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
],
route.php
Route::group(['middleware' => ['web']], function () {
//Login Routes...
Route::get('/admin/login','AdminAuth\AuthController@showLoginForm');
Route::post('/admin/login','AdminAuth\AuthController@login');
Route::get('/admin/logout','AdminAuth\AuthController@logout');
// Registration Routes...
Route::get('admin/register', 'AdminAuth\AuthController@showRegistrationForm');
Route::post('admin/register', 'AdminAuth\AuthController@register');
Route::get('/admin', 'AdminController@index');
});
AdminAuth/AuthController.php
Add two methods and specify $redirectTo
and $guard
protected $redirectTo = '/admin';
protected $guard = 'admin';
public function showLoginForm()
{
if (view()->exists('auth.authenticate')) {
return view('auth.authenticate');
}
return view('admin.auth.login');
}
public function showRegistrationForm()
{
return view('admin.auth.register');
}
it will help you to open another login form for admin
creating a middleware for admin
class RedirectIfNotAdmin
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = 'admin')
{
if (!Auth::guard($guard)->check()) {
return redirect('/');
}
return $next($request);
}
}
register middleware in kernel.php
protected $routeMiddleware = [
'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class,
];
use this middleware in AdminController
e.g.,
}
public function index(){
return view('admin.dashboard');
}
}
That's all needed to make it working and also to get json of authenticated admin use
`Auth::guard('admin')->user()`
**Edit - 1**
We can access authenticated user directly using
`Auth::user()`
but if you have two authentication table then you have to use
Auth::guard('guard_name')->user()
for logout
Auth::guard('guard_name')->user()->logout()
for authenticated user json
Auth::guard('guard_name')->user()
##Edit 2
Now you can download Laravel 5.2 Multiauth implemented Project http://imrealashu.in/code/laravel/multi-auth-with-laravel-5-2-2/
I am trying to implement laravel 5.2 multiple authentication for user and admin . But authentication user provider[] is not defined error is given
I Think this will be helpful.
http://blog.sarav.co/multiple-authentication-in-laravel/
Why don't you give it a try using the built in user model and extending admin with it.
How to Create Multi Auth in Laravel 5.2
And what does this code mean Auth::guard('admin')->user() ?
In simple word, Auth::guard('admin')->user() is used when you need to get details of logged in user. But, in multi auth system, there can be two logged in users (admin/client). So you need to specify that which user you want to get. So by guard('admin'), you tell to get user from admin table.
Where must i type that code?
As from answer, you can understand that where must you use it. But still I can explain with example. Suppose there are multiple admins. Each can approve users request (like post/comments etc). So when an admin approve any request, then to insert id of that admin into approved_by column of post, you must use this line.
Multi auth for laravel 5.2 use middleware
Because on Laravel 5.2 the web
middleware is automatically applied, your routes in routes.php
are grouped with prefix App\Http\Controllers
and web
middleware. You can find this definition on mapWebRoutes()
method of the RouteServiceProdiver.php
.
Laravel 5.2 Multi Auth not loggin in
I found a github repo that solved my problem.
https://github.com/gregoryduckworth/laravel-multi-auth
Related Topics
Checking If Process Still Running
Refresh a Table with Jquery/Ajax Every 5 Seconds
Woocommerce Custom Checkout Field to Add Fee to Order Ajax
Downloading a File with a Different Name to the Stored Name
PHP Function That Creates a Nested Ul Li
MySQL Can't Perform More Than 1 Query at a Time
Cast the Current Object ($This) to a Descendent Class
Bad Request. Connecting to Sites via Curl on Host and System
Highlight Keywords in a Paragraph
How to Convert Pascalcase to Snake_Case
Laravel Advanced Wheres How to Pass Variable into Function
Symfony2 Routing - Route Subdomains
Why Are PHP Function Calls *So* Expensive
Sending Push Notifications to Multiple Android Devices Using Gcm
Downloading Attachments to Directory with Imap in PHP, Randomly Works