Skip to content

Commit

Permalink
Changed Autoload PSR-4
Browse files Browse the repository at this point in the history
  • Loading branch information
riculum committed May 21, 2022
1 parent 0cc03ab commit 31e249f
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 38 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"autoload": {
"psr-4": {
"Auth\\" : "./"
"Riculum\\Auth\\" : "src/"
}
},
"require": {
Expand Down
2 changes: 2 additions & 0 deletions setup/database.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
CREATE TABLE IF NOT EXISTS user (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
uuid VARCHAR(50) NOT NULL UNIQUE,
gender ENUM('male', 'female') NOT NULL,
firstname VARCHAR(50) NOT NULL,
lastname VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
token VARCHAR(255) NOT NULL,
resetToken VARCHAR(255) NOT NULL,
attempts TINYINT NOT NULL DEFAULT 0,
online TINYINT NOT NULL DEFAULT 0,
verified TINYINT NOT NULL DEFAULT 0,
Expand Down
18 changes: 11 additions & 7 deletions core/Authentication.php → src/Authentication.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php
namespace Auth\Core;
namespace Riculum\Auth;

use Auth\Exceptions\InvalidEmailException;
use Auth\Exceptions\InvalidPasswordException;
use Auth\Exceptions\TooManyAttemptsException;
use Auth\Exceptions\UserAlreadyExistsException;
use Auth\Exceptions\UserNotEnabledException;
use Riculum\Auth\core\Session;
use Riculum\Auth\core\User;
use Riculum\Auth\exceptions\InvalidEmailException;
use Riculum\Auth\exceptions\InvalidPasswordException;
use Riculum\Auth\exceptions\TooManyAttemptsException;
use Riculum\Auth\exceptions\UserAlreadyExistsException;
use Riculum\Auth\exceptions\UserNotEnabledException;

class Authentication
{
Expand Down Expand Up @@ -47,11 +49,13 @@ static function login(string $email, string $password): bool
return true;
}

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

return true;
}

static function verify(): bool
Expand Down
2 changes: 1 addition & 1 deletion core/Session.php → src/core/Session.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Auth\Core;
namespace Riculum\Auth\core;

if (session_status() == PHP_SESSION_NONE) {
session_start();
Expand Down
26 changes: 5 additions & 21 deletions core/User.php → src/core/User.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
namespace Auth\Core;
namespace Riculum\Auth\core;

use Auth\Exceptions\UserAlreadyExistsException;
use Database\Core\Database as DB;
use Exception;
use Riculum\Auth\exceptions\UserAlreadyExistsException;
use Riculum\Database as DB;

class User
{
Expand All @@ -18,7 +18,7 @@ private static function emailIsUnique(string $email): bool
}

/**
* Output the 36 character UUID
* Output the 36 character UUIDv4
* @throws Exception
*/
private static function generateUuid(): string
Expand Down Expand Up @@ -76,21 +76,13 @@ static function addUser(array $user): ?int
{
if (self::emailIsUnique($user['email'])) {
$user['uuid'] = self::generateUuid();
$user['resetToken'] = bin2hex(random_bytes(16));
return DB::insertAssoc($_ENV['DB_PREFIX'].'user', $user);
} else {
throw new UserAlreadyExistsException('User with email ' . $user['email'] . ' already exists');
}
}

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

/**
* @param string $uuid
* @return array|null
Expand All @@ -100,14 +92,6 @@ static function getUser(string $uuid): ?array
return DB::single('SELECT * FROM ' . $_ENV['DB_PREFIX'] . 'user WHERE uuid = ?', [$uuid]);
}

/**
* @return array|null
*/
static function getUsers(): ?array
{
return DB::select('SELECT * FROM ' . $_ENV['DB_PREFIX'] . 'user', []);
}

/**
* @param string $email
* @return array|null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
namespace Auth\Exceptions;
namespace Riculum\Auth\exceptions;

class ForbiddenException extends \Exception {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
namespace Auth\Exceptions;
namespace Riculum\Auth\exceptions;

class InvalidEmailException extends \Exception {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
namespace Auth\Exceptions;
namespace Riculum\Auth\exceptions;

class InvalidPasswordException extends \Exception {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
namespace Auth\Exceptions;
namespace Riculum\Auth\exceptions;

class InvalidTokenException extends \Exception {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
namespace Auth\Exceptions;
namespace Riculum\Auth\exceptions;

class NotFoundException extends \Exception {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
namespace Auth\Exceptions;
namespace Riculum\Auth\exceptions;

class TooManyAttemptsException extends \Exception {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
namespace Auth\Exceptions;
namespace Riculum\Auth\exceptions;

class UserAlreadyExistsException extends \Exception {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
namespace Auth\Exceptions;
namespace Riculum\Auth\exceptions;

class UserNotEnabledException extends \Exception {}

0 comments on commit 31e249f

Please sign in to comment.