Skip to content
Closed
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
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ CLOUDINARY_API_KEY=
CLOUDINARY_API_SECRET=
CLOUDINARY_CLOUD_NAME=
DATABASE_URL=
FRONTEND_URL=
REDIS_USERNAME=
REDIS_PASSWORD=
REDIS_HOST=
Expand All @@ -10,8 +11,13 @@ GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_REDIRECT_URI=
PORT=
NODE_ENV=
API_PREFIX=
ALLOWED_ORIGINS=
COOKIE_HTTP_ONLY=
COOKIE_SECURE=
COOKIE_SAME_SITE=
COOKIE_DOMAIN=
ACCESS_JWT_SECRET=
REFRESH_JWT_SECRET=
ACCESS_JWT_EXPIRES_IN=
Expand Down
4 changes: 3 additions & 1 deletion app/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import express from 'express'
import cookieParser from 'cookie-parser'
import cors from 'cors'
import helmet from 'helmet'
import logger from 'morgan'
Expand All @@ -13,8 +14,9 @@ export const app = express()
z.config(zodConfig)

app.use(helmet())
app.use(cors({ origin: env.ALLOWED_ORIGINS }))
app.use(cors({ origin: env.ALLOWED_ORIGINS, credentials: true }))
app.use(logger(app.get('env') === 'development' ? 'dev' : 'combined'))
app.use(cookieParser())
app.use(express.json())

app.use(env.API_PREFIX, apiRouter)
Expand Down
6 changes: 6 additions & 0 deletions app/config/env.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const envSchema = z.object({
CLOUDINARY_API_SECRET: z.string(),
CLOUDINARY_CLOUD_NAME: z.string(),
DATABASE_URL: z.string(),
FRONTEND_URL: z.url(),
GOOGLE_CLIENT_ID: z.string(),
GOOGLE_CLIENT_SECRET: z.string(),
GOOGLE_REDIRECT_URI: z.url(),
Expand All @@ -19,6 +20,11 @@ const envSchema = z.object({
ACCESS_JWT_SECRET: z.string().transform(v => new TextEncoder().encode(v)),
REFRESH_JWT_SECRET: z.string().transform(v => new TextEncoder().encode(v)),
PORT: z.coerce.number().int().positive().min(1000).max(65535),
NODE_ENV: z.enum(['development', 'production']).default('development'),
COOKIE_HTTP_ONLY: z.stringbool(),
COOKIE_SECURE: z.stringbool(),
COOKIE_SAME_SITE: z.enum(['lax', 'strict', 'none']),
COOKIE_DOMAIN: z.string(),
API_PREFIX: z.string(),
ALLOWED_ORIGINS: z
.string()
Expand Down
124 changes: 60 additions & 64 deletions app/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import crypto from 'crypto'
import type {
GoogleCodeSchema,
RefreshTokenSchema,
SigninSchema,
SignupSchema
} from '@/schemas'
import type { JwtPayload, TypedRequestBody } from '@/types'
import type { GoogleCodeSchema, SigninSchema, SignupSchema } from '@/schemas'
import type { JwtPayload, TypedRequestBody, TypedRequestQuery } from '@/types'
import type { NextFunction, Request, Response } from 'express'

import { prisma } from '@/prisma'
Expand All @@ -26,7 +21,12 @@ const {
REFRESH_JWT_ALGORITHM,
GOOGLE_CLIENT_ID,
GOOGLE_CLIENT_SECRET,
GOOGLE_REDIRECT_URI
GOOGLE_REDIRECT_URI,
FRONTEND_URL,
COOKIE_HTTP_ONLY,
COOKIE_DOMAIN,
COOKIE_SECURE,
COOKIE_SAME_SITE
} = env

class AuthController {
Expand All @@ -36,6 +36,9 @@ class AuthController {
GOOGLE_REDIRECT_URI
)

private readonly ACCESS_TOKEN_NAME = 'accessToken'
private readonly REFRESH_TOKEN_NAME = 'refreshToken'

signup = async (
{ body }: TypedRequestBody<typeof SignupSchema>,
res: Response,
Expand All @@ -54,13 +57,11 @@ class AuthController {
}
})

const newSession = await prisma.session.create({
data: { userId: user.id }
})
const tokens = await this.getNewTokens({ id: user.id })

const tokens = await this.getNewTokens({ id: user.id, sid: newSession.id })
this.setTokensCookie(res, tokens)

res.json({ user, ...tokens })
res.json({ user })
}

signin = async (
Expand All @@ -83,16 +84,14 @@ class AuthController {

if (!isPasswordMatch) return next(Unauthorized('Email or password invalid'))

const newSession = await prisma.session.create({
data: { userId: user.id }
})
const tokens = await this.getNewTokens({ id: user.id })

const tokens = await this.getNewTokens({ id: user.id, sid: newSession.id })
this.setTokensCookie(res, tokens)

res.json({ user: userWithoutPassword, ...tokens })
res.json({ user: userWithoutPassword })
}

getGoogleRedirectUrl = async (_: Request, res: Response) => {
googleInitiate = async (_: Request, res: Response) => {
const state = crypto.randomBytes(32).toString('hex')

await redisClient.set(`oauth_state:${state}`, 'true', 'EX', 5 * 60)
Expand All @@ -107,11 +106,11 @@ class AuthController {
}

googleCallback = async (
req: TypedRequestBody<typeof GoogleCodeSchema>,
req: TypedRequestQuery<typeof GoogleCodeSchema>,
res: Response,
next: NextFunction
) => {
const { code, state: receivedState } = req.body
const { code, state: receivedState } = req.query

const redisStateKey = `oauth_state:${receivedState}`

Expand All @@ -126,15 +125,12 @@ class AuthController {
if (!tokens.id_token) return next(Forbidden())

const ticket = await this.googleClient.verifyIdToken({
idToken: tokens.id_token,
audience: GOOGLE_CLIENT_ID
idToken: tokens.id_token
})

const payload = ticket.getPayload()

if (!payload || !payload.email) {
return next(Forbidden('Invalid token'))
}
if (!payload || !payload.email) return next(Forbidden('Invalid token'))

const {
email,
Expand All @@ -151,68 +147,49 @@ class AuthController {
data: { name, email, avatar: picture }
})

const newSession = await prisma.session.create({
data: { userId: user.id }
})
const tokens = await this.getNewTokens({ id: user.id })

const tokens = await this.getNewTokens({
id: user.id,
sid: newSession.id
})
this.setTokensCookie(res, tokens)

res.json({ user, ...tokens })
res.redirect(FRONTEND_URL)
} else {
const newSession = await prisma.session.create({
data: { userId: user.id }
})
const tokens = await this.getNewTokens({ id: user.id })

const tokens = await this.getNewTokens({
id: user.id,
sid: newSession.id
})
this.setTokensCookie(res, tokens)

res.json({ user, ...tokens })
res.redirect(FRONTEND_URL)
}
}

refresh = async (
{ body }: TypedRequestBody<typeof RefreshTokenSchema>,
res: Response,
next: NextFunction
) => {
refresh = async (req: Request, res: Response, next: NextFunction) => {
const refreshToken = req.cookies[this.REFRESH_TOKEN_NAME]

if (!refreshToken) return next(Forbidden())

try {
const {
payload: { id, sid }
} = await jwtVerify<JwtPayload>(body.refreshToken, REFRESH_JWT_SECRET)
payload: { id }
} = await jwtVerify<JwtPayload>(refreshToken, REFRESH_JWT_SECRET)

const user = await prisma.user.findFirst({ where: { id } })

if (!user) return next(Forbidden())

const currentSession = await prisma.session.findFirst({
where: { id: sid }
})

if (!currentSession) return next(Forbidden())

await prisma.session.delete({ where: { id: currentSession.id } })
const tokens = await this.getNewTokens({ id: user.id })

const newSid = await prisma.session.create({
data: { userId: user.id }
})

const tokens = await this.getNewTokens({ id: user.id, sid: newSid.id })
this.setTokensCookie(res, tokens)

res.json(tokens)
res.json({ message: 'Tokens refreshed successfully' })
} catch (error) {
if (error instanceof JWTExpired) return next(Forbidden(error.code))

return next(Forbidden())
}
}

logout = async ({ session }: Request, res: Response) => {
await prisma.session.delete({ where: { id: session } })
logout = async (_: Request, res: Response) => {
res.clearCookie(this.ACCESS_TOKEN_NAME)
res.clearCookie(this.REFRESH_TOKEN_NAME)

res.sendStatus(204)
}
Expand All @@ -230,6 +207,25 @@ class AuthController {

return { accessToken, refreshToken }
}

private setTokensCookie = (
res: Response,
tokens: { accessToken: string; refreshToken: string }
) => {
res.cookie(this.ACCESS_TOKEN_NAME, tokens.accessToken, {
httpOnly: COOKIE_HTTP_ONLY,
secure: COOKIE_SECURE,
sameSite: COOKIE_SAME_SITE,
domain: COOKIE_DOMAIN
})

res.cookie(this.REFRESH_TOKEN_NAME, tokens.refreshToken, {
httpOnly: COOKIE_HTTP_ONLY,
secure: COOKIE_SECURE,
sameSite: COOKIE_SAME_SITE,
domain: COOKIE_DOMAIN
})
}
}

export const authController = new AuthController()
17 changes: 8 additions & 9 deletions app/middlewares/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { NextFunction, Request, Response } from 'express'
import { prisma } from '@/prisma'
import { Unauthorized } from 'http-errors'
import { jwtVerify } from 'jose'
import { JWTExpired } from 'jose/errors'

import { env } from '@/config'

Expand All @@ -12,30 +13,28 @@ export const authenticate = async (
_: Response,
next: NextFunction
) => {
const { authorization = '' } = req.headers
const [bearer, token] = authorization.split(' ')
const token: string = req.cookies.accessToken

if (bearer !== 'Bearer') return next(Unauthorized())
if (!token) return next(Unauthorized())

try {
const {
payload: { id, sid }
payload: { id }
} = await jwtVerify<JwtPayload>(token, env.ACCESS_JWT_SECRET)

const user = await prisma.user.findFirst({
where: { id },
omit: { password: false }
})

const session = await prisma.session.findFirst({ where: { id: sid } })

if (!user || !session) return next(Unauthorized())
if (!user) return next(Unauthorized())

req.user = user
req.session = session.id

next()
} catch {
} catch (e) {
if (e instanceof JWTExpired) return next(Unauthorized(e.code))

return next(Unauthorized())
}
}
19 changes: 5 additions & 14 deletions app/routes/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import { authController } from '@/controllers'

import { authenticate, validateRequest } from '@/middlewares'

import {
GoogleCodeSchema,
RefreshTokenSchema,
SigninSchema,
SignupSchema
} from '@/schemas'
import { GoogleCodeSchema, SigninSchema, SignupSchema } from '@/schemas'

export const authRouter = Router()

Expand All @@ -25,18 +20,14 @@ authRouter.post(
authController.signin
)

authRouter.get('/google/initiate', authController.getGoogleRedirectUrl)
authRouter.post('/google/initiate', authController.googleInitiate)

authRouter.post(
authRouter.get(
'/google/callback',
validateRequest({ body: GoogleCodeSchema }),
validateRequest({ query: GoogleCodeSchema }),
authController.googleCallback
)

authRouter.post(
'/refresh',
validateRequest({ body: RefreshTokenSchema }),
authController.refresh
)
authRouter.post('/refresh', authController.refresh)

authRouter.post('/logout', authenticate, authController.logout)
4 changes: 0 additions & 4 deletions app/schemas/auth.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ export const SignupSchema = z.object({
name: z.string().min(2)
})

export const RefreshTokenSchema = z.object({
refreshToken: z.string().min(1)
})

export const GoogleCodeSchema = z.object({
code: z.string().min(1),
state: z.optional(z.string())
Expand Down
7 changes: 1 addition & 6 deletions app/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,5 @@ export {
ColumnParamsSchema,
UpdateColumnOrderSchema
} from './column.schema'
export {
SigninSchema,
SignupSchema,
RefreshTokenSchema,
GoogleCodeSchema
} from './auth.schema'
export { SigninSchema, SignupSchema, GoogleCodeSchema } from './auth.schema'
export { EditUserSchema, NeedHelpSchema } from './user.schema'
1 change: 0 additions & 1 deletion app/types/express.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { User } from '@prisma/client'
declare global {
namespace Express {
interface Request {
session: string
user: User
}
}
Expand Down
1 change: 0 additions & 1 deletion app/types/jwt-payload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export type JwtPayload = {
id?: string
sid?: string
}
Loading