Implemented InputReader
This commit is contained in:
parent
4202ae2ec7
commit
45e32c4e50
3 changed files with 23 additions and 4 deletions
|
@ -13,13 +13,23 @@ final class AuthController extends AbstractController
|
|||
public function registerRoutes(\Szurubooru\Router $router)
|
||||
{
|
||||
$router->post('/api/login', [$this, 'login']);
|
||||
$router->get('/api/login', [$this, 'login']);
|
||||
$router->put('/api/login', [$this, 'login']);
|
||||
}
|
||||
|
||||
public function login()
|
||||
{
|
||||
$input = new \Szurubooru\Helpers\InputReader();
|
||||
$this->authService->loginFromCredentials($input->userName, $input->password);
|
||||
return $this->authService->getLoginToken();
|
||||
|
||||
if ($input->userName and $input->password)
|
||||
$this->authService->loginFromCredentials($input->userName, $input->password);
|
||||
elseif ($input->token)
|
||||
$this->authService->loginFromToken($input->token);
|
||||
else
|
||||
throw new \Szurubooru\MissingArgumentException();
|
||||
|
||||
return [
|
||||
'token' => $this->authService->getLoginToken(),
|
||||
'user' => $this->authService->getLoggedInUser()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,15 @@ final class InputReader
|
|||
{
|
||||
public function __construct()
|
||||
{
|
||||
$_PUT = [];
|
||||
if ($_SERVER['REQUEST_METHOD'] == 'PUT')
|
||||
parse_str(file_get_contents('php://input'), $_PUT);
|
||||
|
||||
foreach ([$_GET, $_POST, $_PUT] as $source)
|
||||
{
|
||||
foreach ($source as $key => $value)
|
||||
$this->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
public function __get($key)
|
||||
|
|
|
@ -55,7 +55,7 @@ final class AuthService
|
|||
{
|
||||
$loginToken = $this->tokenDao->getByName($loginTokenName);
|
||||
if (!$loginToken)
|
||||
throw new \Exception('Error while logging in (invalid token.)');
|
||||
throw new \Exception('Invalid login token.');
|
||||
|
||||
$this->loginToken = $loginToken;
|
||||
$this->loggedInUser = $this->userDao->getById($loginToken->additionalData);
|
||||
|
|
Loading…
Reference in a new issue