Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
riculum committed Apr 16, 2022
1 parent 5155349 commit a8abdd0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
8 changes: 4 additions & 4 deletions core/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ static function login(string $email, string $password): bool

static function logout()
{
if (!empty(Session::getUserId())) {
if (!empty(Session::getUserUUID())) {
User::logout();
}
}

static function verify(): bool
{
$sessionToken = Session::getUserToken();
$sessionUserId = Session::getUserId();
$sessionUserUUID = Session::getUserUUID();

if (empty($sessionUserId) || empty($sessionToken)) {
if (empty($sessionUserUUID) || empty($sessionToken)) {
return false;
}

$userToken = User::getUser($sessionUserId)['token'];
$userToken = User::getUser($sessionUserUUID)['token'];

if (empty($userToken)) {
return false;
Expand Down
11 changes: 5 additions & 6 deletions core/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ static function getUserToken(): ?string
}

/**
* @return int|null
* @return string|null
*/
static function getUserId(): ?int
public static function getUserUUID(): ?string
{
return $_SESSION['userId'] ?? null;
return $_SESSION['userUUID'] ?? null;
}

/**
Expand All @@ -36,9 +36,8 @@ static function setUserToken(string $token)
$_SESSION['userToken'] = $token;
}


static function setUserId(int $id)
static function setUserUUID(int $uuid)
{
$_SESSION['userId'] = $id;
$_SESSION['userUUID'] = $uuid;
}
}
30 changes: 17 additions & 13 deletions core/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private static function generateUuid(): string
/**
* Login current user and set variables
*/
static function login(int $id)
static function login(string $uuid)
{
$token = md5(uniqid(rand(), true));

Expand All @@ -46,9 +46,9 @@ static function login(int $id)
"online" => 1
];

self::setUser($id, $user);
self::setUser($uuid, $user);

Session::setUserId($id);
Session::setUserUUID($uuid);
Session::setUserToken($token);
}

Expand All @@ -61,7 +61,7 @@ static function logout()
"token" => md5(uniqid(rand(), true)),
"online" => 0
];
self::setUser(Session::getUserId(), $user);
self::setUser(Session::getUserUUID(), $user);

Session::destroySession();
}
Expand All @@ -82,18 +82,22 @@ static function addUser(array $user): ?int
}
}

static function deleteUser(int $id)
/**
* @param string $uuid
* @return void
*/
static function deleteUser(string $uuid)
{
DB::delete("DELETE FROM " . $_ENV['DB_PREFIX'] . "user WHERE id = ?", [$id]);
DB::delete("DELETE FROM " . $_ENV['DB_PREFIX'] . "user WHERE uuid = ?", [$uuid]);
}

/**
* @param int $id
* @param int $uuid
* @return array|null
*/
static function getUser(int $id): ?array
static function getUser(string $uuid): ?array
{
return DB::single('SELECT * FROM ' . $_ENV['DB_PREFIX'] . 'user WHERE id = ?', [$id]);
return DB::single('SELECT * FROM ' . $_ENV['DB_PREFIX'] . 'user WHERE uuid = ?', [$uuid]);
}

static function getUserByEmail(string $email): ?array
Expand All @@ -102,16 +106,16 @@ static function getUserByEmail(string $email): ?array
}

/**
* @param int $id
* @param string $uuid
* @param array $user
* @return void
*/
static function setUser(int $id, array $user)
static function setUser(string $uuid, array $user)
{
$condition = [
"key" => "id",
"key" => "uuid",
"operator" => "=",
"value" => $id
"value" => $uuid
];

DB::updateAssoc($_ENV['DB_PREFIX'].'user', $user, $condition);
Expand Down

0 comments on commit a8abdd0

Please sign in to comment.