Laravel5.5 基於內置的Auth實現先後臺登錄

步驟1:生成Auth

在項目創建完成後,生成內置Auth組件:php

php artisan make:auth

步驟2:添加guard

打開config\auth.php,爲方便先後臺用戶切換,項目共用Users表json

'guards' => [
        //...
        'admin' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        //...
    ],

步驟3:實現基類

class AdminController extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function __construct()
    {
        $this->middleware('auth:admin');
    }
}

步驟4:實現後臺登錄控制器

新建appcontrollersAdminLoginController.phpsession

<?php

namespace App\Http\Controllers\Admin;

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

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.
    |
    */

    use AuthenticatesUsers;

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

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

    /**
     * 重寫登錄頁面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function showLoginForm()
    {
        return view('backend.login');
    }

    /**
     * 重寫退出方法
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function logout(Request $request)
    {
        $this->guard()->logout();
        $request->session()->flush();
        $request->session()->regenerate();
        return redirect('/admin/login');
    }

    /**
     * 重寫guard認證
     * @return mixed
     */
    protected function guard()
    {
        return Auth::guard('admin');
    }
}

步驟5:實現登錄後跳轉到不一樣路徑

app\Middleware\RedirectIfAuthenticated.phpapp

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

        return $next($request);
}

步驟6:實現未經過認證跳轉不一樣登錄頁

app\Exceptions\Handler.phpide

/**
     * 重寫實現未認證用戶跳轉至相應登錄頁
     * @param \Illuminate\Http\Request $request
     * @param AuthenticationException $exception
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
     */
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if($request->expectsJson()){
            return response()->json(['message' => $exception->getMessage()], 401);
        }else{
            return in_array('admin', $exception->guards()) ? return redirect()->guest('/admin/login') : redirect()->guest('login');
        }
    }

完成this

相關文章
相關標籤/搜索