-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/challenges question specific validation (#150)
* feat: add getSeasonQuestion function * fix: return correct itemcount and pagecount when generating pagination metadata * test: add test file for pagination and getSeasonQuestion * fix: modify some function to use cont-serv-repo architecture * fix: add get season question endpoint * feat: add ability to do custom validation * chore: remove unused ranking model * feat: add validation function and oauth signin * feat: add jwt middleware * fix: add oauth sign in
- Loading branch information
1 parent
f9bac61
commit 72df095
Showing
33 changed files
with
898 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import asyncHandler from "express-async-handler"; | ||
import { Request, Response } from "express"; | ||
import { z } from "zod"; | ||
import AuthService from "../service/authService"; | ||
import { accessTokenMaxAgeSeconds, refreshCookieMaxAgeSeconds, secondInMilliseconds } from "../model/constants"; | ||
|
||
|
||
const oauthSignIn = asyncHandler(async (req: Request, res: Response) => { | ||
const { access_token } = req.body; | ||
|
||
try { | ||
const { accessToken, refreshToken, createNewUser } = await AuthService.oauthSignIn(access_token); | ||
res.status(createNewUser ? 200 : 201).json({ | ||
"access_token": accessToken, | ||
"refresh_token": refreshToken | ||
}) | ||
} catch (error) { | ||
console.log("AuthService.oauthSignIn", error); | ||
res.status(500).json({ message: 'Internal Server Error' }); | ||
} | ||
}); | ||
|
||
const refreshToken = asyncHandler(async (req: Request, res: Response) => { | ||
try { | ||
const userID = req.params.userID; | ||
const token = await AuthService.refreshToken(userID); | ||
res.status(200).json(token); | ||
} catch (err) { | ||
console.error(err); | ||
res.status(500).json({ message: "Internal Server Error" }) | ||
} | ||
}) | ||
|
||
const AuthController = { | ||
oauthSignIn, | ||
refreshToken, | ||
} | ||
|
||
export { AuthController as default }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,34 @@ | ||
import UserService from "../service/userService"; | ||
import asyncHandler from "express-async-handler"; | ||
import { Request, Response } from "express"; | ||
import { StatusCodeError } from "../types/types"; | ||
|
||
const createUser = asyncHandler(async (req: Request, res: Response) => { | ||
const { name, email } = req.body; | ||
const getUser = asyncHandler(async (req: Request, res: Response) => { | ||
const { userID } = req.params; | ||
|
||
try { | ||
const user = await UserService.createUser(name, email); | ||
res.status(201).json(user); | ||
} catch (error) { | ||
res.status(500).json({ message: 'Internal Server Error' }); | ||
const user = await UserService.getUserByID(userID); | ||
res.status(200).json(user); | ||
} catch (err) { | ||
if (err instanceof StatusCodeError) { | ||
res.status(err.status).json({ message: err.message }); | ||
} else { | ||
res.status(500).json({ message: "Internal Server Error" }) | ||
} | ||
|
||
} | ||
}); | ||
|
||
const checkTokens = asyncHandler(async (req: Request, res: Response) => { | ||
const token = req.signedCookies; | ||
|
||
console.log(token); | ||
|
||
res.status(200); | ||
}); | ||
const UserController = { | ||
createUser | ||
getUser, | ||
checkTokens, | ||
} | ||
|
||
export { UserController as default }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// jwt middleware for express | ||
import jwt from "jsonwebtoken"; | ||
import { Request, Response, NextFunction } from "express"; | ||
|
||
const jwtMiddleware = (req: Request, res: Response, next: NextFunction) => { | ||
const token = req.signedCookies.access_token; | ||
|
||
if (token == null) { | ||
return res.sendStatus(401); | ||
} | ||
|
||
jwt.verify(token, process.env.JWT_SECRET || "", (err, tokenContent: any) => { | ||
if (err) { | ||
return res.sendStatus(401); | ||
} | ||
|
||
req.params.userID = tokenContent.id; | ||
req.params.email = tokenContent.email; | ||
|
||
|
||
next(); | ||
}); | ||
} | ||
|
||
export default jwtMiddleware; |
Oops, something went wrong.