public static function generateToken($uid) { $u = new User($uid); $signer = new Sha256(); $token = (new Builder())->setIssuer('http://testify.com')->setAudience('http://testify.com')->setId('testify_token_user_' . $uid, true)->setIssuedAt(time())->setExpiration(time() + 60 * 60 * 24 * 7)->set('user_id', $uid)->set('hash_id', $u->getHashID())->sign($signer, 'sheyi')->getToken(); // Retrieves the generated token return $token; }
}); $app->post('/users/', function () use($app) { $body = $app->request->getBody(); $nu = json_decode($body); if ($uid = User::create($nu->firstName, $nu->lastName, $nu->email)) { $app->response()->status("201"); $token = App::generateToken($uid); echo json_encode(array('token' => "{$token}")); } else { $app->response()->status("401"); } }); $app->get('/users/:hash_id/', function ($hash_id) use($app) { try { $u = new User(Tools::decodeHashID("user", $hash_id)); echo json_encode(["user_id" => $u->getID(), "hash_id" => $u->getHashID(), "first_name" => $u->getFirstName(), "last_name" => $u->getLastName(), "email" => $u->getEmail(), "avatar" => $u->getProfilePictureURL()]); } catch (Exception $e) { $app->response->status(404); echo json_encode(["status" => "User not found"]); } }); $app->get('/users/:hash_id/posts', function ($hash_id) use($app) { $req = $app->request; $prms = []; if ($req->get("limit")) { $prms['limit'] = $req->get("limit"); } if ($req->get("offset") && $req->get("direction")) { $prms['offset'] = $req->get("offset"); $prms['direction'] = $req->get("direction"); }