Skip to content

Commit

Permalink
fix(auth): change cookies logic
Browse files Browse the repository at this point in the history
  • Loading branch information
anteqkois committed May 26, 2024
1 parent 07f0c1a commit e635fff
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
4 changes: 3 additions & 1 deletion apps/web/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { NextRequest, NextResponse } from 'next/server'
export function middleware(req: NextRequest) {
const authStatus = req.cookies.get(Cookies.AUTH_STATUS)
const accessToken = req.cookies.get(Cookies.ACCESS_TOKEN)
if (!authStatus || !accessToken || authStatus.value !== AuthStatus.AUTHENTICATED)
if (!authStatus || !accessToken || authStatus.value !== AuthStatus.AUTHENTICATED) {
console.log('Unauthorized request', req.nextUrl.pathname, req.url)
return NextResponse.redirect(new URL(`/login?from=${req.nextUrl.pathname}`, req.url))
}
return NextResponse.next()
}

Expand Down
15 changes: 12 additions & 3 deletions libs/nest-core/src/modules/users/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ export class AuthController {
async signup(@BodySchema(signUpInputSchema) body: SignUpInput, @Response({ passthrough: true }) res: FastifyReply): Promise<SignUpResponse> {
const { access_token, user: userRes } = await this.authService.signUp(body)
const expireDateUnix = +this.configService.get<number>('JWT_ACCES_TOKEN_EXPIRE_SSECONDS', 3600)
const domain = this.configService.getOrThrow('DOMAIN')

res.setCookie(Cookies.ACCESS_TOKEN, access_token, {
path: '/',
domain,
httpOnly: true,
secure: false,
sameSite: 'lax',
// sameSite: 'lax',
sameSite: 'none',
expires: new Date(Date.now() + 1000 * expireDateUnix),
})
res.setCookie(Cookies.AUTH_STATUS, AuthStatus.AUTHENTICATED, {
domain,
path: '/',
})

Expand All @@ -55,15 +59,19 @@ export class AuthController {

const { access_token, user: userRes } = await this.authService.login(user)
const expireDateUnix = +this.configService.get('JWT_ACCES_TOKEN_EXPIRE_SSECONDS', 3600)
const domain = this.configService.getOrThrow('DOMAIN')

res.setCookie(Cookies.ACCESS_TOKEN, access_token, {
path: '/',
domain,
httpOnly: true,
secure: false,
sameSite: 'lax',
// sameSite: 'lax',
sameSite: 'none',
expires: new Date(Date.now() + 1000 * expireDateUnix),
})
res.setCookie(Cookies.AUTH_STATUS, AuthStatus.AUTHENTICATED, {
domain,
path: '/',
})

Expand Down Expand Up @@ -107,7 +115,8 @@ export class AuthController {
// res.setCookie('access_token', access_token, {
// httpOnly: true,
// secure: false,
// sameSite: 'lax',
// sameSite: 'lax',
// none: 'lax',
// expires: new Date(Date.now() + expireDateUnix),
// })
// return res.send({ user: userRes, status: 'ok' })
Expand Down

0 comments on commit e635fff

Please sign in to comment.