1

I have followed this questions asnwer Can anyone explain Laravel 5.2 Multi Auth with example

to implement multiple table authentication using admin and user table . I have done all steps stated above such as added admin as provider , guard in config/auth.php, created auth controller for admin , created a middleware for admin . But after i run my project it say authentication user provider[] is not defined . But i defined provider for admin and user in config/auth.php .I have used different login and registration form, controller for user and admin.When the user or admin logged in then i want to show user or admin name in nav bar. To do this I used auth guard as stated in laravel docs(the code in attachment of master file html) . I have stucked with this error for 6 days . I need your help eagerly . Here is my attaches config/auth.php

    return [



'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],



'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
    'user' =>[
    'driver' => 'session',
    'provider' => 'user',
    ],
    'admin' => [
        'driver' => 'session',
        'provider' => 'admin',
    ],
],



'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
        'admin' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ]

    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],



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

];

routes.php

    <?php


Route::get('/', function () {
    return view('welcome');
});

Route::auth();

Route::get('/home', 'HomeController@index');

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');

}); 

app/Http/Controllers/AdminAuth/AuthController.php

namespace App\Http\Controllers\AdminAuth;

use App\Admin;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{


    use AuthenticatesAndRegistersUsers, ThrottlesLogins;


    protected $redirectTo = '/admin';
    protected $guard = 'admin';
    //protected $redirectTo = '/';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }


    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);
    }

            protected function create(array $data)
    {
        return Admin::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
    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');
    } 
}

Admin.php

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;


class Admin extends Authenticatable
{

    protected $fillable = [
        'name', 'email', 'password',
    ];


    protected $hidden = [
        'password', 'remember_token',
    ];
}

AdminController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Contracts\View\View;

class AdminController extends Controller
{

    protected $guard =  'admin';

     public function __construct(){
        $this->middleware('admin');
    }
    public function index(){
        return view('admin.home');
    }

}

Admin middleware

<?php

namespace App\Http\Middleware;

use Closure;

class RedirectIfNotAdmin
{

    public function handle($request, Closure $next, $guard = 'admin')
    {
        if (!Auth::guard($guard)->check()) {
            return redirect('/');
        }

        return $next($request);
    }
}

Master layout for showing admin

<!-- Authentication Links -->
                    @if (Auth::guard('admin')->guest())
                        <li><a href="{{ url('/login') }}">Login</a></li>
                        <li><a href="{{ url('/register') }}">Register</a></li>
                    @else
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
                                {{ Auth::guard('admin')->user()->name }} <span class="caret"></span>
                            </a>

                            <ul class="dropdown-menu" role="menu">
                                <li><a href="{{ url('/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li>
                            </ul>
                        </li>
                    @endif
Community
  • 1
  • 1
  • 1
    Please post a more simplified description of the issue with the smallest set of files and configuration options that still show the issue you are having. Otherwise no one will sort through all of your files. – Adam B May 19 '16 at 20:05

1 Answers1

1

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.

Gayan Kavirathne
  • 1,933
  • 1
  • 15
  • 24
  • your blog link actually done the trick .Though i checked it previously . After 2nd check I understand that this is a short article . I have to do all things said their and then just do thinks for taking admin credentials and pass it to a controller for checking and logging in admin . That's it many many thanks. – avijit bhattacharjee May 20 '16 at 00:31