48 lines
1.2 KiB
PHP
Executable File
48 lines
1.2 KiB
PHP
Executable File
<?php
|
|
|
|
namespace app\models;
|
|
|
|
use app\core\AuthInterface;
|
|
use app\exceptions\GenericErrorException;
|
|
use app\exceptions\UserNotExistingException;
|
|
use Exception;
|
|
use mysqli;
|
|
|
|
class LoginModel implements AuthInterface
|
|
{
|
|
private ?mysqli $conn = NULL;
|
|
private array $config;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->config = $_SESSION['database_configuration'];
|
|
}
|
|
|
|
/**
|
|
* @throws GenericErrorException
|
|
*/
|
|
public function createConnection(): void
|
|
{
|
|
try {
|
|
$this->conn = new mysqli($this->config['dsn'], $this->config['user'], $this->config['password'], $this->config['dbname']);
|
|
} catch (Exception){
|
|
throw new GenericErrorException();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @throws UserNotExistingException
|
|
*/
|
|
public function executeReq(array $body): int|string
|
|
{
|
|
$email = $body["email"];
|
|
$password = $body["password"];
|
|
|
|
$query = "SELECT id FROM Users WHERE email = '$email' AND password = '$password'";
|
|
$result = $this->conn->query($query);
|
|
if(!$result->num_rows) throw new UserNotExistingException();
|
|
|
|
$row = mysqli_fetch_row($result);
|
|
return $row[0];
|
|
}
|
|
} |