public static function logout(Slim $app) { self::setContentType($app); $token = $app->request->headers->get('Authorization'); $userOperation = new UserOperations(); $userOperation->invalidateSession($token); self::$response['Authorization'] = null; self::$response->body(json_encode(['Status' => 200, 'Message' => 'Token successfully unset.'])); return self::$response; }
public function validate($username, $password) { $userOperation = new UserOperations(); try { $user = $userOperation->check('username', '=', $username); if ($user['password'] === $password) { $message = true; } else { $message = json_encode(['Error' => 'Invalid password.']); } } catch (DataNotFoundException $e) { $message = json_encode(['Error' => 'User account does not exist']); } return $message; }
use Dipo\NaijaEmojis\API\EmojiController; use Dipo\NaijaEmojis\Operations\UserOperations; use Dipo\NaijaEmojis\Exceptions\DataNotFoundException; /* Creates the Slim application instance */ $app = new Slim(['templates.path' => 'api-view/', 'debug' => true]); /* Prepares application view */ $app->view(new \Slim\Views\Twig()); $app->view->parserOptions = array('charset' => 'utf-8', 'cache' => realpath('templates/cache'), 'auto_reload' => true, 'strict_variables' => false, 'autoescape' => true); $app->view->parserExtensions = array(new \Slim\Views\TwigExtension()); /* Checks for authorization status */ $authenticator = function () use($app) { $response = $app->response(); $response->header("Content-type", "application/json"); $authorization = $app->request->headers->get('Authorization'); if (!is_null($authorization)) { $userOperation = new UserOperations(); try { $user = $userOperation->check('token', '=', $authorization); // Checks if token is still active if ($user['token_expire'] < date('Y-m-d H:i:s')) { $response->body(json_encode(['status' => 401, 'message' => 'You need authorization.'])); $response->status(401); $app->stop(); return $response; } $app->response->header('Authorization', $authorization); } catch (DataNotFoundException $e) { $response->body(json_encode(['status' => 401, 'message' => 'You need authorization.'])); $response->status(401); $app->stop(); return $response;