Laravel 5.5 Based on built in Auth module to realize foreground and foreground login details

  • 2021-08-28 19:29:18
  • OfStack

Preface

This article mainly introduces the related contents of Laravel 5.5 based on the built-in Auth module. For more information about Auth module, please refer to this article: https://www.ofstack.com/article/121401. htm

The following words are not much to say, let's take a look at the detailed introduction.

The method is as follows:

Step 1: Generate Auth

After the project is set up, build the built-in Auth component:


php artisan make:auth

Step 2: Add guard

Open config\ auth. php. For the convenience of switching between front and back users, the project shares Users table


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

Step 3: Implement the base class


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

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

Step 4: Implement the background login controller

New appcontrollersAdminLoginController. php


<?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');
 }
 /**
 *  Rewrite the landing page 
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
 */
 public function showLoginForm()
 {
 return view('backend.login');
 }
 /**
 *  Override exit method 
 * @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');
 }
 /**
 *  Rewrite guard Certification 
 * @return mixed
 */
 protected function guard()
 {
 return Auth::guard('admin');
 }
}

Step 5: Jump to different paths after landing

app\Middleware\RedirectIfAuthenticated.php


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

Step 6: Jump to different landing pages without authentication

app\Exceptions\Handler.php


/**
 *  Rewrite to realize that unauthenticated users jump to the corresponding landing page 
 * @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');
 }
 }

Finish

Summarize


Related articles: