refactor(api): switch to refresh nuxt-auth provider

This commit is contained in:
parent 76317e8006
commit e88474d406
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C
2 changed files with 36 additions and 3 deletions

@ -65,16 +65,18 @@ export default defineNuxtConfig({
auth: { auth: {
baseURL: "/api/auth", baseURL: "/api/auth",
provider: { provider: {
type: "local", type: "refresh",
pages: { pages: {
login: "/auth/login", login: "/auth/login",
}, },
token: { token: {
signInResponseTokenPointer: "/token", signInResponseTokenPointer: "/token/access",
type: "Bearer", type: "Bearer",
headerName: "Authorization", headerName: "Authorization",
maxAgeInSeconds: 14 * 24 * 60 * 60, maxAgeInSeconds: 14 * 24 * 60 * 60,
// sameSiteAttribute: , },
refreshToken: {
signInResponseRefreshTokenPointer: "/token/refresh",
}, },
// @ts-ignore // @ts-ignore
sessionDataType: {} as IUser, sessionDataType: {} as IUser,
@ -85,6 +87,10 @@ export default defineNuxtConfig({
path: "/session", path: "/session",
method: "get", method: "get",
}, },
refresh: {
path: "/refresh",
method: "post",
},
}, },
}, },
globalAppMiddleware: false, globalAppMiddleware: false,

@ -0,0 +1,27 @@
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),
},
};
});