当前位置:网站首页>Larvel document reading notes custom authentication login and registration using larvel 8
Larvel document reading notes custom authentication login and registration using larvel 8
2022-07-02 17:45:00 【IT1995】
Blog posts mainly use Laravel8 Create custom login and registration .
Create custom user logins using traditional simple methods 、 register 、 Panel page .
Case study
There are the following steps :
① establish Laravel application ;
② Connect MySQL database ;
③ Set up auth Of Controller;
④ establish auth route ;
⑤ establish auth Of blade view file ;
⑥ start-up Laravel service .
establish Laravel application
Installation and configuration composer after , Use the following command to create Laravel project .
composer create-project --prefer-dist laravel/laravel laravel_new Enter the directory where you created the application .
cd laravel_new Connect to database
Give Way Laravel Connect mysql database , open .env The configuration file , Modify the database name , user name , password :
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=database_user_name
DB_PASSWORD=database_password Laravel The default user model and migration file are provided , So it can be used php The command is created from the migration file given by default mysql surface .
php artisan migrate Set the permissions Controller
Call the following command , establish Controller
php artisan make:controller CustomAuthControllerstay app\Http\Controllers\CustomAuthController.php Replace with the following code :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Hash;
use Session;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
class CustomAuthController extends Controller
{
public function index()
{
return view('auth.login');
}
public function customLogin(Request $request)
{
$request->validate([
'email' => 'required',
'password' => 'required',
]);
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
return redirect()->intended('dashboard')
->withSuccess('Signed in');
}
return redirect("login")->withSuccess('Login details are not valid');
}
public function registration()
{
return view('auth.registration');
}
public function customRegistration(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
$data = $request->all();
$check = $this->create($data);
return redirect("dashboard")->withSuccess('You have signed-in');
}
public function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password'])
]);
}
public function dashboard()
{
if(Auth::check()){
return view('dashboard');
}
return redirect("login")->withSuccess('You are not allowed to access');
}
public function signOut() {
Session::flush();
Auth::logout();
return Redirect('login');
}
} establish Auth route
stay routes/web.php Add the following code to :
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CustomAuthController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/
Route::get('dashboard', [CustomAuthController::class, 'dashboard']);
Route::get('login', [CustomAuthController::class, 'index'])->name('login');
Route::post('custom-login', [CustomAuthController::class, 'customLogin'])->name('login.custom');
Route::get('registration', [CustomAuthController::class, 'registration'])->name('register-user');
Route::post('custom-registration', [CustomAuthController::class, 'customRegistration'])->name('register.custom');
Route::get('signout', [CustomAuthController::class, 'signOut'])->name('signout'); establish auth Of blade view file
stay resources/views Created in login.blade.php:
@extends('app')
@section('content')
<main class="login-form mt-5">
<div class="cotainer">
<div class="row justify-content-center">
<div class="col-md-4">
<div class="card">
<h3 class="card-header text-center">Login</h3>
<div class="card-body">
<form method="POST" action="{
{ route('login.custom') }}">
@csrf
<div class="form-group mb-3">
<input type="text" placeholder="Email" id="email" class="form-control" name="email" required
autofocus>
@if ($errors->has('email'))
<span class="text-danger">{
{ $errors->first('email') }}</span>
@endif
</div>
<div class="form-group mb-3">
<input type="password" placeholder="Password" id="password" class="form-control" name="password" required>
@if ($errors->has('password'))
<span class="text-danger">{
{ $errors->first('password') }}</span>
@endif
</div>
<div class="form-group mb-3">
<div class="checkbox">
<label>
<input type="checkbox" name="remember"> Remember Me
</label>
</div>
</div>
<div class="d-grid mx-auto">
<button type="submit" class="btn btn-dark btn-block">Signin</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</main>
@endsection Then create registration.blade.php stay resources/views/auth Under the table of contents :
@extends('app')
@section('content')
<main class="signup-form mt-5">
<div class="cotainer">
<div class="row justify-content-center">
<div class="col-md-4">
<div class="card">
<h3 class="card-header text-center">Registration</h3>
<div class="card-body">
<form action="{
{ route('register.custom') }}" method="POST">
@csrf
<div class="form-group mb-3">
<input type="text" placeholder="Name" id="name" class="form-control" name="name"
required autofocus>
@if ($errors->has('name'))
<span class="text-danger">{
{ $errors->first('name') }}</span>
@endif
</div>
<div class="form-group mb-3">
<input type="text" placeholder="Email" id="email_address" class="form-control"
name="email" required autofocus>
@if ($errors->has('email'))
<span class="text-danger">{
{ $errors->first('email') }}</span>
@endif
</div>
<div class="form-group mb-3">
<input type="password" placeholder="Password" id="password" class="form-control"
name="password" required>
@if ($errors->has('password'))
<span class="text-danger">{
{ $errors->first('password') }}</span>
@endif
</div>
<div class="form-group mb-3">
<div class="checkbox">
<label><input type="checkbox" name="remember"> Remember Me</label>
</div>
</div>
<div class="d-grid mx-auto">
<button type="submit" class="btn btn-dark btn-block">Sign up</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</main>
@endsection The next step is resources/views Created in dashborad.blade.php file :
@extends('layouts.app')
@section('content')
<nav class="navbar navbar-light navbar-expand-lg mb-5" style="background-color: #e3f2fd;">
<div class="container">
<a class="navbar-brand mr-auto" href="#">Example</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
@guest
<li class="nav-item">
<a class="nav-link" href="{
{ route('login') }}">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{
{ route('register-user') }}">Register</a>
</li>
@else
<li class="nav-item">
<a class="nav-link" href="{
{ route('signout') }}">Logout</a>
</li>
@endguest
</ul>
</div>
</div>
</nav>
@endsection Finally create resources/views/app.blade.php file :
<!DOCTYPE html>
<html lang="{
{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{
{config('app.name','LSAPP')}}</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
@yield('content')
</body>function laravel service
php artisan serve register URL And login URL Respectively :
http://127.0.0.1:8000/registration
http://127.0.0.1:8000/login Output :


边栏推荐
- 嵌入式 ~ 介绍
- Ssm+ wechat applet to realize property management system
- Si446 usage record (II): generate header files using wds3
- 链表求和[dummy+尾插法+函数处理链表引用常见坑位]
- 【目标跟踪】|数据集汇总
- 常用SQL语句(完整范例)
- JS20 数组扁平化
- 【目标跟踪】|SiamFC
- Navigateur Chrome pour un accès rapide au stackoverflow
- [nonlinear control theory]8_ Comparison of three robust controllers
猜你喜欢

Microservice architecture practice: Construction of highly available distributed file system fastdfs architecture

智能水电表能耗监测云平台

一日2篇Nature!中科大校友段镶锋团队纳米材料新成果,曾是贝尔比奖章第三位华人得主...

About me

This "architect growth note" made 300 people successfully change jobs and enter the big factory, with an annual salary of 50W

RK1126平台项目总结
![[非线性控制理论]8_三种鲁棒控制器的比较](/img/a8/03ed363659a0a067c2f1934457c106.png)
[非线性控制理论]8_三种鲁棒控制器的比较
![List summation [dummy+ tail interpolation + function processing list reference common pit]](/img/08/30e8ca2376104d648a82dca8a72c42.png)
List summation [dummy+ tail interpolation + function processing list reference common pit]

外包干了五年,废了...

The construction of scalable distributed database cluster and the partition design of oneproxy sub database
随机推荐
Schoolbag novel multithreaded crawler [easy to understand]
【网络是怎么连接的】第四章 探索接入网和网络运营商
线性规划例题 投资的收益与风险
海思Hi3798MV100机顶盒芯片介绍[通俗易懂]
[非线性控制理论]7_High gain and High Frequency
[非线性控制理论]8_三种鲁棒控制器的比较
阿里云子账户 - 权限策略 - 授权给某个账户某个 OSS Bucket 的完全控制权限
Introduction to Hisilicon hi3798mv100 set top box chip [easy to understand]
【目标跟踪】|数据集汇总
外包干了五年,废了...
PCL knowledge points - voxelized grid method for down sampling of point clouds
HDU - 1114 Piggy-Bank(完全背包)
【曆史上的今天】7 月 2 日:BitTorrent 問世;商業系統 Linspire 被收購;索尼部署 PlayStation Now
ROS knowledge points -- the difference between ros:: nodehandle N and NH ("~")
Use of nexttile function in MATLAB
After meeting a full stack developer from Tencent, I saw what it means to be proficient in MySQL tuning
详细介绍scrollIntoView()方法属性
SAP commerce Cloud Architecture Overview
牛客 JS3 分隔符
ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装