Added: Signin Up, connection to the DB, validator, base controller, base view, environment file, User model

This commit is contained in:
2024-12-26 02:00:33 +01:00
parent d7b8aba072
commit 5bca9b12aa
9 changed files with 338 additions and 27 deletions

View File

@ -1,19 +1,68 @@
<?php
class AuthController {
class AuthController extends Controller {
public function signin() {
$view = new View();
$data = [
'title' => 'Log In'
];
$view->render('auth/signin', $data);
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = $_POST['email'] ?? '';
$password = $_POST['password'] ?? '';
$user = new User();
$result = $user->login($email, $password);
if($result === true) {
$this->redirect('/dashboard');
} else {
$this->view('auth/signin', ['error' => $result]);
}
} else {
$this->view('auth/signin', ['title' => 'Log In']);
}
}
public function signup() {
$view = new View();
$data = [
'title' => 'Register'
];
$view->render('auth/signup', $data);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'] ?? '';
$email = $_POST['email'] ?? '';
$password = $_POST['password'] ?? '';
$password2 = $_POST['password-2'] ?? '';
// Perform validations
$validator = new Validator();
$validator->required('username', $username);
$validator->email('email', $email);
$validator->required('password', $password);
$validator->minLength('password', $password, 8);
$validator->alphanumeric('password', $password);
if ($password !== $password2) {
$validator->errors()['password_confirmation'] = 'Passwords do not match.';
}
if (!$validator->passes()) {
$this->view('auth/signup', [
'error' => 'Please correct the errors below.',
'validationErrors' => $validator->errors() ?: [],
]);
return;
}
$user = new User();
$result = $user->register($username, $email, $password);
if ($result === true) {
$this->redirect('/auth/signin');
} else {
$this->view('auth/signup', [
'error' => $result,
'validationErrors' => [],
]);
}
} else {
$this->view('auth/signup', [
'title' => 'Register',
'validationErrors' => [],
]);
}
}
}