144 lines
4.7 KiB
PHP
144 lines
4.7 KiB
PHP
|
<?php
|
||
|
namespace api\user;
|
||
|
require_once __DIR__ . "/../utils/routesInterface.php";
|
||
|
require_once "userData.php";
|
||
|
|
||
|
use api\utils\routesInterface;
|
||
|
use Psr\Http\Message\ResponseInterface as Response;
|
||
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||
|
use Slim\App;
|
||
|
|
||
|
class userRoutes implements routesInterface
|
||
|
{
|
||
|
private userData $user;
|
||
|
public function __construct(App $app)
|
||
|
{
|
||
|
$this->user = new userData();
|
||
|
$this->createRoutes($app);
|
||
|
}
|
||
|
|
||
|
public function createRoutes(App $app): void
|
||
|
{
|
||
|
$app->post("/user/login", function (Request $request, Response $response)
|
||
|
{
|
||
|
// get request data
|
||
|
$data = $request->getParsedBody();
|
||
|
|
||
|
if (empty($data["username"]) || empty($data["password"]))
|
||
|
{
|
||
|
// uh oh userData sent empty data
|
||
|
return $response->withStatus(400);
|
||
|
}
|
||
|
|
||
|
if ($this->user->checkUser($data["username"], $data["password"]))
|
||
|
{
|
||
|
// yay, userData is logged in
|
||
|
$_SESSION["token"] = $this->user->createToken($data["username"]);
|
||
|
$_SESSION["username"] = $data["username"];
|
||
|
$response->getBody()->write(json_encode(array("token" => $_SESSION["token"])));
|
||
|
return $response;
|
||
|
}
|
||
|
$response->getBody()->write(json_encode(array("error" => "Unauthorised")));
|
||
|
return $response->withStatus(401);
|
||
|
});
|
||
|
|
||
|
$app->get("/user/logout", function (Request $request, Response $response)
|
||
|
{
|
||
|
session_unset();
|
||
|
return $response;
|
||
|
});
|
||
|
|
||
|
$app->get("/user/isLoggedIn", function (Request $request, Response $response)
|
||
|
{
|
||
|
if (empty($_SESSION["token"]) && empty($_SESSION["username"]))
|
||
|
{
|
||
|
// uh oh userData not logged in
|
||
|
return $response->withStatus(401);
|
||
|
}
|
||
|
|
||
|
if (empty($_SESSION["token"]))
|
||
|
{
|
||
|
// userData is logged in but no token was created
|
||
|
$_SESSION["token"] = $this->user->createToken($_SESSION["username"]);
|
||
|
return $response;
|
||
|
}
|
||
|
|
||
|
$response->getBody()->write(json_encode(array("token" => $_SESSION["token"])));
|
||
|
return $response;
|
||
|
|
||
|
});
|
||
|
|
||
|
$app->get("/user/checkResetEmail/{email}", function (Request $request, Response $response, array $args)
|
||
|
{
|
||
|
if (empty($args["email"]))
|
||
|
{
|
||
|
// uh oh sent empty data
|
||
|
return $response->withStatus(400);
|
||
|
}
|
||
|
|
||
|
if ($this->user->checkEmail($args["email"]))
|
||
|
{
|
||
|
// yay email does exist
|
||
|
$_SESSION["resetToken"] = $this->user->sendResetEmail($args["email"]);
|
||
|
$_SESSION["resetEmail"] = $args["email"];
|
||
|
return $response;
|
||
|
}
|
||
|
return $response->withStatus(404);
|
||
|
});
|
||
|
|
||
|
$app->get("/user/resendEmail", function (Request $request, Response $response)
|
||
|
{
|
||
|
if (empty($_SESSION["resetToken"]))
|
||
|
{
|
||
|
// uh oh not authorized to resend email
|
||
|
return $response->withStatus(401);
|
||
|
}
|
||
|
|
||
|
$_SESSION["resetToken"] = $this->user->sendResetEmail($_SESSION["resetEmail"]);
|
||
|
return $response;
|
||
|
});
|
||
|
|
||
|
$app->get("/user/checkResetCode/{code}", function (Request $request, Response $response, array $args)
|
||
|
{
|
||
|
if (empty($args["code"]))
|
||
|
{
|
||
|
// uh oh sent empty data
|
||
|
return $response->withStatus(400);
|
||
|
}
|
||
|
|
||
|
if ($_SESSION["resetToken"] === $args["code"])
|
||
|
{
|
||
|
// yay, code code matches
|
||
|
return $response;
|
||
|
}
|
||
|
|
||
|
return $response->withStatus(401);
|
||
|
});
|
||
|
|
||
|
$app->post("/user/changePassword", function (Request $request, Response $response)
|
||
|
{
|
||
|
if (empty($_SESSION["resetToken"]) && empty($_SESSION["resetEmail"]))
|
||
|
{
|
||
|
// uh oh not authorized to change password
|
||
|
return $response->withStatus(401);
|
||
|
}
|
||
|
|
||
|
$data = $request->getParsedBody();
|
||
|
if (empty($data["password"]))
|
||
|
{
|
||
|
// uh oh sent empty data
|
||
|
return $response->withStatus(400);
|
||
|
}
|
||
|
|
||
|
if ($this->user->changePassword($_SESSION["resetEmail"], $data["password"]))
|
||
|
{
|
||
|
// yay, password changed
|
||
|
unset($_SESSION["resetToken"]);
|
||
|
unset($_SESSION["resetEmail"]);
|
||
|
return $response;
|
||
|
}
|
||
|
|
||
|
return $response->withStatus(500);
|
||
|
});
|
||
|
}
|
||
|
}
|