refactor utils

This commit is contained in:
wancat committed 2024-02-19 18:05:00 +08:00
1 parent 9509bbc355
commit d6c95cea4a
13 files changed
+126 -31

No files matched your search

+38
View File
@@ -0,0 +1,38 @@
import jwt from 'jsonwebtoken'
import bcrypt from 'bcrypt'
import config from '../config.js'
export function hashPassword(password: string): Promise<string> {
return bcrypt.hash(password, 10)
}
export function genJWT(payload: any): Promise<string> {
return new Promise((resolve, reject) => {
jwt.sign(
payload,
config.secret,
{ expiresIn: config.containerExpiry },
(err, token) => {
if (err) {
reject(err)
} else if (token === undefined) {
reject(new Error('Token is undefined'))
} else {
resolve(token)
}
}
)
})
}
export function verifyJWT(token: string): Promise<any> {
return new Promise((resolve, reject) => {
jwt.verify(token, config.secret, (err, decoded) => {
if (err) {
reject(err)
} else {
resolve(decoded)
}
})
})
}