Skip to content

Commit

Permalink
Merge pull request #27 from Mohammadreza-73/1.x
Browse files Browse the repository at this point in the history
[1.x] Implements login action
  • Loading branch information
Mohammadreza-73 authored Apr 29, 2023
2 parents dd70633 + 2ebeb93 commit e91f2f3
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 15 deletions.
14 changes: 10 additions & 4 deletions app/Core/Authenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ public function attemp(string $email, string $password)
$user = $this->db()->query("SELECT * FROM `users` WHERE `email` = :email", [
'email' => $email,
])->find();

if ($user) {
if (password_verify($password, $user->password)) {
$this->login([
'email' => $email,
// $this->login($user);
Session::put('user', [
'email' => $user->email,
]);

// Prevent Session Hijacking
session_regenerate_id(true);

return true;
}
Expand All @@ -27,7 +31,9 @@ public function login(array|object $user)
{
$user = is_object($user) ? $user : (object) $user;

Session::flash('email', $user->email);
Session::put('user', [
'email' => $user->email,
]);

// Prevent Session Hijacking
session_regenerate_id(true);
Expand Down
2 changes: 1 addition & 1 deletion app/Core/Http/Middleware/Authenticated.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Authenticated
public function handle()
{
if (! $_SESSION['user'] ?? false) {
header('location: /admin');
header('location: /');
exit();
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/Core/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Session
{
public static function put(string $key, string $value)
public static function put(string $key, $value)
{
$_SESSION[$key] = $value;
}
Expand All @@ -19,7 +19,7 @@ public static function has(string $key)
return (bool) static::get($key);
}

public static function flash(string $key, string $value)
public static function flash(string $key, $value)
{
$_SESSION['_flash'][$key] = $value;
}
Expand Down
7 changes: 7 additions & 0 deletions app/Core/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,10 @@ function session(string $key)
return Session::get($key);
}
}

if (! function_exists('old')) {
function old(string $key, $default = '')
{
return Session::get('old')[$key] ?? $default;
}
}
16 changes: 11 additions & 5 deletions app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@ public function verify()
{
$inputs = inputs();

// TODO: validation
$errors = [];
if (! Validator::email($inputs['email'])) {
$errors['email'] = 'Please provide a valid email address.';
}

if ($this->attemp($inputs['email'], $inputs['password'])) {
return redirect('/admin'); // Bug: to many redirects
if (! empty($errors)) {
return view('auth.login', compact('errors'));
}

} else {
return redirect('/login', 'error', 'Invalid email or password.');
if ($this->attemp($inputs['email'], $inputs['password'])) {
return redirect('/admin');
}

return redirect('/login', 'error', 'Invalid email or password.');
}

public function signup()
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public function index()

public function dashboard()
{
echo 'admin dashboard';
return view('dashboard');
}
}
2 changes: 1 addition & 1 deletion public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
require_once BASE_PATH . 'bootstrap/app.php';
require_once BASE_PATH . 'routes.php';

$router = (new App\Core\Http\Router)->run();
(new App\Core\Http\Router)->run();

Session::unflash();
2 changes: 1 addition & 1 deletion resources/Views/auth/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</head>
<body>
<div>
<h4><?= session('error') ?? session('succes') ?></h4>
<h4><?= session('errors') ?? session('succes') ?></h4>
</div>
<form action="<?= url('/verify') ?>" method="post">
<label for="email">Eamil: </label>
Expand Down
12 changes: 12 additions & 0 deletions resources/Views/dashboard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard</title>
</head>
<body>
<h1>Welcome to Dashboard</h1>
</body>
</html>

0 comments on commit e91f2f3

Please sign in to comment.