0% found this document useful (0 votes)
10 views5 pages

User Authentication and Dashboard System

The document contains a PHP web application with user authentication and task management features. It includes controllers for user and authentication actions, models for user and task data handling, and views for user interfaces such as login, signup, dashboard, and profile. The routing configuration connects various endpoints to their respective controllers and methods for handling requests.

Uploaded by

pahega8852
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

User Authentication and Dashboard System

The document contains a PHP web application with user authentication and task management features. It includes controllers for user and authentication actions, models for user and task data handling, and views for user interfaces such as login, signup, dashboard, and profile. The routing configuration connects various endpoints to their respective controllers and methods for handling requests.

Uploaded by

pahega8852
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UserController.

php

<?php
class UserController extends BaseController {
public function dashboard() {
$this->requireLogin();
$taskModel = new Task();
$tasks = $taskModel->getTasksByUser($_SESSION['user_id']);
$this->render('user/dashboard', ['tasks' => $tasks]);
}

public function profile() {


$this->requireLogin();
$userModel = new User();
$user = $userModel->findById($_SESSION['user_id']);
$this->render('user/profile', ['user' => $user]);
}
}

---

[Link]

<?php
class AuthController extends BaseController {
public function login() {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = $_POST['email'];
$password = $_POST['password'];

$userModel = new User();


$user = $userModel->findByEmail($email);

if ($user && password_verify($password, $user['password'])) {


$_SESSION['user_id'] = $user['id'];
$_SESSION['role'] = $user['role'];
if ($user['role'] === 'admin') {
header('Location: /admin/dashboard');
} else if ($user['role'] === 'org') {
header('Location: /org/dashboard');
} else {
header('Location: /user/dashboard');
}
exit;
} else {
$error = "Invalid email or password";
$this->render('auth/login', ['error' => $error]);
return;
}
}

$this->render('auth/login');
}

public function signup() {


if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
$password = password_hash($_POST['password'], PASSWORD_BCRYPT);

$userModel = new User();


$userModel->create($name, $email, $password);

header('Location: /login');
exit;
}

$this->render('auth/signup');
}

public function logout() {


session_destroy();
header('Location: /login');
exit;
}
}

---

[Link] (Model)

<?php
class User extends BaseModel {
protected $table = 'users';
public function create($name, $email, $password, $role = 'user') {
$stmt = $this->db->prepare("INSERT INTO users (name, email, password, role) VALUES
(?, ?, ?, ?)");
return $stmt->execute([$name, $email, $password, $role]);
}

public function findByEmail($email) {


$stmt = $this->db->prepare("SELECT * FROM users WHERE email = ?");
$stmt->execute([$email]);
return $stmt->fetch();
}

public function findById($id) {


$stmt = $this->db->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$id]);
return $stmt->fetch();
}
}

---

[Link] (Model)

<?php
class Task extends BaseModel {
protected $table = 'tasks';

public function create($user_id, $title, $description) {


$stmt = $this->db->prepare("INSERT INTO tasks (user_id, title, description) VALUES (?, ?,
?)");
return $stmt->execute([$user_id, $title, $description]);
}

public function getTasksByUser($user_id) {


$stmt = $this->db->prepare("SELECT * FROM tasks WHERE user_id = ?");
$stmt->execute([$user_id]);
return $stmt->fetchAll();
}
}

---
views/user/[Link]

<h1>User Dashboard</h1>
<p>Welcome to your dashboard!</p>
<ul>
<?php foreach ($tasks as $task): ?>
<li><?php echo htmlspecialchars($task['title']); ?> - <?php echo
htmlspecialchars($task['description']); ?></li>
<?php endforeach; ?>
</ul>

---

views/user/[Link]

<h1>User Profile</h1>
<p>Name: <?php echo htmlspecialchars($user['name']); ?></p>
<p>Email: <?php echo htmlspecialchars($user['email']); ?></p>

---

views/auth/[Link]

<h1>Login</h1>
<?php if (!empty($error)): ?>
<p style="color:red;"><?php echo htmlspecialchars($error); ?></p>
<?php endif; ?>
<form method="POST" action="/login">
<input type="email" name="email" placeholder="Email" required>
<input type="password" name="password" placeholder="Password" required>
<button type="submit">Login</button>
</form>

---

views/auth/[Link]

<h1>Signup</h1>
<form method="POST" action="/signup">
<input type="text" name="name" placeholder="Name" required>
<input type="email" name="email" placeholder="Email" required>
<input type="password" name="password" placeholder="Password" required>
<button type="submit">Signup</button>
</form>

---

[Link]

<?php
$router->get('/', 'HomeController@index');

$router->get('/login', 'AuthController@login');
$router->post('/login', 'AuthController@login');
$router->get('/signup', 'AuthController@signup');
$router->post('/signup', 'AuthController@signup');
$router->get('/logout', 'AuthController@logout');

$router->get('/user/dashboard', 'UserController@dashboard');
$router->get('/user/profile', 'UserController@profile');

$router->get('/admin/dashboard', 'AdminController@dashboard');
$router->get('/org/dashboard', 'OrgController@dashboard');

You might also like