1. 程式人生 > >laravel 後臺使用者登入

laravel 後臺使用者登入

新建表admin_users,欄位跟usersbiao表

在config/auth.php中新增配置

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

        'backend' => [
            'driver' => 'session',
            'provider' => 'backends',
           ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],

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

        'backends' => [
            'driver' => 'eloquent',
            'model' => App\Models\AdminUser::class,
           ],

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

建立中介軟體BackendAuthMiddleware.php

 public function handle($request, Closure $next) {
        if (auth()->guard('backend')->guest()) {
            if ($request->ajax() || $request->wantsJson()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('backend/login');
            }
        }

        return $next($request);
    }

以及中介軟體BackendGuestMiddleware.php

public function handle($request, Closure $next) {
        if (auth()->guard('backend')->check()) {
            return redirect('/backend/user_groups');
        }

        return $next($request);
    }

並將中介軟體新增到Kernel.php中

'auth.backend' => \App\Http\Middleware\BackendAuthMiddleware::class,
        'guest.backend' => \App\Http\Middleware\BackendGuestMiddleware::class,

 

建立給控制器Backend/LoginController.php

namespace App\Http\Controllers\Backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    use AuthenticatesUsers;

    protected $redirectTo = '/backend/configs';

    public function __construct() {
        $this->middleware('guest.backend', ['except' => 'logout']);
    }

    public function showLoginForm() {
        return view('backend.layouts.login');
    }

    protected function guard() {
        return auth()->guard('backend');
    }

    public function username() {
        return 'name';
    }

    public function logout(Request $request) {

        $request->session()->flush();

        $request->session()->regenerate();

        return redirect('/backend/login');
    }
}

路由

protected function mapAdminRoutes()
    {
        Route::prefix('backend')
             ->middleware('web')
             ->namespace($this->namespace.'\Backend')
             ->group(base_path('routes/backend.php'));
    }

routes/backend.php


Route::group(['middleware' => 'auth.backend'], function () {
  Route::get('/', '[email protected]');
  Route::resource('admin_users', 'AdminUsersController');
  Route::resource('configs', 'ConfigsController');
  Route::get('sitemap', '[email protected]');
  
});

Route::get('login', '[email protected]')->name('backend.login');
Route::post('login', '[email protected]');
Route::get('logout', '[email protected]');