-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create an api controller for sessions endpoint
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |