Bulletinboard/app/Http/Controllers/AuthController.php

66 lines
1.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Role;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
class AuthController extends Controller
{
public function loginView() {
return view('login');
}
public function login(Request $request) {
$credentials = $request->validate([
'username' => 'required|max:64',
'password' => 'required'
]);
if(Auth::attempt($credentials)) {
$request->session()->regenerate();
return redirect("/");
}
return view("error");
}
public function registerView() {
return view('register');
}
public function register(Request $request) {
$rawUser = $request->validate([
'name' => 'required|max:64|min:2',
'password' => 'required|max:48|min:8|confirmed',
'username' => 'required'
]);
$user = new User($rawUser);
$user->password = Hash::make($user->password);
$user->role()->associate(Role::where('name', 'User')->first());
if($user->save()) {
return redirect("login");
}
return redirect("error");
}
public function logout() {
Auth::logout();
return redirect("/");
}
}