$app->post('/auth/login', function () use($app) { $username = $app->request->params('username'); $password = $app->request->params('password'); $user = new User(); $login = $user->login($username, $password); echo json_encode($login); }); $app->post('/auth/register', function () use($app) { $user = new User(); $username = $app->request->params('username'); $password = $app->request->params('password'); $register = $user->register($username, $password); echo json_encode($register); }); $app->get('/auth/logout', $authCheck, function () use($app) { $user = new User(); $userToken = $app->request->headers['token']; $logout = $user->logout($userToken); echo json_encode($logout); }); $app->get('/emojis', function () { echo json_encode(Emoji::getAll()); }); $app->post('/emojis', $authCheck, function () use($app) { $emoji = new Emoji(); echo json_encode($emoji->post($app)); }); $app->get('/emojis/:id', function ($id) { $emoji = Emoji::find($id); echo json_encode($emoji->resultRows); });
/** * Log user out * * @param string $username * @param string $password * @return array */ public function logout($token) { $user = $this->where('users', 'token', $token); $currentUser = User::find($user['id']); $currentUser->logged_in = false; $currentUser->token = false; $currentUser->token_expiry = false; return $currentUser->save() ? ["msg" => "You have been logged out"] : ["msg" => "An error occurred. Try logging out again"]; }