Example #1
0
$app->get('/', function () use($app) {
    // send back the authentication/documentation page
    $app->response->headers->set('Content-Type', 'text/html');
    $app->render('landing.php');
});
$app->group('/auth', function () use($app) {
    $app->post('/login', function () use($app) {
        $app->response->headers->set('Content-Type', 'application/json');
        $username = $app->request()->post('username');
        if (empty($username)) {
            $app->response->setStatus(400);
            $app->response->body('{"error" : "Provide a username"}');
            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);