Skip to content

Latest commit

 

History

History
90 lines (72 loc) · 1.76 KB

authentication.md

File metadata and controls

90 lines (72 loc) · 1.76 KB

Authentication

SleepingOwl Admin uses Laravel authentication by middleware.

In your app you should run artisan command: (See https://laravel.com/docs/5.2/authentication#authentication-quickstart)

$ php artisan make:auth

And after add middleware auth to config/sleeping_owl.php

Example

/*
 | ...
 | see https://laravel.com/docs/5.2/authentication#authentication-quickstart
 |
 */
'middleware' => ['web', 'auth'],
...

Custom Middleware

You cat create custom middleware class< for example App\Http\Middleware\AdminAuthenticate

<?php

namespace App\Http\Middleware;

use App\User;
use Closure;
use Illuminate\Support\Facades\Auth;
class AdminAuthenticate
{
    /**
     * 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)
    {
        $auth = Auth::guard($guard);

        if (Auth::guard($guard)->guest()) {
            if ($request->ajax() || $request->wantsJson()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('login');
            }
        }

        if (! $auth->user()->isAdmin()) {
            return response('Access denied.', 401);
        }
        
        return $next($request);
    }
}

And register this middleware in App\Http\Kernel

 ...
 protected $routeMiddleware = [
     ...
     'admin' => \App\Http\Middleware\AdminAuthenticate::class,
     ...
 ];
 ...

And add this middleware admin to config\sleeoping_owl.php

...
'middleware' => ['web', 'admin'],
...