Skip to content

Commit

Permalink
add login admin guard
Browse files Browse the repository at this point in the history
  • Loading branch information
Hxor committed Sep 25, 2017
1 parent 3b5b278 commit 8219ce9
Show file tree
Hide file tree
Showing 19 changed files with 564 additions and 9 deletions.
33 changes: 33 additions & 0 deletions app/Exceptions/Handler.php
Expand Up @@ -4,6 +4,9 @@

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Request;
use Response;
use Illuminate\Auth\AuthenticationException;

class Handler extends ExceptionHandler
{
Expand Down Expand Up @@ -50,4 +53,34 @@ 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)
{
// return $request->expectsJson()
// ? response()->json(['message' => 'Unauthenticated.'], 401)
// : redirect()->guest(route('login'));

if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}

$guard = array_get($exception->guards(), 0);
switch ($guard) {
case 'admin':
$login = 'admin.login';
break;

default:
$login = 'login';
break;
}
return redirect()->guest(route($login));
}
}
27 changes: 27 additions & 0 deletions app/Http/Controllers/AdminController.php
@@ -0,0 +1,27 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AdminController 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');
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/RegisterController.php
Expand Up @@ -2,7 +2,7 @@

namespace App\Http\Controllers\Auth;

use App\User;
use App\Models\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
Expand Down
76 changes: 76 additions & 0 deletions app/Http/Controllers/AuthAdmin/LoginController.php
@@ -0,0 +1,76 @@
<?php

namespace App\Http\Controllers\AuthAdmin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
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 applications.
|
*/

/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';

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

/**
* Show the application's login form.
*
* @return \Illuminate\Http\Response
*/
public function showLoginForm()
{
return view('authAdmin.login');
}

/**
* Handle a login request to the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
*/
public function login(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6'
]);

$credential = [
'email' => $request->email,
'password' => $request->password
];

// Attempt to log the user in
if (Auth::guard('admin')->attempt($credential, $request->member)){
// If login succesful, then redirect to their intended location
return redirect()->intended(route('admin.home'));
}

// If Unsuccessful, then redirect back to the login with the form data
return redirect()->back()->withInput($request->only('email', 'remember'));
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/HomeController.php
Expand Up @@ -23,6 +23,6 @@ public function __construct()
*/
public function index()
{
return view('home');
return view('user.home');
}
}
14 changes: 12 additions & 2 deletions app/Http/Middleware/RedirectIfAuthenticated.php
Expand Up @@ -17,8 +17,18 @@ class RedirectIfAuthenticated
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/home');
switch ($guard){
case 'admin':
if (Auth::guard($guard)->check()) {
return redirect()->route('admin.home');
}
break;

default:
if (Auth::guard($guard)->check()) {
return redirect()->route('home');
}
break;
}

return $next($request);
Expand Down
32 changes: 32 additions & 0 deletions app/Models/Admin.php
@@ -0,0 +1,32 @@
<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Admin extends Authenticatable
{
use Notifiable;

// declare guard type
protected $guard = 'admin';

/**
* 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',
];
}
2 changes: 1 addition & 1 deletion app/User.php → app/Models/User.php
@@ -1,6 +1,6 @@
<?php

namespace App;
namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
Expand Down
22 changes: 21 additions & 1 deletion config/auth.php
Expand Up @@ -45,6 +45,16 @@
'driver' => 'token',
'provider' => 'users',
],

'admin' => [
'driver' => 'session',
'provider' => 'admins',
],

'api-admin' => [
'driver' => 'token',
'provider' => 'admins',
],
],

/*
Expand All @@ -67,7 +77,12 @@
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
'model' => App\Models\User::class,
],

'admins' => [
'driver' => 'eloquent',
'model' => App\Models\Admin::class,
],

// 'users' => [
Expand Down Expand Up @@ -97,6 +112,11 @@
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 60,
],
],

];
35 changes: 35 additions & 0 deletions database/migrations/2017_09_25_134600_create_admins_table.php
@@ -0,0 +1,35 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAdminsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('admins', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('admins');
}
}
2 changes: 1 addition & 1 deletion database/seeds/DatabaseSeeder.php
Expand Up @@ -11,6 +11,6 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
// $this->call(UsersTableSeeder::class);
$this->call(UsersTableSeeder::class);
}
}
29 changes: 29 additions & 0 deletions database/seeds/UsersTableSeeder.php
@@ -0,0 +1,29 @@
<?php

use Illuminate\Database\Seeder;

class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$user = [
'name' => 'User',
'email' => 'user@mail.com',
'password' => bcrypt('password')
];

$admin = [
'name' => 'Admin',
'email' => 'admin@mail.com',
'password' => bcrypt('password')
];

DB::table('users')->insert($user);
DB::table('admins')->insert($admin);
}
}
23 changes: 23 additions & 0 deletions resources/views/admin/home.blade.php
@@ -0,0 +1,23 @@
@extends('layouts.app')

@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Admin Dashboard</div>

<div class="panel-body">
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif

You are logged in, Admin!
</div>
</div>
</div>
</div>
</div>
@endsection

1 comment on commit 8219ce9

@itsgratien
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no working for me, am using laravel 5.7

Please sign in to comment.