Initial Commit
This commit is contained in:
59
core/Auth.php
Normal file
59
core/Auth.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Core;
|
||||
|
||||
class Auth
|
||||
{
|
||||
/**
|
||||
* Check if user is authenticated
|
||||
*/
|
||||
public static function check(): bool
|
||||
{
|
||||
return isset($_SESSION['user_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current user ID
|
||||
*/
|
||||
public static function id(): ?int
|
||||
{
|
||||
return $_SESSION['user_id'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current username
|
||||
*/
|
||||
public static function username(): ?string
|
||||
{
|
||||
return $_SESSION['username'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current user's full name
|
||||
*/
|
||||
public static function fullName(): ?string
|
||||
{
|
||||
return $_SESSION['full_name'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Require authentication (redirect to login if not authenticated)
|
||||
*/
|
||||
public static function require(): void
|
||||
{
|
||||
// Get current path
|
||||
$currentPath = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
|
||||
|
||||
// Don't redirect if already on login page or logout
|
||||
if ($currentPath === '/login' || $currentPath === '/logout') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!self::check()) {
|
||||
$_SESSION['error'] = 'Please login to continue';
|
||||
header('Location: /login');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user