154 lines
3.8 KiB
PHP
154 lines
3.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Modules\Auth;
|
||
|
|
|
||
|
|
use App\Core\Controller as BaseController;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Auth Controller
|
||
|
|
* Handles authentication
|
||
|
|
*/
|
||
|
|
class Controller extends BaseController
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Show login form
|
||
|
|
*/
|
||
|
|
public function showLogin()
|
||
|
|
{
|
||
|
|
return $this->view('Auth.view.login', [
|
||
|
|
'title' => 'Login - Woles Framework'
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle login
|
||
|
|
*/
|
||
|
|
public function login()
|
||
|
|
{
|
||
|
|
$data = $this->request()->all();
|
||
|
|
|
||
|
|
// Basic validation
|
||
|
|
$errors = $this->validate($data, [
|
||
|
|
'email' => 'required|email',
|
||
|
|
'password' => 'required|min:6'
|
||
|
|
]);
|
||
|
|
|
||
|
|
if (!empty($errors)) {
|
||
|
|
if ($this->request()->expectsJson()) {
|
||
|
|
return $this->error('Validation failed', 422);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->view('Auth.view.login', [
|
||
|
|
'title' => 'Login - NovaCore Framework',
|
||
|
|
'errors' => $errors,
|
||
|
|
'old' => $data
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Simple authentication (in production, use proper user model)
|
||
|
|
if ($data['email'] === 'admin@novacore.dev' && $data['password'] === 'password123') {
|
||
|
|
$_SESSION['auth'] = true;
|
||
|
|
$_SESSION['user'] = [
|
||
|
|
'id' => 1,
|
||
|
|
'email' => $data['email'],
|
||
|
|
'name' => 'Administrator'
|
||
|
|
];
|
||
|
|
|
||
|
|
if ($this->request()->expectsJson()) {
|
||
|
|
return $this->success(['user' => $_SESSION['user']], 'Login successful');
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->redirect('/dashboard');
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($this->request()->expectsJson()) {
|
||
|
|
return $this->error('Invalid credentials', 401);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->view('Auth.view.login', [
|
||
|
|
'title' => 'Login - NovaCore Framework',
|
||
|
|
'error' => 'Invalid email or password',
|
||
|
|
'old' => $data
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle logout
|
||
|
|
*/
|
||
|
|
public function logout()
|
||
|
|
{
|
||
|
|
session_destroy();
|
||
|
|
|
||
|
|
if ($this->request()->expectsJson()) {
|
||
|
|
return $this->success([], 'Logout successful');
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->redirect('/login');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Show registration form
|
||
|
|
*/
|
||
|
|
public function showRegister()
|
||
|
|
{
|
||
|
|
return $this->view('Auth.view.register', [
|
||
|
|
'title' => 'Register - NovaCore Framework'
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle registration
|
||
|
|
*/
|
||
|
|
public function register()
|
||
|
|
{
|
||
|
|
$data = $this->request()->all();
|
||
|
|
|
||
|
|
// Basic validation
|
||
|
|
$errors = $this->validate($data, [
|
||
|
|
'name' => 'required|min:2',
|
||
|
|
'email' => 'required|email',
|
||
|
|
'password' => 'required|min:6',
|
||
|
|
'password_confirmation' => 'required'
|
||
|
|
]);
|
||
|
|
|
||
|
|
if ($data['password'] !== $data['password_confirmation']) {
|
||
|
|
$errors['password_confirmation'] = 'Password confirmation does not match.';
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!empty($errors)) {
|
||
|
|
if ($this->request()->expectsJson()) {
|
||
|
|
return $this->error('Validation failed', 422);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->view('Auth.view.register', [
|
||
|
|
'title' => 'Register - NovaCore Framework',
|
||
|
|
'errors' => $errors,
|
||
|
|
'old' => $data
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
// In production, save to database
|
||
|
|
// For now, just redirect to login
|
||
|
|
if ($this->request()->expectsJson()) {
|
||
|
|
return $this->success([], 'Registration successful');
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->redirect('/login');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Show dashboard
|
||
|
|
*/
|
||
|
|
public function dashboard()
|
||
|
|
{
|
||
|
|
if (!isset($_SESSION['auth']) || !$_SESSION['auth']) {
|
||
|
|
return $this->redirect('/login');
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->view('Auth.view.dashboard', [
|
||
|
|
'title' => 'Dashboard - NovaCore Framework',
|
||
|
|
'user' => $_SESSION['user']
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|