Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix user router + docs #84

Merged
merged 5 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/services/auth/auth-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,12 @@ authRouter.get("/roles/", strongJwtVerification, async (_: Request, res: Respons
});

/**
* @api {get} /auth/roles/list/:role GET /auth/roles/list/:role
* @api {get} /auth/roles/list/:ROLE GET /auth/roles/list/:ROLE
* @apiGroup Auth
* @apiDescription Get all users that have a certain role.
*
* @apiParam ROLE Role to get the user for. Roles: USER, APPLICANT, ATTENDEE, VOLUNTEER, STAFF, ADMIN, MENTOR, SPONSOR
*
* @apiSuccess (200: Success) {String[]} Array of ids of users w/ the specified role.
* @apiSuccessExample Example Success Response:
* HTTP/1.1 200 OK
Expand Down
2 changes: 1 addition & 1 deletion src/services/profile/profile-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ profileRouter.get("/", strongJwtVerification, async (_: Request, res: Response)
* @apiGroup Profile
* @apiDescription Retrieve the user's profile based on the provided ID as a path parameter.
*
* @apiParam {String} id User's unique ID.
* @apiParam {String} USERID User's unique ID.
*
* @apiSuccess (200: Success) {Json} user User's profile information.
* @apiSuccessExample Example Success Response:
Expand Down
14 changes: 10 additions & 4 deletions src/services/user/user-formats.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
export interface UserFormat {
id: string;
username: string;
firstname: string;
lastname: string;
userId: string;
name: string;
email: string;
}

export function isValidUserFormat(u: UserFormat): boolean {
if (typeof u.userId !== "string" || typeof u.name !== "string" || typeof u.email !== "string") {
return false;
}

return true;
}
60 changes: 26 additions & 34 deletions src/services/user/user-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { strongJwtVerification } from "../../middleware/verify-jwt.js";
import { JwtPayload } from "../auth/auth-models.js";
import { generateJwtToken, getJwtPayloadFromDB, hasElevatedPerms, hasStaffPerms } from "../auth/auth-lib.js";

import { UserFormat } from "./user-formats.js";
import { UserFormat, isValidUserFormat } from "./user-formats.js";
import { UserInfo, UserInfoModel } from "../../database/user-db.js";

const userRouter: Router = Router();
Expand All @@ -17,13 +17,13 @@ const userRouter: Router = Router();
* @apiDescription Get a QR code with a pre-defined expiration for the user provided in the JWT token. Since expiry is set to 20 seconds,
* we recommend that the results from this endpoint are not stored, but instead used immediately.
*
* @apiSuccess (200: Success) {String} id User to generate a QR code for
* @apiSuccess (200: Success) {String} userId User to generate a QR code for
* @apiSuccess (200: Success) {String} qrInfo Stringified QR code for the given user

* @apiSuccessExample Example Success Response:
* HTTP/1.1 200 OK
* {
* "id": "provider000001",
* "userId": "provider000001",
* "qrinfo": "hackillinois://user?userToken=loremipsumdolorsitamet"
* }
*
Expand All @@ -34,7 +34,7 @@ userRouter.get("/qr/", strongJwtVerification, (_: Request, res: Response) => {
const payload: JwtPayload = res.locals.payload as JwtPayload;
const token: string = generateJwtToken(payload, false, "20s");
const uri: string = `hackillinois://user?userToken=${token}`;
res.status(Constants.SUCCESS).send({ id: payload.id, qrInfo: uri });
res.status(Constants.SUCCESS).send({ userId: payload.id, qrInfo: uri });
});

/**
Expand All @@ -43,15 +43,15 @@ userRouter.get("/qr/", strongJwtVerification, (_: Request, res: Response) => {
* @apiDescription Get a QR code with a pre-defined expiration for a particular user, provided that the JWT token's user has elevated perms. Since expiry is set to 20 seconds,
* we recommend that the results from this endpoint are not stored, but instead used immediately.
*
* @apiParam {String} USERID to generate the QR code for.
* @apiParam {String} USERID Id to generate the QR code for.
*
* @apiSuccess (200: Success) {String} id User to generate a QR code for
* @apiSuccess (200: Success) {String} userId User to generate a QR code for
* @apiSuccess (200: Success) {String} qrInfo Stringified QR code for the user to be used

* @apiSuccessExample Example Success Response:
* HTTP/1.1 200 OK
* {
* "id": "provider000001",
* "userId": "provider000001",
* "qrinfo": "hackillinois://user?userToken=loremipsumdolorsitamet"
* }
*
Expand Down Expand Up @@ -86,7 +86,7 @@ userRouter.get("/qr/:USERID", strongJwtVerification, async (req: Request, res: R
// Generate the token
const token: string = generateJwtToken(newPayload, false, "20s");
const uri: string = `hackillinois://user?userToken=${token}`;
return res.status(Constants.SUCCESS).send({ id: payload.id, qrInfo: uri });
return res.status(Constants.SUCCESS).send({ userId: payload.id, qrInfo: uri });
});

/**
Expand All @@ -95,17 +95,15 @@ userRouter.get("/qr/:USERID", strongJwtVerification, async (req: Request, res: R
* @apiDescription Get user data for a particular user, provided that the JWT token's user has elevated perms.
* @apiParam {String} USERID to generate the QR code for.
*
* @apiSuccess (200: Success) {String} id UserID
* @apiSuccess (200: Success) {String} firstname User's first name.
* @apiSuccess (200: Success) {String} lastname User's last name.
* @apiSuccess (200: Success) {String} userId UserID
* @apiSuccess (200: Success) {String} name User's name.
* @apiSuccess (200: Success) {String} email Email address (staff gmail or Github email).

* @apiSuccessExample Example Success Response:
* HTTP/1.1 200 OK
* {
"id": "provider00001",
"firstname": "john",
"lastname": "doe",
"userId": "provider00001",
"name": "john doe",
"email": "johndoe@provider.com"
* }
*
Expand Down Expand Up @@ -141,17 +139,15 @@ userRouter.get("/:USERID", strongJwtVerification, async (req: Request, res: Resp
* @apiGroup User
* @apiDescription Get user data for the current user in the JWT token.
*
* @apiSuccess (200: Success) {String} id UserID
* @apiSuccess (200: Success) {String} firstname User's first name.
* @apiSuccess (200: Success) {String} lastname User's last name.
* @apiSuccess (200: Success) {String} userId UserID
* @apiSuccess (200: Success) {String} name User's name.
* @apiSuccess (200: Success) {String} email Email address (staff gmail or Github email).

* @apiSuccessExample Example Success Response:
* HTTP/1.1 200 OK
* {
"id": "provider00001",
"firstname": "john",
"lastname": "doe",
"userId": "provider00001",
"name": "john doe",
"email": "johndoe@provider.com"
* }
*
Expand All @@ -175,29 +171,25 @@ userRouter.get("/", strongJwtVerification, async (_: Request, res: Response) =>
* @apiGroup User
* @apiDescription Update a given user
*
* @apiBody {String} id UserID
* @apiBody {String} firstname User's first name.
* @apiBody {String} lastname User's last name.
* @apiBody {String} userId UserID
* @apiBody {String} name User's name.
* @apiBody {String} email Email address (staff gmail or Github email).
* @apiParamExample {json} Example Request:
* {
"id": "provider00001",
"firstname": "john",
"lastname": "doe",
"userId": "provider00001",
"name": "john doe",
"email": "johndoe@provider.com"
* }
*
* @apiSuccess (200: Success) {String} id UserID
* @apiSuccess (200: Success) {String} firstname User's first name.
* @apiSuccess (200: Success) {String} lastname User's last name.
* @apiSuccess (200: Success) {String} userId UserID
* @apiSuccess (200: Success) {String} name User's name.
* @apiSuccess (200: Success) {String} email Email address (staff gmail or Github email).

* @apiSuccessExample Example Success Response:
* HTTP/1.1 200 OK
* {
"id": "provider00001",
"firstname": "john",
"lastname": "doe",
"userId": "provider00001",
"name": "john",
"email": "johndoe@provider.com"
* }
* @apiUse strongVerifyErrors
Expand All @@ -212,13 +204,13 @@ userRouter.post("/", strongJwtVerification, async (req: Request, res: Response)
// Get userData from the request, and print to output
const userData: UserFormat = req.body as UserFormat;

if (!userData.id || !userData.email || !userData.firstname || !userData.lastname || !userData.username) {
if (!isValidUserFormat(userData)) {
return res.status(Constants.BAD_REQUEST).send({ error: "InvalidParams" });
}

// Update the given user
const updatedUser: UserInfo | null = await UserInfoModel.findOneAndUpdate(
{ userId: userData.id },
{ userId: userData.userId },
{ $set: userData },
{ upsert: true },
);
Expand Down
Loading