/** * Disable issued token to a user * * @param int $user_id ID of a user * @param Slim $app * @return string */ public static function logout($user_id, Slim $app) { $app->response->headers->set('Content-Type', 'application/json'); try { $user = User::find($user_id); } catch (RecordNotFoundException $e) { $app->halt(404, json_encode(['message' => 'Not Registered'])); } if (Setup::unsetToken($user) === 1) { return json_encode(['message' => 'Logged out']); } else { $app->halt3(503); } }
/** * Validate token * * @param Slim $app * @return bool */ public static function validateToken(Slim $app) { Setup::setTimezone(); $user = Setup::getUserWithToken($app); $tokenExpire = $user->getRecord()['dbData']['token_expire']; $timeNow = new DateTime(); $expiryTime = new DateTime($tokenExpire); if ($timeNow->getTimestamp() < $expiryTime->getTimestamp()) { return true; } else { Setup::unsetToken($user); $app->response->headers->set('Content-Type', 'application/json'); $app->halt(401, json_encode(['message' => 'Expired Token'])); } }