How to create or use multi Auth in laravel 5.2

Solution :

Step1: Now open config/auth.php and make the following changes.
Step2 :

‘guards’ => [
‘web’ => [
‘driver’ => ‘session’,
‘provider’ => ‘users’,
],

‘admin’ => [
‘driver’ => ‘session’,
‘provider’ => ‘admin’,
],
],

‘providers’ => [
‘users’ => [
‘driver’ => ‘eloquent’,
‘model’ => App\User::class,
],

‘admin’ => [
‘driver’ => ‘eloquent’,
‘model’ => App\Admin::class,
],
],

‘passwords’ => [
‘users’ => [
‘provider’ => ‘users’,
’email’ => ‘auth.emails.password’,
‘table’ => ‘password_resets’,
‘expire’ => 60,
],
‘admin’ => [
‘provider’ => ‘admin’,
’email’ => ‘auth.emails.password’,
‘table’ => ‘password_resets’,
‘expire’ => 60,
],
],

Step3: Create a new Middleware RedirectIfNotAdmin
step4 : Now open RedirectIfNotAdmin file and add following code

<?php
namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

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(‘admin/login’);
}

return $next($request);
}
}

step5: open Kernel.php // path : app/Http/kernal.php
Step6: Add following line at

protected $routeMiddleware = [
‘admin’ => \App\Http\Middleware\RedirectIfNotAdmin::class,
];

Step7 : To check login use following in your controller

public function __construct(){
$this->middleware(‘admin’);
}

Step8: Create custom Login controller

namespace App\Http\Controllers;

use Auth, Input;
use App\User;
use App\Admin;

class LoginController extends Controller
{

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(‘[email protected]’);
} else {
echo ‘Error’;
}
} else {
return view(‘admin.login’);
}
}

}

Step9: Now your custom admin is working. default is Auth::attempt() and it will always check from user table. but if you want to check Auth from Admin table then use above procedure and to check user is login or not use

Auth::guard(‘admin’)->check() // it will return true or false, in stead Auth::check();

Thanks 🙂

50 comments
  1. I’d like to thank you for the efforts you have put in penning this blog.
    I really hope to view the same high-grade content by you in the
    future as well. In fact, your creative writing abilities
    has encouraged me to get my own blog now 😉 fast burn extreme

  2. I do not know whether it’s just me or if perhaps everyone else encountering problems with your website.
    It looks like some of the written text on your content are running off the
    screen. Can somebody else please comment and let me know if
    this is happening to them as well? This might be a issue with my browser because I’ve had this happen previously.
    Thanks nutrigo lab burner

  3. Hello! I could have sworn I’ve been to this site before but
    after going through some of the articles I realized it’s
    new to me. Nonetheless, I’m definitely happy I stumbled upon it
    and I’ll be book-marking it and checking back frequently!
    collagen select

  4. Hello! I could have sworn I’ve been to your blog before but
    after looking at many of the articles I realized it’s new to me.
    Anyways, I’m certainly happy I stumbled upon it and I’ll be
    bookmarking it and checking back regularly!

  5. Hiya! Quick question that’s totally off topic.

    Do you know how to make your site mobile friendly?
    My website looks weird when browsing from my apple iphone.
    I’m trying to find a template or plugin that might be able to resolve this issue.
    If you have any suggestions, please share. Appreciate
    it! Musculin Active

  6. I believe everything typed was very reasonable.
    But, think on this, suppose you added a little information? I ain’t saying your content
    isn’t solid., however what if you added a post title that makes people desire more?
    I mean How to create or use multi Auth in laravel
    5.2 – Programmer Lab is kinda plain. You could peek at Yahoo’s front
    page and see how they create post headlines to grab
    people to open the links. You might try adding a video or a pic
    or two to get people excited about everything’ve
    written. Just my opinion, it could make your posts a little bit more interesting.
    http://lacratex.free.fr/site/index.php?file=Members&op=detail&autor=zuvore

  7. Good day I am so happy I found your web site, I really found you by error, while I was looking
    on Askjeeve for something else, Regardless I am here now and would just like to say cheers
    for a marvelous post and a all round entertaining blog (I also love the theme/design),
    I don’t have time to read through it all at the minute but
    I have saved it and also added your RSS feeds, so when I have time I will be
    back to read much more, Please do keep up the excellent b.
    http://hi-careers.com/author/genecoweyul/

Add Comment

Required fields are marked *. Your email address will not be published.