29 lines
719 B
TypeScript
29 lines
719 B
TypeScript
import jwt from 'jsonwebtoken';
|
|
import { ERROR_ENUM } from '@fastgpt/common/constant/errorCode';
|
|
|
|
/* 生成 token */
|
|
export const generateToken = (userId: string) => {
|
|
const key = process.env.TOKEN_KEY as string;
|
|
const token = jwt.sign(
|
|
{
|
|
userId,
|
|
exp: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 7
|
|
},
|
|
key
|
|
);
|
|
return token;
|
|
};
|
|
// auth token
|
|
export const authJWT = (token: string) =>
|
|
new Promise<string>((resolve, reject) => {
|
|
const key = process.env.TOKEN_KEY as string;
|
|
|
|
jwt.verify(token, key, function (err, decoded: any) {
|
|
if (err || !decoded?.userId) {
|
|
reject(ERROR_ENUM.unAuthorization);
|
|
return;
|
|
}
|
|
resolve(decoded.userId);
|
|
});
|
|
});
|