Admin 区域

This commit is contained in:
iVampireSP.com 2022-11-14 19:45:48 +08:00
parent 74bbf6cadf
commit e451c76d3a
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
4 changed files with 178 additions and 0 deletions

View File

@ -0,0 +1,59 @@
<?php
namespace App\Console\Commands;
use App\Models\Admin;
use Illuminate\Console\Command;
use Symfony\Component\Console\Command\Command as CommandAlias;
class CreateAdmin extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'create:admin';
/**
* The console command description.
*
* @var string
*/
protected $description = '创建一个管理员账号';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
// 邮箱
$email = $this->ask('请输入邮箱');
// 密码
$password = $this->secret('请输入密码');
// 确认密码
$password_confirmation = $this->secret('请再次输入密码');
// 验证密码
if ($password !== $password_confirmation) {
$this->error('两次输入的密码不一致。');
return CommandAlias::FAILURE;
}
// 创建管理员
$admin = Admin::create([
'email' => $email,
'password' => bcrypt($password)
]);
// 输出信息
$this->info('管理员创建成功ID为' . $admin->id);
return CommandAlias::SUCCESS;
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AuthController extends Controller
{
//
public function index()
{
// if not authed
if (!auth('admin')->check()) {
return view('admin.login');
} else {
return redirect()->route('admin.index');
}
}
public function login(Request $request) {
if (auth('admin')->attempt($request->only('email', 'password'))) {
return redirect()->route('admin.index');
} else {
return redirect()->route('admin.login')->with('error', 'Invalid credentials');
}
}
}

View File

@ -0,0 +1,69 @@
@extends('layouts.admin')
@section('content')
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Login') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('admin.login') }}">
@csrf
<div class="row mb-3">
<label for="email" class="col-md-4 col-form-label text-md-end">{{ __('Email Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control @error('email') is-invalid @enderror"
name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="row mb-3">
<label for="password" class="col-md-4 col-form-label text-md-end">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password"
class="form-control @error('password') is-invalid @enderror" name="password" required
autocomplete="current-password">
@error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="row mb-3">
<div class="col-md-6 offset-md-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="remember"
id="remember" {{ old('remember') ? 'checked' : '' }}>
<label class="form-check-label" for="remember">
{{ __('Remember Me') }}
</label>
</div>
</div>
</div>
<div class="row mb-0">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Login') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
@endsection

20
routes/admin.php Normal file
View File

@ -0,0 +1,20 @@
<?php
use App\Http\Controllers\Admin\AuthController;
use Illuminate\Support\Facades\Route;
Route::withoutMiddleware(['auth'])->group(function () {
Route::get('/login', [AuthController::class, 'index'])->name('login');
Route::post('/login', [AuthController::class, 'login']);
});
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
Route::view('/', 'admin.index')->name('index')->middleware('auth:admin');
Route::group([
'middleware' => 'auth:admins',
], function () {
// Route::resource('merchants', MerchantController::class);
});