9
src/libs/AuthUtils.ts
Normal file
9
src/libs/AuthUtils.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import bcryptjs from 'bcryptjs'
|
||||
|
||||
export function hashPassword(password: string): Promise<string> {
|
||||
return bcryptjs.hash(password, 10)
|
||||
}
|
||||
|
||||
export function comparePassword(password: string, hash: string): Promise<boolean> {
|
||||
return bcryptjs.compare(password, hash)
|
||||
}
|
@ -1,11 +1,12 @@
|
||||
import type { ServerResponse } from 'node:http'
|
||||
|
||||
export default class CookieManager {
|
||||
private cookies: Record<string, string> = {}
|
||||
public constructor(data?: string | Record<string, string>) {
|
||||
if (typeof data === 'string') {
|
||||
data.split(';').forEach((keyValuePair) => {
|
||||
const [key, value] = keyValuePair.split('=')
|
||||
if (!key || !value) {
|
||||
return
|
||||
}
|
||||
this.cookies[key.trim()] = value.trim().replace(/%3B/g, ';')
|
||||
})
|
||||
} else if (typeof data === 'object') {
|
||||
@ -13,7 +14,7 @@ export default class CookieManager {
|
||||
}
|
||||
}
|
||||
|
||||
public static addCookie(res: ServerResponse, cookie: {
|
||||
public static addCookie(res: ResponseInit & { readonly headers: Headers; }, cookie: {
|
||||
key: string
|
||||
value: string
|
||||
expire?: string
|
||||
@ -46,7 +47,7 @@ export default class CookieManager {
|
||||
if (cookie.sameSite) {
|
||||
items.push(`SameSite=${cookie.sameSite}`)
|
||||
}
|
||||
res.setHeader('Set-Cookie', items.join('; '))
|
||||
res.headers.append('Set-Cookie', items.join('; '))
|
||||
}
|
||||
|
||||
public get(key: string): string | undefined {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import DaoFactory from '../models/DaoFactory'
|
||||
import CookieManager from './CookieManager'
|
||||
import RFC7807, { buildRFC7807 } from './RFCs/RFC7807'
|
||||
import { buildRFC7807 } from './RFCs/RFC7807'
|
||||
|
||||
interface Permission {
|
||||
name: string
|
||||
|
Reference in New Issue
Block a user