Home > Backend Development > PHP Tutorial > Detailed explanation of front-end and back-end login in Laravel 5.5 based on the built-in Auth module

Detailed explanation of front-end and back-end login in Laravel 5.5 based on the built-in Auth module

jacklove
Release: 2023-04-02 15:24:01
Original
1378 people have browsed it

I have been using laravel5.5 recently and implemented a function. I will share it with you below. This article mainly introduces you to the relevant information on how Laravel 5.5 implements front-end and back-end login based on the built-in Auth module. The article uses examples The code is introduced in very detail. Friends who need it can refer to it. Let's learn with the editor.

Preface

I won’t say much more, let’s take a look at the detailed introduction.

The method is as follows:

Step 1: Generate Auth

After the project is established, Generate built-in Auth component:

php artisan make:auth
Copy after login

Step 2: Add guard

Open config\auth.php for convenience Front and back user switching, projects share the Users table

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

Step 3: Implement the base class

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

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

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 = &#39;/admin&#39;;
 /**
 * Create a new controller instance.
 *
 * @return void
 */
 public function __construct()
 {
 $this->middleware(&#39;guest:admin&#39;)->except(&#39;logout&#39;);
 }
 /**
 * 重写登陆页面
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
 */
 public function showLoginForm()
 {
 return view(&#39;backend.login&#39;);
 }
 /**
 * 重写退出方法
 * @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(&#39;/admin/login&#39;);
 }
 /**
 * 重写guard认证
 * @return mixed
 */
 protected function guard()
 {
 return Auth::guard(&#39;admin&#39;);
 }
}
Copy after login

Step 5: Jump to different paths after login

app\Middleware\RedirectIfAuthenticated.php

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

Step 6: Jump to different login pages if authentication fails

app\Exceptions\Handler.php

/**
 * 重写实现未认证用户跳转至相应登陆页
 * @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([&#39;message&#39; => $exception->getMessage()], 401);
 }else{
  return in_array(&#39;admin&#39;, $exception->guards()) ? return redirect()->guest(&#39;/admin/login&#39;) : redirect()->guest(&#39;login&#39;);
 }
 }
Copy after login

Complete

Summary

Articles you may be interested in:

PHP Two-dimensional array implementation method to remove duplicates

Summary of Thinkphp5 behavior usage methods

ajax php implementation without refresh Examples of verifying mobile phone numbers

The above is the detailed content of Detailed explanation of front-end and back-end login in Laravel 5.5 based on the built-in Auth module. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template