[add] Added wrapper for login method

Also changed the logout endpoint to get
This commit is contained in:
Jose Pabl Domingo Aramburo Sanchez 2018-08-09 01:35:44 -06:00
parent 23868b60ee
commit 0449f202ef
2 changed files with 19 additions and 4 deletions

View File

@ -69,7 +69,7 @@ class SessionApplication{
// If the credentials don't match anything in the the records
if(!isset($storedPassword)){
throw new Exception('The user or password didnt match, please try again.');
return false;
}
// Already has a session
@ -82,7 +82,22 @@ class SessionApplication{
return true;
}
else{
return false;
throw new Exception('The user or password didnt match, please try again.');
}
}
/**
* @param $userName
* @param $password
* @return array
* @throws Exception
*/
function login($userName, $password){
if($this->newSession($userName, $password)){
return array('status' => 'success', 'message' => 'Logged in successfully.');
}
else{
throw new Exception('The user or password didnt match, please try again.');
}
}

View File

@ -22,14 +22,14 @@ $app->get('/api/session', function (Request $request, Response $response, array
$app->post('/api/session/login', function ($request, $response) {
$requestData = $request->getParsedBody();
$data = $this->sessionApplication->newSession($requestData['userName'], $requestData['password']);
$data = $this->sessionApplication->login($requestData['userName'], $requestData['password']);
return $response->withStatus(200)
->withHeader('Content-Type', 'application/json')
->write(json_encode($data));
});
$app->post('/api/session/logout', function (Request $request, Response $response, array $args) {
$app->get('/api/session/logout', function (Request $request, Response $response, array $args) {
return $response->withStatus(200)
->withHeader('Content-Type', 'application/json')
->write(json_encode($this->sessionApplication->destroySession()));