28 lines
782 B
TypeScript
28 lines
782 B
TypeScript
import jswt from "jsonwebtoken";
|
|
import { IJwt } from "@server/types/authstuff";
|
|
import { User } from "@models/user";
|
|
import { log } from "@server/logger";
|
|
const { verify } = jswt;
|
|
export default eventHandler(async (ev) => {
|
|
const body = await readBody<any>(ev);
|
|
const errMsg = createError({ statusCode: 403, message: "could not verify!" });
|
|
const { jwt } = useRuntimeConfig();
|
|
log.debug(JSON.stringify(body), { label: "WHAT???" });
|
|
if (!body.refreshToken) {
|
|
throw errMsg;
|
|
}
|
|
const decoded = verify(body.refreshToken, jwt) as IJwt | undefined;
|
|
|
|
if (!decoded) {
|
|
throw errMsg;
|
|
}
|
|
const user = await User.findById(decoded.id);
|
|
if (!user) throw errMsg;
|
|
return {
|
|
token: {
|
|
access: user.generateAccessToken(jwt),
|
|
refresh: user.generateRefreshToken(jwt),
|
|
},
|
|
};
|
|
});
|