-
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.
- Loading branch information
1 parent
bf0839b
commit 1abb379
Showing
18 changed files
with
262 additions
and
37 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
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
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,38 @@ | ||
// jwt middleware for express | ||
import jwt from "jsonwebtoken"; | ||
import { Request, Response, NextFunction } from "express"; | ||
import TokenRepo from "../repo/tokenRepo"; | ||
import { refreshCookieMaxAgeSeconds, secondInMilliseconds } from "../model/constants"; | ||
|
||
const jwtRefreshMiddleware = (req: Request, res: Response, next: NextFunction) => { | ||
const token = req.signedCookies.refreshToken; | ||
|
||
if (token == null) { | ||
return res.sendStatus(401); | ||
} | ||
|
||
jwt.verify(token, process.env.JWT_SECRET || "", (err, tokenContent: any) => { | ||
if (err) { | ||
return res.sendStatus(401); | ||
} | ||
|
||
const token = TokenRepo.extendRefreshToken(tokenContent.id) | ||
if (token == null) { | ||
res.status(401).json({message: "Invalid refresh token"}) | ||
return; | ||
} | ||
|
||
res.cookie('refresh_token', token, { | ||
httpOnly: true, | ||
maxAge: refreshCookieMaxAgeSeconds * secondInMilliseconds, | ||
signed: true, | ||
}); | ||
|
||
req.params.userID = tokenContent.id; | ||
req.params.email = tokenContent.email; | ||
|
||
next(); | ||
}); | ||
} | ||
|
||
export default jwtRefreshMiddleware; |
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,6 @@ | ||
export const secondInMilliseconds = 1000; | ||
const minuteInSeconds = 60; | ||
const dayInSeconds = 86400; | ||
|
||
export const accessTokenMaxAgeSeconds = 10 * minuteInSeconds; | ||
export const refreshCookieMaxAgeSeconds = 7 * dayInSeconds; |
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,28 @@ | ||
import mongoose, { Schema } from 'mongoose'; | ||
|
||
export interface TokenModel { | ||
_id: mongoose.Types.ObjectId; | ||
jwt: string; | ||
userID: mongoose.Types.ObjectId; | ||
expiry: Date; | ||
} | ||
|
||
const tokenSchema: Schema<TokenModel> = new Schema({ | ||
jwt: { | ||
type: String, | ||
unique: true, | ||
}, | ||
expiry: { | ||
type: Date, | ||
required: [true, 'Please add a expiry date'] | ||
}, | ||
userID: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'User', | ||
required: [true, 'Please add a user ID'] | ||
}, | ||
}) | ||
|
||
const Token = mongoose.model<TokenModel>('Token', tokenSchema); | ||
|
||
export { Token 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { refreshCookieMaxAgeSeconds, secondInMilliseconds } from "../model/constants"; | ||
import Token, { TokenModel } from "../model/token" | ||
import mongoose from 'mongoose'; | ||
|
||
const getRefreshToken = async (userID: mongoose.Types.ObjectId): Promise<TokenModel | null> => { | ||
const token = await Token.findOne({ | ||
userID: userID, | ||
expiry: { $gt: new Date() } | ||
}); | ||
return token; | ||
} | ||
|
||
const saveRefreshToken = async ( | ||
token: TokenModel | ||
): Promise<TokenModel | null> => { | ||
const dbToken = await Token.create(token); | ||
await dbToken.save(); | ||
return dbToken; | ||
} | ||
|
||
|
||
const extendRefreshToken = async (userID: mongoose.Types.ObjectId): Promise<TokenModel | null> => { | ||
const now = new Date(); | ||
const newExpiry = new Date(now.getTime() + refreshCookieMaxAgeSeconds * secondInMilliseconds); | ||
|
||
const token = await Token.findOneAndUpdate({ | ||
userID: userID, | ||
expiry : { $gt: new Date() } | ||
}, { | ||
$set: { "expiry": newExpiry } | ||
}, { new: true }) | ||
return token | ||
} | ||
|
||
const TokenRepo = { | ||
getRefreshToken, | ||
saveRefreshToken, | ||
extendRefreshToken, | ||
} | ||
|
||
export { TokenRepo 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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import Express from "express"; | ||
import UserController from "../controllers/user"; | ||
import jwtMiddleware from "../middleware/jwtMiddleware"; | ||
|
||
const router = Express.Router(); | ||
|
||
router.get("/:userID", UserController.createUser); | ||
router.get("/", jwtMiddleware, UserController.getUser); | ||
router.get("/token", UserController.checkTokens); | ||
|
||
export { router as default }; |
Oops, something went wrong.