Signed-off-by: Avior <github@avior.me>
This commit is contained in:
2023-06-22 00:50:42 +02:00
parent 8442c7b4e3
commit ae1b7f9ec4
23 changed files with 684 additions and 83 deletions

9
src/libs/AuthUtils.ts Normal file
View 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)
}

View File

@ -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 {

View File

@ -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