Skip to content
Snippets Groups Projects
Commit 9ad19bb4 authored by Bitha17's avatar Bitha17
Browse files

integrate login and register fe and be

parent 6a133b7e
Branches feat/authentication
Tags
No related merge requests found
<?php
// app/controllers/userController.php
ob_start();
require_once(__DIR__ . '/../Models/User.php');
......@@ -72,9 +72,36 @@ class UserController {
$emailOrUsername = $_POST['loginIdentifier'];
$password = $_POST['loginPassword'];
if (filter_var($emailOrUsername, FILTER_VALIDATE_EMAIL)){
echo $this->loginByEmail($emailOrUsername, $password);
if ($this->loginByEmail($emailOrUsername, $password) === "success") {
header("Location:/../../../home.php");
ob_end_flush();
} else {
header("Location:Views/login/login.php");
ob_end_flush();
}
} else {
if ($this->loginByUsername($emailOrUsername,$password) === "success") {
header("Location:/../../../home.php");
ob_end_flush();
} else {
header("Location:Views/login/login.php");
ob_end_flush();
}
}
} elseif ($_POST['userAction'] === 'createUser') {
unset($_POST['userAction']);
$userName = $_POST['userName'];
$userUsername = $_POST['userUsername'];
$userEmail = $_POST['userEmail'];
$userPassword = $_POST['userPassword'];
$isAdmin = isset($_POST['isAdmin']) ? 1 : 0;
$success = $this->registerUser($userName,$userUsername,$userEmail,$userPassword,$isAdmin)['success'];
if($success){
header("Location:Views/login/login.php");
ob_end_flush();
} else {
echo $this->loginByUsername($emailOrUsername,$password);
header("Location:Views/login/register.php");
ob_end_flush();
}
} else {
// Handle other actions here, if needed
......@@ -83,4 +110,4 @@ class UserController {
}
}
?>
?>
\ No newline at end of file
......@@ -7,20 +7,28 @@ require_once(__DIR__ . '/../../db/connect.php');
class UserModel {
public function createUser($name, $username, $email, $hashedPass, $isAdmin) {
global $db;
$response = [
'success'=> true,
'message'=> ''
];
try {
$stmt = $db->prepare("INSERT INTO users (user_name, username, user_email, user_hashedPass, isAdmin) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$name, $username, $email, $hashedPass, $isAdmin]);
return "User created successfully";
$response['message'] = "User created successfully";
return $response;
} catch (PDOException $e) {
$response['success'] = false;
// Check if the error code corresponds to a unique constraint violation.
if ($e->getCode() === '23000') {
// Handle the error as a duplicate entry.
return "Username or email already exists. Please choose a different one.";
$response['message'] = "Username or email already exists. Please choose a different one.";
return $response;
} else {
// Handle other database errors.
return "Database error: " . $e->getMessage();
$response['message'] = "Database error: " . $e->getMessage();
return $response;
}
}
}
......
......@@ -11,7 +11,7 @@
<body>
<div class="register-container">
<h1>Register</h1>
<form id="registerForm" method="post" action="../../../home.php">
<form id="registerForm" method="post" action="../../router.php">
<label for="userName">Name</label>
<input type="text" id="userName" name="userName" required>
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment