Posts: 3
Blog 1: User_Authentication_Flow.md | (Open) | (Share)
▲ Previous |
Next ▼/Projects/dataDesk/Docs
(
Last modified: June 06 2025 23:21:24)
User Authentication Flow
User Authentication Flow
🔐 User Authentication Flow
1. Entry Point: index.php
When the user navigates to the sign-in page, the app is accessed via:
index.php/?page=signin
Route Handling:
switch ($route) {
case 'signin':
$force = $_GET['force_select'] ?? null;
if (isset($_SESSION['user_id']) && !$force) {
// User already signed in and not switching accounts
header("Location: ./");
exit;
}
require __DIR__ . '/app/auth/signin.html';
break;
}
- If the user is already signed in and not explicitly forcing account switch, they are redirected to the homepage.
- Otherwise, the
signin.html page is loaded.
2. Sign-In Form Submission: /app/auth/signin.html
JavaScript sends the form data via fetch:
fetch('./user.php?action=signin', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.status === "success") {
window.location.href = data.redirect;
}
});
- Sends a
POST request to user.php?action=signin
- If login is successful, the user is redirected to the given URL.
3. Login Handler: user.php
require 'config.php';
require 'includes/AuthManager.php';
$auth = new AuthManager($pdo);
$action = $_GET['action'] ?? '';
header('Content-Type: application/json');
try {
switch ($action) {
case 'signin':
$email = $_POST['email'] ?? '';
$password = $_POST['password'] ?? '';
$remember = isset($_POST['remember_me']);
echo json_encode($auth->login($email, $password, $remember));
break;
}
} catch (Exception $e) {
echo json_encode([
"status" => "error",
"message" => $e->getMessage()
]);
}
- Loads dependencies and initializes
AuthManager
- Handles
signin action by calling login() with form data
- Returns a JSON response indicating success or error
4. Authentication Logic: AuthManager.php
public function login($email, $password, $remember_me = false)
{
// Validate user and authenticate (code omitted)
$_SESSION['user_id'] = $user['id'];
if ($remember_me) {
setcookie("remember_token", $remember_token, [...]);
setcookie("remember_selector", $remember_selector, [...]);
}
return [
"status" => "success",
"message" => "Welcome, you are now signed in!",
"redirect" => $redirectUrl
];
}
- Authenticates the user and stores their ID in
$_SESSION
- If "Remember Me" is checked, persistent cookies are set
- Returns a JSON response with status and redirect URL
https://mermaid.live/
sequenceDiagram
participant U as User
participant B as Browser (index.php)
participant F as signin.html
participant S as user.php
participant A as AuthManager
U->>B: Request /?page=signin
B->>F: Load signin.html
F->>S: POST user.php?action=signin
S->>A: login(email, password, remember)
A-->>S: JSON (status, redirect)
S-->>F: JSON response
F->>U: Redirect to success page
