create token decorator and consumer module

This commit is contained in:
2026-03-16 17:56:51 +03:30
parent 0ad6a3200e
commit 69e9a0d082
38 changed files with 1027 additions and 66 deletions
+6 -5
View File
@@ -1,11 +1,13 @@
import { ITokenPayload } from '@/modules/auth/auth.utils'
import { UnauthorizedException } from '@nestjs/common'
import { JwtService } from '@nestjs/jwt'
import { Request } from 'express'
import { AccessTokenPayload } from 'modules/auth/models'
export function getAccountIdFromJwt(req: Request, jwtService: JwtService): string | null {
// Try to get token from Authorization header
export function checkAndDecodeJwtToken(
req: Request,
jwtService: JwtService,
): ITokenPayload | null {
let token = req.cookies?.accessToken
if (!token) {
@@ -18,11 +20,10 @@ export function getAccountIdFromJwt(req: Request, jwtService: JwtService): strin
}
if (!token) throw new UnauthorizedException('Missing accessToken cookie')
// const token = authHeader.replace('Bearer ', '')
try {
const payload = jwtService.decode(token) as AccessTokenPayload
return payload?.account_id || null
return payload as ITokenPayload
} catch {
return null
}