34 lines
852 B
PHP
34 lines
852 B
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Models\User;
|
|
use App\Core\Auth;
|
|
use App\Core\Validator;
|
|
use App\Core\Controller;
|
|
|
|
class AuthController extends Controller {
|
|
public function login() {
|
|
$errors = Validator::validate($_POST, [
|
|
'email' => 'required|email',
|
|
'password' => 'required'
|
|
]);
|
|
|
|
if (!empty($errors)) {
|
|
return $this->render('auth/login', ['errors' => $errors]);
|
|
}
|
|
|
|
// Check database for user
|
|
$user = User::findByEmail($_POST['email']); // Custom method in User model
|
|
|
|
if ($user && password_verify($_POST['password'], $user['password'])) {
|
|
Auth::login($user);
|
|
header('Location: /dashboard');
|
|
} else {
|
|
return $this->render('auth/login', ['error' => 'Invalid credentials']);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
?>
|