Skip to content

Commit

Permalink
add token generate methods and update /refresh endpoint and middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmabulage committed Sep 16, 2024
1 parent d4e195d commit 4d9f2fe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
16 changes: 6 additions & 10 deletions src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
resetPassword
} from '../services/auth.service'
import type { ApiResponse } from '../types'
import { signAndSetCookie } from '../utils'
import { setAccessToken, signAndSetCookie } from '../utils'

export const googleRedirect = async (
req: Request,
Expand Down Expand Up @@ -169,23 +169,19 @@ export const requireAuth = (

if (!token && !refreshToken) {
return res
.status(401)
.json({ error: 'Access Denied. No token provided.' })
.status(403)
.json({ error: 'Forbidden. No token provided.' })
}

try {
jwt.verify(token, JWT_SECRET)
} catch (err) {
if (!refreshToken) {
return res.status(401).send('Access Denied. Please Login again.')
}

} catch (err) {
try {
const decoded = jwt.verify(refreshToken, REFRESH_JWT_SECRET) as {
userId: string
}

signAndSetCookie(res, decoded.userId)
setAccessToken(res, decoded.userId)
} catch (error) {
return res
.status(401)
Expand Down Expand Up @@ -260,7 +256,7 @@ export const refresh = async (req: Request, res: Response): Promise<void> => {
userId: string
}

signAndSetCookie(res, decoded.userId)
setAccessToken(res, decoded.userId)
} catch (error) {
res.status(401).json({ error: 'Invalid token, please log in again' })
}
Expand Down
24 changes: 21 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ import type Mentor from './entities/mentor.entity'
import { MenteeApplicationStatus, MentorApplicationStatus } from './enums'
import { generateCertificate } from './services/admin/generateCertificate'

export const signAndSetCookie = (res: Response, uuid: string): void => {
const accessToken = jwt.sign({ userId: uuid }, JWT_SECRET ?? '')
const refreshToken = jwt.sign({ userId: uuid }, REFRESH_JWT_SECRET ?? '', {
const generateAccessToken = (uuid: string): string => {
return jwt.sign({ userId: uuid }, JWT_SECRET ?? '')
}

const generateRefreshToken = (uuid: string): string => {
return jwt.sign({ userId: uuid }, REFRESH_JWT_SECRET ?? '', {
expiresIn: '10d'
})
}

export const signAndSetCookie = (res: Response, uuid: string): void => {
const accessToken = generateAccessToken(uuid)
const refreshToken = generateRefreshToken(uuid)

res.cookie('accessToken', accessToken, {
httpOnly: true,
Expand All @@ -30,6 +38,16 @@ export const signAndSetCookie = (res: Response, uuid: string): void => {
})
}

export const setAccessToken = (res: Response, uuid: string): void => {
const accessToken = generateAccessToken(uuid)

res.cookie('accessToken', accessToken, {
httpOnly: true,
maxAge: 5 * 24 * 60 * 60 * 1000,
secure: false // TODO: Set to true when using HTTPS
})
}

export const getMentorPublicData = (mentor: Mentor): Mentor => {
const { application, profile } = mentor

Expand Down

0 comments on commit 4d9f2fe

Please sign in to comment.