Initial commit
This commit is contained in:
34
app/Controllers/AuthController.php
Normal file
34
app/Controllers/AuthController.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?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']);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user