mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-07 19:10:12 +08:00
refactor utils
This commit is contained in:
1 parent
9509bbc355
commit
d6c95cea4a
13 files changed
+126
-31
No files matched your search
@@ -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)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user