Added login and profile page

This commit is contained in:
Notoric 2024-06-09 23:03:34 +01:00
parent 67abd0b6d1
commit b25459ad6f
5 changed files with 78 additions and 2 deletions

View File

@ -0,0 +1,24 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller {
public function login(Request $request) {
$credentials = $request->validate([
'email' => 'required|email|string',
'password' => 'required|string',
]);
if (Auth::attempt($credentials, $remember = false)) {
$request->session()->regenerate();
return redirect('/profile');
}
return back()->withErrors([
'email' => 'The provided credentials do not match our records.',
]);
}
}

View File

@ -17,7 +17,9 @@ class RegisterController extends Controller {
$user = User::create($data); $user = User::create($data);
return redirect('/'); auth()->login($user);
return redirect('/profile');
} catch (ValidationException $e) { } catch (ValidationException $e) {
return redirect()->back()->withInput($request->input())->withErrors($e->errors()); return redirect()->back()->withInput($request->input())->withErrors($e->errors());
} }

View File

@ -0,0 +1,18 @@
@extends('default')
@section('title')
Log In
@endsection
@section('content')
<h1>Log In</h1>
<form method="post" action="login">
@csrf
<label for="email">Email</label>
<input type="email" name="email" id="email" value="{{ old('email') }}" required>
<label for="password">Password</label>
<input type="password" name="password" id="password" required>
<button type="submit">Log In</button>
</form>
<pre style="color: red;">{{ $errors->first() }}</pre>
@endsection

View File

@ -0,0 +1,12 @@
@extends('default')
@section('title')
Profile
@endsection
@section('content')
<h1>Profile</h1>
<p>Username: <em>{{ Auth::user()->name }}</em></p>
<p>Email: <em>{{ Auth::user()->email }}</em></p>
<p>Created at: <em>{{ Auth::user()->created_at }}</em></p>
@endsection

View File

@ -2,6 +2,7 @@
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
use App\Http\Controllers\RegisterController; use App\Http\Controllers\RegisterController;
use App\Http\Controllers\LoginController;
Route::get('/', function () { Route::get('/', function () {
return view('welcome'); return view('welcome');
@ -10,4 +11,23 @@ Route::get('/', function () {
Route::get('/register', function () { Route::get('/register', function () {
return view('register'); return view('register');
}); });
Route::post('/register', [RegisterController::class, 'create']); Route::post('/register', [RegisterController::class, 'create']);
Route::get('/login', function () {
return view('login');
});
Route::post('/login', [LoginController::class, 'login']);
Route::get('/logout', function () {
auth()->logout();
return redirect('/');
});
Route::get('/profile', function () {
if (!auth()->check()) {
return redirect('/');
}
return view('profile');
});