/** * This method will check the HTTP request for authentication. * If the request is authenticated, the next middleware is called. * Otherwise, a 401 response is returned to the client. */ public function call() { $req = $this->app->request(); $app = $this->app; if ($req->isGet()) { $this->next->call(); return; } if ($req->isPost() || $req->isPatch() || $req->isPut() || $req->isDelete()) { if ($req->getResourceUri() == '/auth/login') { $this->next->call(); return; } $token = $req->headers->get('token'); if (AuthController::authenticateToken($token)) { $this->next->call(); } else { $app->response->status(401); $app->response->headers->set('Content-Type', 'application/json'); $app->response->body('{"error" : "Not Authorized"}'); return $app->response(); } } }
} // login the user and return auth token $json = Controllers\AuthController::login($username); $app->response->body($json); return $app->response(); }); $app->get('/logout', function () use($app) { $app->response->headers->set('Content-Type', 'application/json'); // Delete auth token from DB. $token = $app->request->headers->get('token'); if (empty($token)) { $app->response->setStatus(400); $app->response->body('{"error" : "Provide a token to remove"}'); return $app->response(); } $prompt = Controllers\AuthController::logout($token); if (!$prompt) { $app->response->setStatus(400); $app->response->body('{"error" : "Invalid token"}'); return $app->response(); } $app->response->body('{"success" : "Logged out successfuly"}'); return $app->response(); }); }); $app->group('/emojis', function () use($app) { $app->response->headers->set('Content-Type', 'application/json'); // Get an emoji with ID $app->get('/', function () use($app) { $emoji = Controllers\EmojiController::get(); if (empty($emoji)) {