From 0449f202ef8619acb2a4d5401fee9e725e6b5e16 Mon Sep 17 00:00:00 2001 From: Jose Pabl Domingo Aramburo Sanchez Date: Thu, 9 Aug 2018 01:35:44 -0600 Subject: [PATCH] [add] Added wrapper for login method Also changed the logout endpoint to get --- .../src/application/SessionApplication.php | 19 +++++++++++++++++-- api-payroll/src/routes.php | 4 ++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/api-payroll/src/application/SessionApplication.php b/api-payroll/src/application/SessionApplication.php index e916970..62ff2fd 100644 --- a/api-payroll/src/application/SessionApplication.php +++ b/api-payroll/src/application/SessionApplication.php @@ -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.'); } } diff --git a/api-payroll/src/routes.php b/api-payroll/src/routes.php index 7d28e55..1bef8c0 100644 --- a/api-payroll/src/routes.php +++ b/api-payroll/src/routes.php @@ -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()));