1
0
mirror of https://koodu.h-i.works/projects/thebadspace synced 2025-06-25 16:04:37 -05:00

Auth Framework, Part 1

Started buildig the authorization infrastructure to 1. create the
initial admin class to then 2. the Auth manager class can be created to
manage access based on roles.

added number of template files as well just as a UI base to get things
started.

Auth Framework Part 2 will complete the Auth mangager and clean up the
admin area.
This commit is contained in:
Ro
2022-12-12 17:42:40 -08:00
parent 74ae426275
commit 54b5227a0d
23 changed files with 3567 additions and 4 deletions

97
src/Service/Auth.php Normal file
View File

@ -0,0 +1,97 @@
<?php
// src/Controller/ProductController.php
namespace App\Service;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Entity\Members;
use ReallySimpleJWT\Token;
class Auth
{
private $session;
private $entityManager;
public function __construct(
EntityManagerInterface $entityManager,
RequestStack $requestStack
) {
$this->entityManager = $entityManager;
$this->session = $requestStack->getSession();
$this->secret = '!$ec7eT$l0w*';
}
public function authCheck($email, $password)
{
$response = [];
$member = new Members();
$members = $this->entityManager->getRepository(Members::class);
$member = $members->findOneBy(["email" => $email]);
if (!$member) {
$response = ["status" => false, "message" => "Member Not Found"];
} else {
if (!password_verify($password, $member->getPassword())) {
$response = ["status" => false, "message" => "Check that password"];
} else {
$this->session->set("member", $member);
$secret = $this->secret;
$expiration = time() + 3600;
$token = Token::create(
$member->getMemberId(),
$secret,
$expiration,
"nile_admin"
);
$this->session->set("token", $token);
$response = ["status" => true, "message" => "Welcome Back"];
}
}
return $response;
}
public function logOut()
{
$this->session->set("member", null);
$this->session->set("token", null);
}
public function APIStatus()
{
$response = [];
$verify = Token::validate($this->session->get("token"), $this->secret);
if ($verify) {
$response = [
"status" => true,
"message" => "Token is good",
"token" => $this->session->get("token"),
];
} else {
$response = ["status" => false, "message" => "Bad Token, champ."];
}
return $response;
}
public function status()
{
$response = [];
if ($this->session->get("member")) {
//$member = $this->session->get("member");
$response = [
"status" => true,
"role" => $this->session->get("member")->getRole(),
"token" => $this->session->get("token"),
];
} else {
$response = ["status" => false, "role" => null];
}
return $response;
}
}