refactor(api): switch to refresh
nuxt-auth provider
This commit is contained in:
parent
76317e8006
commit
e88474d406
@ -65,16 +65,18 @@ export default defineNuxtConfig({
|
||||
auth: {
|
||||
baseURL: "/api/auth",
|
||||
provider: {
|
||||
type: "local",
|
||||
type: "refresh",
|
||||
pages: {
|
||||
login: "/auth/login",
|
||||
},
|
||||
token: {
|
||||
signInResponseTokenPointer: "/token",
|
||||
signInResponseTokenPointer: "/token/access",
|
||||
type: "Bearer",
|
||||
headerName: "Authorization",
|
||||
maxAgeInSeconds: 14 * 24 * 60 * 60,
|
||||
// sameSiteAttribute: ,
|
||||
},
|
||||
refreshToken: {
|
||||
signInResponseRefreshTokenPointer: "/token/refresh",
|
||||
},
|
||||
// @ts-ignore
|
||||
sessionDataType: {} as IUser,
|
||||
@ -85,6 +87,10 @@ export default defineNuxtConfig({
|
||||
path: "/session",
|
||||
method: "get",
|
||||
},
|
||||
refresh: {
|
||||
path: "/refresh",
|
||||
method: "post",
|
||||
},
|
||||
},
|
||||
},
|
||||
globalAppMiddleware: false,
|
||||
|
27
server/api/auth/refresh.post.ts
Normal file
27
server/api/auth/refresh.post.ts
Normal file
@ -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),
|
||||
},
|
||||
};
|
||||
});
|
Loading…
Reference in New Issue
Block a user