transform core api codes into this project, update modules as admin/pos context modules
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
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
|
||||
|
||||
let token = req.cookies?.accessToken
|
||||
|
||||
if (!token) {
|
||||
const authHeader = (req.headers.authorization || req.headers.Authorization) as
|
||||
| string
|
||||
| undefined
|
||||
if (authHeader && authHeader.startsWith('Bearer ')) {
|
||||
token = authHeader.slice(7)
|
||||
}
|
||||
}
|
||||
if (!token) throw new UnauthorizedException('Missing accessToken cookie')
|
||||
|
||||
// const token = authHeader.replace('Bearer ', '')
|
||||
try {
|
||||
const payload = jwtService.decode(token) as AccessTokenPayload
|
||||
console.log(payload)
|
||||
|
||||
return payload?.account_id || null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import * as bcrypt from 'bcrypt'
|
||||
|
||||
export class PasswordUtil {
|
||||
static async hash(password: string, saltRounds = 10): Promise<string> {
|
||||
return bcrypt.hash(password, saltRounds)
|
||||
}
|
||||
|
||||
static async compare(password: string, hashedPassword: string): Promise<boolean> {
|
||||
return bcrypt.compare(password, hashedPassword)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user