Added: Signin Up, connection to the DB, validator, base controller, base view, environment file, User model
This commit is contained in:
@ -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' => [],
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
45
app/models/User.php
Normal file
45
app/models/User.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
require_once '../core/Database.php';
|
||||
|
||||
class User {
|
||||
private $db;
|
||||
|
||||
public function __construct() {
|
||||
$this->db = Database::getInstance()->getConnection();
|
||||
|
||||
if ($this->db) {
|
||||
error_log("Database connection established successfully.");
|
||||
} else {
|
||||
error_log("Failed to connect to the database.");
|
||||
}
|
||||
}
|
||||
|
||||
public function register($username, $email, $password) {
|
||||
// Check if email already exists
|
||||
$stmt = $this->db->prepare("SELECT id FROM users WHERE email = ?");
|
||||
$stmt->bind_param("s", $email);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
$stmt->close();
|
||||
|
||||
if ($result->num_rows > 0) {
|
||||
return "Email is already registered";
|
||||
}
|
||||
|
||||
// Hash the password
|
||||
$hashedPassword = password_hash($password, PASSWORD_BCRYPT);
|
||||
|
||||
$stmt = $this->db->prepare("INSERT INTO users (username, email, password, points, created_at) VALUES (?, ?, ?, 0, NOW())");
|
||||
$stmt->bind_param("sss", $username, $email, $hashedPassword);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
return true;
|
||||
} else {
|
||||
return "Error: " . $stmt->error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
@ -1,11 +1,33 @@
|
||||
<section class="signup">
|
||||
<form>
|
||||
<label for="username">Username</label>
|
||||
<input type="text" name="username" id="username">
|
||||
<label for="password">Password</label>
|
||||
<input type="password" name="password" id="password">
|
||||
<label for="password-2">Password again</label>
|
||||
<input type="password" name="password-2" id="password-2">
|
||||
<input type="submit" value="Sign Up" id="submit-signup">
|
||||
</form>
|
||||
<?php if ($this->get('error')): ?>
|
||||
<div class="error"><?= $this->get('error') ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<form action="/auth/signup" method="POST">
|
||||
<label for="username">Username</label>
|
||||
<input type="text" name="username" id="username" value="<?= htmlspecialchars($_POST['username'] ?? '') ?>" required>
|
||||
<?php if (isset($this->get('validationErrors')['username'])): ?>
|
||||
<small class="error"><?= $this->get('validationErrors')['username'] ?></small>
|
||||
<?php endif; ?>
|
||||
|
||||
<label for="email">Email</label>
|
||||
<input type="email" name="email" id="email" value="<?= htmlspecialchars($_POST['email'] ?? '') ?>" required>
|
||||
<?php if (isset($this->get('validationErrors')['email'])): ?>
|
||||
<small class="error"><?= $this->get('validationErrors')['email'] ?></small>
|
||||
<?php endif; ?>
|
||||
|
||||
<label for="password">Password</label>
|
||||
<input type="password" name="password" id="password" required>
|
||||
<?php if (isset($this->get('validationErrors')['password'])): ?>
|
||||
<small class="error"><?= $this->get('validationErrors')['password'] ?></small>
|
||||
<?php endif; ?>
|
||||
|
||||
<label for="password-2">Password again</label>
|
||||
<input type="password" name="password-2" id="password-2" required>
|
||||
<?php if (isset($this->get('validationErrors')['password_confirmation'])): ?>
|
||||
<small class="error"><?= $this->get('validationErrors')['password_confirmation'] ?></small>
|
||||
<?php endif; ?>
|
||||
|
||||
<input type="submit" value="Sign Up">
|
||||
</form>
|
||||
</section>
|
||||
|
Reference in New Issue
Block a user