用户支持登录登出
This commit is contained in:
parent
cca0958899
commit
d07cad1cd8
67
app/Http/Controllers/UserController.php
Normal file
67
app/Http/Controllers/UserController.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class UserController extends BaseController
|
||||
{
|
||||
public function login_page(Request $request)
|
||||
{
|
||||
return view("user.login");
|
||||
}
|
||||
|
||||
public function authenticate(Request $request): \Illuminate\Http\RedirectResponse
|
||||
{
|
||||
$request->validate([
|
||||
"username" => ["required"],
|
||||
"password" => ["required"],
|
||||
]);
|
||||
$credentials = [
|
||||
"password" => $request->post("password"),
|
||||
];
|
||||
if (str_contains($request->post("username"), "@")) {
|
||||
$credentials["email"] = $request->post("username");
|
||||
} else {
|
||||
$credentials["name"] = $request->post("username");
|
||||
}
|
||||
if (Auth::attempt($credentials, $request->post("remember", 0) == 1)) {
|
||||
$request->session()->regenerate();
|
||||
return redirect()->intended();
|
||||
}
|
||||
return back()->withErrors([
|
||||
"username" => "无此用户",
|
||||
]);
|
||||
}
|
||||
|
||||
public function logout(Request $request)
|
||||
{
|
||||
Auth::logout();
|
||||
$request->session()->invalidate();
|
||||
$request->session()->regenerateToken();
|
||||
return redirect("/");
|
||||
}
|
||||
|
||||
public function register_page(Request $request)
|
||||
{
|
||||
return view("user.register");
|
||||
}
|
||||
|
||||
public function register(Request $request)
|
||||
{
|
||||
$request_payload = $request->validate([
|
||||
"name" => ["required", "unique:users"],
|
||||
"email" => ["required", "email", "unique:users"],
|
||||
"password" => ["required"],
|
||||
]);
|
||||
$user = new User();
|
||||
$request_payload["password"] = Hash::make($request_payload["password"]);
|
||||
$user->fill($request_payload);
|
||||
$user->save();
|
||||
return redirect(route("login"));
|
||||
}
|
||||
}
|
44
app/Models/User.php
Normal file
44
app/Models/User.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
use HasApiTokens, HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for serialization.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast.
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
protected $casts = [
|
||||
'email_verified_at' => 'datetime',
|
||||
];
|
||||
}
|
@ -17,7 +17,7 @@ class RouteServiceProvider extends ServiceProvider
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public const HOME = '/home';
|
||||
public const HOME = '/';
|
||||
|
||||
/**
|
||||
* The controller namespace for the application.
|
||||
@ -38,10 +38,10 @@ class RouteServiceProvider extends ServiceProvider
|
||||
$this->configureRateLimiting();
|
||||
|
||||
$this->routes(function () {
|
||||
// Route::prefix('api')
|
||||
// ->middleware('api')
|
||||
// ->namespace($this->namespace)
|
||||
// ->group(base_path('routes/api.php'));
|
||||
Route::prefix('api')
|
||||
->middleware('api')
|
||||
->namespace($this->namespace)
|
||||
->group(base_path('routes/api.php'));
|
||||
|
||||
Route::middleware('web')
|
||||
->namespace($this->namespace)
|
||||
|
39
database/factories/UserFactory.php
Normal file
39
database/factories/UserFactory.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class UserFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function definition()
|
||||
{
|
||||
return [
|
||||
'name' => $this->faker->name(),
|
||||
'email' => $this->faker->unique()->safeEmail(),
|
||||
'email_verified_at' => now(),
|
||||
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
|
||||
'remember_token' => Str::random(10),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the model's email address should be unverified.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Factories\Factory
|
||||
*/
|
||||
public function unverified()
|
||||
{
|
||||
return $this->state(function (array $attributes) {
|
||||
return [
|
||||
'email_verified_at' => null,
|
||||
];
|
||||
});
|
||||
}
|
||||
}
|
36
database/migrations/2014_10_12_000000_create_users_table.php
Normal file
36
database/migrations/2014_10_12_000000_create_users_table.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateUsersTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('users', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->string('email')->unique();
|
||||
$table->timestamp('email_verified_at')->nullable();
|
||||
$table->string('password');
|
||||
$table->rememberToken();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('users');
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreatePasswordResetsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('password_resets', function (Blueprint $table) {
|
||||
$table->string('email')->index();
|
||||
$table->string('token');
|
||||
$table->timestamp('created_at')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('password_resets');
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateFailedJobsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('failed_jobs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('uuid')->unique();
|
||||
$table->text('connection');
|
||||
$table->text('queue');
|
||||
$table->longText('payload');
|
||||
$table->longText('exception');
|
||||
$table->timestamp('failed_at')->useCurrent();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('failed_jobs');
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreatePersonalAccessTokensTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('personal_access_tokens', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->morphs('tokenable');
|
||||
$table->string('name');
|
||||
$table->string('token', 64)->unique();
|
||||
$table->text('abilities')->nullable();
|
||||
$table->timestamp('last_used_at')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('personal_access_tokens');
|
||||
}
|
||||
}
|
@ -6,5 +6,13 @@
|
||||
<a class="text-blue-600 underline" href="/comments">节目单查询</a>
|
||||
<a class="text-blue-600 underline" href="/danmakus">直播弹幕查询</a>
|
||||
<a class="text-blue-600 underline" href="/programs" title="数据不全,待补充">节目查询</a>
|
||||
@auth
|
||||
<a class="text-blue-600 underline" href="/programs/construct">节目建设</a>
|
||||
<a class="text-blue-600 underline float-right" href="{{ route("logout") }}">登出</a>
|
||||
<div class="float-right text-cyan-300 mx-2 border bg-blue-600">{{ Auth::user()->name }}</div>
|
||||
@endauth
|
||||
@guest
|
||||
<a class="text-blue-600 underline float-right" href="{{ route("login") }}">登录</a>
|
||||
@endguest
|
||||
</div>
|
||||
</div>
|
||||
|
@ -11,7 +11,7 @@
|
||||
<input type="hidden" name="id" value="{{$program_video->id}}">
|
||||
<label class="block my-2">
|
||||
BVID
|
||||
<input class="form-input border-0 border-b-2 w-full" @if($program_video->video_bvid) disabled @endif type="text" name="video_bvid" value="{{ old("video_bvid", $program_video->video_bvid) }}">
|
||||
<input class="form-input border-0 border-b-2 w-full" @disabled($program_video->video_bvid) type="text" name="video_bvid" value="{{ old("video_bvid", $program_video->video_bvid) }}">
|
||||
</label>
|
||||
<label class="block my-2">
|
||||
开始P数
|
||||
|
35
resources/views/user/login.blade.php
Normal file
35
resources/views/user/login.blade.php
Normal file
@ -0,0 +1,35 @@
|
||||
<html lang="zh">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>登录</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link href="{{ mix('/css/app.css') }}" rel="stylesheet"/>
|
||||
</head>
|
||||
<body>
|
||||
@include("common.header")
|
||||
<form class="w-full lg:w-1/2 border-2 mx-auto my-2" action="" method="post" enctype="multipart/form-data">
|
||||
<div class="block text-lg font-bold ml-4 mt-2">登录</div>
|
||||
<label class="block my-2">
|
||||
用户名
|
||||
<input class="form-input border-0 border-b-2 w-full" type="text" name="username" value="{{ old("username") }}" placeholder="用户名或邮箱">
|
||||
</label>
|
||||
<label class="block my-2">
|
||||
密码
|
||||
<input class="form-input border-0 border-b-2 w-full" type="password" name="password" value="{{ old("password") }}" placeholder="密码">
|
||||
</label>
|
||||
<label class="block my-4">
|
||||
记住密码
|
||||
<input class="form-checkbox" type="checkbox" name="remember" @checked(old("remember") == 1) value="1">
|
||||
</label>
|
||||
@if($errors->any())
|
||||
@foreach ($errors->all() as $error)
|
||||
<div class="bg-red-600 text-white">错误:{{ $error }}</div>
|
||||
@endforeach
|
||||
@endif
|
||||
<div class="block my-2 text-center">
|
||||
<input class="px-6 py-2 inline-block rounded-full bg-cyan-600 text-white" type="submit" value="登录">
|
||||
</div>
|
||||
</form>
|
||||
@include("common.footer")
|
||||
</body>
|
||||
</html>
|
35
resources/views/user/register.blade.php
Normal file
35
resources/views/user/register.blade.php
Normal file
@ -0,0 +1,35 @@
|
||||
<html lang="zh">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>注册</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link href="{{ mix('/css/app.css') }}" rel="stylesheet"/>
|
||||
</head>
|
||||
<body>
|
||||
@include("common.header")
|
||||
<form class="w-full lg:w-1/2 border-2 mx-auto my-2" action="" method="post" enctype="multipart/form-data">
|
||||
<div class="block text-lg font-bold ml-4 mt-2">登录</div>
|
||||
<label class="block my-2">
|
||||
用户名
|
||||
<input class="form-input border-0 border-b-2 w-full" type="text" name="name" value="{{ old("name") }}" placeholder="用户名">
|
||||
</label>
|
||||
<label class="block my-2">
|
||||
邮箱
|
||||
<input class="form-input border-0 border-b-2 w-full" type="email" name="email" value="{{ old("email") }}" placeholder="邮箱">
|
||||
</label>
|
||||
<label class="block my-2">
|
||||
密码
|
||||
<input class="form-input border-0 border-b-2 w-full" minlength="5" type="password" name="password" value="{{ old("password") }}" placeholder="密码">
|
||||
</label>
|
||||
@if($errors->any())
|
||||
@foreach ($errors->all() as $error)
|
||||
<div class="bg-red-600 text-white">错误:{{ $error }}</div>
|
||||
@endforeach
|
||||
@endif
|
||||
<div class="block my-2 text-center">
|
||||
<input class="px-6 py-2 inline-block rounded-full bg-cyan-600 text-white" type="submit" value="注册">
|
||||
</div>
|
||||
</form>
|
||||
@include("common.footer")
|
||||
</body>
|
||||
</html>
|
19
routes/api.php
Normal file
19
routes/api.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| API Routes
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Here is where you can register API routes for your application. These
|
||||
| routes are loaded by the RouteServiceProvider within a group which
|
||||
| is assigned the "api" middleware group. Enjoy building your API!
|
||||
|
|
||||
*/
|
||||
|
||||
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
|
||||
return $request->user();
|
||||
});
|
@ -13,7 +13,7 @@ use Illuminate\Support\Facades\Route;
|
||||
|
|
||||
*/
|
||||
// 对外列表
|
||||
Route::redirect('/', '/programs');
|
||||
Route::get('/', ["\\App\\Http\\Controllers\\ProgramQueryController","index"]);
|
||||
Route::get('/comments', ["\\App\\Http\\Controllers\\CommentQueryController","index"]);
|
||||
Route::get('/video/{video}', ["\\App\\Http\\Controllers\\VideoQueryController","info"]);
|
||||
Route::get('/programs', ["\\App\\Http\\Controllers\\ProgramQueryController","index"]);
|
||||
@ -22,6 +22,12 @@ Route::get('/danmakus', ["\\App\\Http\\Controllers\\DanmakuQueryController","ind
|
||||
Route::get('/danmakus/{bvid}', ["\\App\\Http\\Controllers\\DanmakuQueryController","specific_search"]);
|
||||
Route::get('/upload', ["\\App\\Http\\Controllers\\FileController","index"]);
|
||||
Route::post('/upload', ["\\App\\Http\\Controllers\\FileController","upload"]);
|
||||
// 用户部分
|
||||
Route::get('/login', ["\\App\\Http\\Controllers\\UserController", "login_page"])->name("login");
|
||||
Route::post('/login', ["\\App\\Http\\Controllers\\UserController", "authenticate"])->name("login.submit");
|
||||
Route::get('/register', ["\\App\\Http\\Controllers\\UserController", "register_page"])->name("register");
|
||||
Route::post('/register', ["\\App\\Http\\Controllers\\UserController", "register"])->name("register.submit");
|
||||
Route::get('/logout', ["\\App\\Http\\Controllers\\UserController", "logout"])->name("logout");
|
||||
// 建设部分
|
||||
// 节目建设
|
||||
Route::get('/programs/construct', ["\\App\\Http\\Controllers\\ProgramConstructController","index"])->name("program.construct.list");
|
||||
|
@ -16,3 +16,5 @@ mix.js('resources/js/app.js', 'public/js')
|
||||
require('tailwindcss')
|
||||
])
|
||||
.version();
|
||||
|
||||
mix.disableNotifications();
|
||||
|
Loading…
x
Reference in New Issue
Block a user