Today I wasted almost 3 hours to search on internet about Multi Authentication in Laravel 5.2. I have gone though laracast's post and other few forums too, I was still not able to find any solution for it. Their were packages such as sboo/multiauth, ollieread/multiauth, kbweb/multiauth etc. yet was not satisfied with their solutions, I tried to use them a lot but very soon I got stuck with bugs.
Finally after a long research I finally found out a great solution for it, i found it on a frequent website I visit for my solution of any coding problems, and the solution was really shocking for me that it required no package installation or any other kinda stuff. It's already included with Laravel 5.2.
Thanks to Hasmukh Tank a stack overflow user, he made a beautiful step by step procedure to do that.
Here are those procedures:
Refer Url : How to use multi Auth in laravel 5.2
Finally after a long research I finally found out a great solution for it, i found it on a frequent website I visit for my solution of any coding problems, and the solution was really shocking for me that it required no package installation or any other kinda stuff. It's already included with Laravel 5.2.
Thanks to Hasmukh Tank a stack overflow user, he made a beautiful step by step procedure to do that.
Here are those procedures:
Refer Url : How to use multi Auth in laravel 5.2
1. First we create two models
1) user, 2)admin
2. Update the config/auth.php file:
return [
'defaults' => [
'guard' => 'user',
'passwords' => 'user',
],
'guards' => [
'user' => [
'driver' => 'session',
'provider' => 'user',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
],
'providers' => [
'user' => [
'driver' => 'eloquent',
'model' => 'App\User',
],
'admin' => [
'driver' => 'eloquent',
'model' => 'App\Admin',
],
],
'passwords' => [
'user' => [
'provider' => 'user',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
'admin' => [
'provider' => 'admin',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
]
]
];
3. Now modify the app/Http/kernel.php file
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class
];
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class
],
'api' => [
'throttle:60,1',
],
];
4. Create LoginController and set below code in it.
Note: You have to create login pages for 'user' as well as 'admin'. And submit login form requests to appropriate controller function i.e. userLogin() or adminLogin().
namespace App\Http\Controllers;
use Auth, Input;
use App\User;
use App\Admin;
class LoginController extends Controller
{
public function userLogin(){
$input = Input::all();
if(count($input) > 0){
$auth = auth()->guard('user');
$credentials = [
'email' => $input['email'],
'password' => $input['password'],
];
if ($auth->attempt($credentials)) {
return redirect()->action('LoginController@profile');
} else {
echo 'Error';
}
} else {
return view('user.login');
}
}
public function adminLogin(){
$input = Input::all();
if(count($input) > 0){
$auth = auth()->guard('admin');
$credentials = [
'email' => $input['email'],
'password' => $input['password'],
];
if ($auth->attempt($credentials)) {
return redirect()->action('LoginController@profile');
} else {
echo 'Error';
}
} else {
return view('admin.login');
}
}
public function profile(){
if(auth()->guard('admin')->check()){
pr(auth()->guard('admin')->user()->toArray());
}
if(auth()->guard('user')->check()){
pr(auth()->guard('user')->user()->toArray());
}
}
}
Indefined index:password is coming
ReplyDeleteErrorException in EloquentUserProvider.php line 122: Undefined index: password