easynode/server/app/utils/verify-auth.js
2024-07-11 12:13:33 +08:00

34 lines
957 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { AESDecryptSync } = require('./encrypt')
const { readKey } = require('./storage')
const jwt = require('jsonwebtoken')
const enumLoginCode = {
SUCCESS: 1,
EXPIRES: -1,
ERROR_TOKEN: -2
}
// 校验token与登录IP
const verifyAuthSync = async (token, clientIp) => {
consola.info('verifyAuthSync IP', clientIp)
try {
token = await AESDecryptSync(token) // 先aes解密
const { commonKey } = await readKey()
const { exp } = jwt.verify(token, commonKey)
if (Date.now() > (exp * 1000)) return { code: -1, msg: 'token expires' } // 过期
return { code: enumLoginCode.SUCCESS, msg: 'success' } // 验证成功
} catch (error) {
return { code: enumLoginCode.ERROR_TOKEN, msg: error } // token错误, 验证失败
}
}
const isProd = () => {
const EXEC_ENV = process.env.EXEC_ENV || 'production'
return EXEC_ENV === 'production'
}
module.exports = {
verifyAuthSync,
isProd
}