Skip to content

Commit

Permalink
create an api controller for sessions endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed Apr 20, 2024
1 parent 0bf753c commit b2f15db
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/controllers/api/Sessions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Steamy\Controller\API;

use Steamy\Model\Administrator;

/**
* Handles /sessions route of API
*/
class Sessions
{
private function handleLogin(): void
{
$email = trim($_POST['email'] ?? "");
$password = trim($_POST['password'] ?? "");

if (empty($email) || empty($password)) {
http_response_code(400);
die();
}

// fetch administrator account
$admin = Administrator::getByEmail($email);

// validate email
if (!$admin) {
http_response_code(401);
die();
}

// validate password
if (!$admin->verifyPassword($password)) {
http_response_code(401);
die();
}

$_SESSION['admin_email'] = $email;
session_regenerate_id();
}

public function index(): void
{
switch ($_SERVER['REQUEST_METHOD']) {
case 'POST':
$this->handleLogin();
break;
default:
http_response_code(400);
die();
}
}
}

0 comments on commit b2f15db

Please sign in to comment.