-
-
Save oluwajubelo1/ffb4e99b4c2e3898cf6a851fa24b2af3 to your computer and use it in GitHub Desktop.
Multi Auth in Laravel 5.6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Notifications\Notifiable; | |
use Illuminate\Foundation\Auth\User as Authenticatable; | |
class Admin extends Authenticatable | |
{ | |
use Notifiable; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array | |
*/ | |
protected $fillable = [ | |
'name', 'email', 'password', | |
]; | |
/** | |
* The attributes that should be hidden for arrays. | |
* | |
* @var array | |
*/ | |
protected $hidden = [ | |
'password', 'remember_token', | |
]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Faker\Generator as Faker; | |
/* | |
|-------------------------------------------------------------------------- | |
| Model Factories | |
|-------------------------------------------------------------------------- | |
| | |
| This directory should contain each of the model factory definitions for | |
| your application. Factories provide a convenient way to generate new | |
| model instances for testing / seeding your application's database. | |
| | |
*/ | |
$factory->define(App\Admin::class, function (Faker $faker) { | |
return [ | |
'name' => $faker->name, | |
'email' => $faker->unique()->safeEmail, | |
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret | |
'remember_token' => str_random(10), | |
]; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Seeder; | |
class AdminsTableSeeder extends Seeder | |
{ | |
/** | |
* Run the database seeds. | |
* | |
* @return void | |
*/ | |
public function run() | |
{ | |
factory(\App\Admin::class)->create(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="{{ app()->getLocale() }}"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<!-- CSRF Token --> | |
<meta name="csrf-token" content="{{ csrf_token() }}"> | |
<title>{{ config('app.name', 'Laravel') }}</title> | |
<!-- Styles --> | |
<link href="{{ asset('css/app.css') }}" rel="stylesheet"> | |
<style> | |
body{ | |
background-color: tomato; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="app"> | |
<nav class="navbar navbar-expand-md navbar-light navbar-laravel"> | |
<div class="container"> | |
<a class="navbar-brand" href="{{ url('/admin') }}"> | |
{{ config('app.name', 'Laravel') }} | |
</a> | |
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> | |
<span class="navbar-toggler-icon"></span> | |
</button> | |
<div class="collapse navbar-collapse" id="navbarSupportedContent"> | |
<!-- Left Side Of Navbar --> | |
<ul class="navbar-nav mr-auto"> | |
</ul> | |
<!-- Right Side Of Navbar --> | |
<ul class="navbar-nav ml-auto"> | |
<!-- Authentication Links --> | |
@guest('admin') | |
<li><a class="nav-link" href="{{ route('admin.login') }}">@lang('Login')</a></li> | |
@else | |
<li class="nav-item dropdown"> | |
<a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> | |
{{ Auth::user()->name }} <span class="caret"></span> | |
</a> | |
<div class="dropdown-menu" aria-labelledby="navbarDropdown"> | |
<a class="dropdown-item" href="{{ route('admin.logout') }}" | |
onclick="event.preventDefault(); | |
document.getElementById('logout-form').submit();"> | |
@lang('Logout') | |
</a> | |
<form id="logout-form" action="{{ route('admin.logout') }}" method="POST" style="display: none;"> | |
@csrf | |
</form> | |
</div> | |
</li> | |
@endguest | |
</ul> | |
</div> | |
</div> | |
</nav> | |
<main class="py-4"> | |
@yield('content') | |
</main> | |
</div> | |
<!-- Scripts --> | |
<script src="{{ asset('js/app.js') }}"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
/* | |
|-------------------------------------------------------------------------- | |
| Authentication Defaults | |
|-------------------------------------------------------------------------- | |
| | |
| This option controls the default authentication "guard" and password | |
| reset options for your application. You may change these defaults | |
| as required, but they're a perfect start for most applications. | |
| | |
*/ | |
'defaults' => [ | |
'guard' => 'web', | |
'passwords' => 'users', | |
], | |
/* | |
|-------------------------------------------------------------------------- | |
| Authentication Guards | |
|-------------------------------------------------------------------------- | |
| | |
| Next, you may define every authentication guard for your application. | |
| Of course, a great default configuration has been defined for you | |
| here which uses session storage and the Eloquent user provider. | |
| | |
| All authentication drivers have a user provider. This defines how the | |
| users are actually retrieved out of your database or other storage | |
| mechanisms used by this application to persist your user's data. | |
| | |
| Supported: "session", "token" | |
| | |
*/ | |
'guards' => [ | |
'web' => [ | |
'driver' => 'session', | |
'provider' => 'users', | |
], | |
'api' => [ | |
'driver' => 'token', | |
'provider' => 'users', | |
], | |
'admin' => [ | |
'driver' => 'session', | |
'provider' => 'admins', | |
], | |
], | |
/* | |
|-------------------------------------------------------------------------- | |
| User Providers | |
|-------------------------------------------------------------------------- | |
| | |
| All authentication drivers have a user provider. This defines how the | |
| users are actually retrieved out of your database or other storage | |
| mechanisms used by this application to persist your user's data. | |
| | |
| If you have multiple user tables or models you may configure multiple | |
| sources which represent each model / table. These sources may then | |
| be assigned to any extra authentication guards you have defined. | |
| | |
| Supported: "database", "eloquent" | |
| | |
*/ | |
'providers' => [ | |
'users' => [ | |
'driver' => 'eloquent', | |
'model' => App\User::class, | |
], | |
'admins' => [ | |
'driver' => 'eloquent', | |
'model' => App\Admin::class, | |
], | |
// 'users' => [ | |
// 'driver' => 'database', | |
// 'table' => 'users', | |
// ], | |
], | |
/* | |
|-------------------------------------------------------------------------- | |
| Resetting Passwords | |
|-------------------------------------------------------------------------- | |
| | |
| You may specify multiple password reset configurations if you have more | |
| than one user table or model in the application and you want to have | |
| separate password reset settings based on the specific user types. | |
| | |
| The expire time is the number of minutes that the reset token should be | |
| considered valid. This security feature keeps tokens short-lived so | |
| they have less time to be guessed. You may change this as needed. | |
| | |
*/ | |
'passwords' => [ | |
'users' => [ | |
'provider' => 'users', | |
'table' => 'password_resets', | |
'expire' => 60, | |
], | |
'admins' => [ | |
'provider' => 'admins', | |
'table' => 'password_resets', | |
'expire' => 60, | |
], | |
], | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Seeder; | |
class DatabaseSeeder extends Seeder | |
{ | |
/** | |
* Seed the application's database. | |
* | |
* @return void | |
*/ | |
public function run() | |
{ | |
// $this->call(UsersTableSeeder::class); | |
$this->call(AdminsTableSeeder::class); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Exceptions; | |
use Exception; | |
use Illuminate\Auth\AuthenticationException; | |
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; | |
class Handler extends ExceptionHandler | |
{ | |
/** | |
* A list of the exception types that are not reported. | |
* | |
* @var array | |
*/ | |
protected $dontReport = [ | |
// | |
]; | |
/** | |
* A list of the inputs that are never flashed for validation exceptions. | |
* | |
* @var array | |
*/ | |
protected $dontFlash = [ | |
'password', | |
'password_confirmation', | |
]; | |
/** | |
* Report or log an exception. | |
* | |
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. | |
* | |
* @param \Exception $exception | |
* @return void | |
*/ | |
public function report(Exception $exception) | |
{ | |
parent::report($exception); | |
} | |
/** | |
* Render an exception into an HTTP response. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Exception $exception | |
* @return \Illuminate\Http\Response | |
*/ | |
public function render($request, Exception $exception) | |
{ | |
return parent::render($request, $exception); | |
} | |
/** | |
* Convert an authentication exception into a response. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Illuminate\Auth\AuthenticationException $exception | |
* @return \Illuminate\Http\Response | |
*/ | |
protected function unauthenticated($request, AuthenticationException $exception) | |
{ | |
if ($request->expectsJson()) { | |
return response()->json(['error' => 'Unauthenticated.'], 401); | |
} | |
if (in_array('admin', $exception->guards())) { | |
return redirect()->guest(route('admin.login')); | |
} | |
return redirect()->guest(route('login')); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Admin\Auth; | |
use App\Http\Controllers\Controller; | |
use Illuminate\Http\Request; | |
class HomeController extends Controller | |
{ | |
/** | |
* Create a new controller instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
$this->middleware('auth:admin'); | |
} | |
/** | |
* Show the application dashboard. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function index() | |
{ | |
return view('admin.home'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@extends('admin.layouts.app') | |
@section('content') | |
<div class="container"> | |
<div class="row justify-content-center"> | |
<div class="col-md-8"> | |
<div class="card"> | |
<div class="card-header">@lang('Login')</div> | |
<div class="card-body"> | |
<form method="POST" action="{{ route('admin.login') }}"> | |
@csrf | |
<div class="form-group row"> | |
<label for="email" class="col-sm-4 col-form-label text-md-right">E-Mail Address</label> | |
<div class="col-md-6"> | |
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required autofocus> | |
@if ($errors->has('email')) | |
<span class="invalid-feedback"> | |
<strong>{{ $errors->first('email') }}</strong> | |
</span> | |
@endif | |
</div> | |
</div> | |
<div class="form-group row"> | |
<label for="password" class="col-md-4 col-form-label text-md-right">@lang('Password')</label> | |
<div class="col-md-6"> | |
<input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required> | |
@if ($errors->has('password')) | |
<span class="invalid-feedback"> | |
<strong>{{ $errors->first('password') }}</strong> | |
</span> | |
@endif | |
</div> | |
</div> | |
<div class="form-group row"> | |
<div class="col-md-6 offset-md-4"> | |
<div class="checkbox"> | |
<label> | |
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> @lang('Remember Me') | |
</label> | |
</div> | |
</div> | |
</div> | |
<div class="form-group row mb-0"> | |
<div class="col-md-8 offset-md-4"> | |
<button type="submit" class="btn btn-primary"> | |
@lang('Login') | |
</button> | |
</div> | |
</div> | |
</form> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
@endsection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Admin\Auth; | |
use App\Http\Controllers\Controller; | |
use Illuminate\Foundation\Auth\AuthenticatesUsers; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\Auth; | |
class LoginController extends Controller | |
{ | |
/* | |
|-------------------------------------------------------------------------- | |
| Login Controller | |
|-------------------------------------------------------------------------- | |
| | |
| This controller handles authenticating users for the application and | |
| redirecting them to your home screen. The controller uses a trait | |
| to conveniently provide its functionality to your application. | |
| | |
*/ | |
use AuthenticatesUsers; | |
/** | |
* Where to redirect users after login. | |
* | |
* @var string | |
*/ | |
protected $redirectTo = '/admin/home'; | |
/** | |
* Create a new controller instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
$this->middleware('guest:admin')->except('logout'); | |
} | |
public function showLoginForm() | |
{ | |
return view('admin.auth.login'); | |
} | |
protected function guard() | |
{ | |
return Auth::guard('admin'); | |
} | |
public function logout(Request $request) | |
{ | |
$this->guard()->logout(); | |
$request->session()->invalidate(); | |
return redirect('/admin'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Middleware; | |
use Closure; | |
use Illuminate\Support\Facades\Auth; | |
class RedirectIfAuthenticated | |
{ | |
/** | |
* 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 = null) | |
{ | |
if (Auth::guard($guard)->check()) { | |
if ($guard == 'admin') { | |
return redirect('/admin/home'); | |
} | |
return redirect('/home'); | |
} | |
return $next($request); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
|-------------------------------------------------------------------------- | |
| Web Routes | |
|-------------------------------------------------------------------------- | |
| | |
| Here is where you can register web routes for your application. These | |
| routes are loaded by the RouteServiceProvider within a group which | |
| contains the "web" middleware group. Now create something great! | |
| | |
*/ | |
Route::get('/', function () { | |
return redirect('/home'); | |
}); | |
Auth::routes(); | |
Route::get('/home', 'HomeController@index')->name('home'); | |
Route::group(['prefix' => 'admin'], function() { | |
Route::get('/', function () { | |
return redirect('/admin/home'); | |
}); | |
Route::get('login', 'Admin\Auth\LoginController@showLoginForm'); | |
Route::post('login', 'Admin\Auth\LoginController@login')->name('admin.login'); | |
Route::post('logout', 'Admin\Auth\LoginController@logout')->name('admin.logout'); | |
Route::get('home', 'Admin\Auth\HomeController@index')->name('admin.home'); | |
}); | |
Route::group(['prefix' => 'admin', 'middleware' => 'auth:admin', 'namespace' => 'Admin', 'as' => 'admin.'], function() { | |
// | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment