Deduplicate api auth
This commit is contained in:
+22
-20
@@ -35,6 +35,21 @@ const s = (() => {
|
||||
return { Standards, Calculator, Commerce };
|
||||
})();
|
||||
|
||||
// PLUGINS
|
||||
// -----------------------
|
||||
const authPlugin = new Elysia({ name: "auth" })
|
||||
.use(jwt({ name: "jwt", secret: env.AUTH_SECRET }))
|
||||
.guard({ cookie: t.Cookie({ auth: t.Optional(t.String()) }) })
|
||||
.macro({
|
||||
auth: {
|
||||
async resolve({ jwt, cookie: { auth } }) {
|
||||
const token = auth.value && await jwt.verify(auth.value);
|
||||
if (!token || !token.sessionId) throw status(401, "Unauthorized");
|
||||
return { sessionId: token.sessionId.toString() };
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// ELYSIA
|
||||
// -----------------------
|
||||
export const app = new Elysia()
|
||||
@@ -50,7 +65,9 @@ export const app = new Elysia()
|
||||
"http://localhost:5173",
|
||||
],
|
||||
}),
|
||||
).use(jwt({ name: "jwt", secret: env.AUTH_SECRET }))
|
||||
)
|
||||
.guard({ cookie: t.Cookie({ auth: t.Optional(t.String()) }) })
|
||||
.use(authPlugin)
|
||||
|
||||
.error({
|
||||
PrintfulError,
|
||||
@@ -91,7 +108,6 @@ export const app = new Elysia()
|
||||
status
|
||||
}, "request");
|
||||
})
|
||||
.guard({ cookie: t.Cookie({ auth: t.Optional(t.String()) }) })
|
||||
|
||||
.get("/", () => ({ status: "ok" }))
|
||||
.get("/health", () => ({ status: "ok" }))
|
||||
@@ -124,11 +140,8 @@ export const app = new Elysia()
|
||||
activityPerformances: t.Array(ActivityPerformanceSchema)
|
||||
}),
|
||||
})
|
||||
.resolve(async ({ jwt, cookie: { auth } }) => {
|
||||
const token = auth.value && await jwt.verify(auth.value);
|
||||
if (!token || !token.sessionId) throw status(401, "Unauthorized");
|
||||
return { sessionId: token.sessionId.toString() };
|
||||
})
|
||||
// Authenticated
|
||||
.guard({ auth: true })
|
||||
.get("/standards/config", async () => {
|
||||
return await s.Calculator.Standards.Config.get();
|
||||
})
|
||||
@@ -138,12 +151,7 @@ export const app = new Elysia()
|
||||
body: t.Object({ standardsConfigId: t.String() })
|
||||
})
|
||||
)
|
||||
.group("/standards", (app) => app
|
||||
.resolve(async ({ jwt, cookie: { auth } }) => {
|
||||
const token = auth.value && await jwt.verify(auth.value);
|
||||
if (!token || !token.sessionId) throw status(401, "Unauthorized");
|
||||
return { sessionId: token.sessionId.toString() };
|
||||
})
|
||||
.group("/standards", { auth: true }, (app) => app
|
||||
.post("/configs", async ({ body: { name, datasetId, params } }) => {
|
||||
return await s.Standards.Configs.create(name, datasetId, params);
|
||||
}, {
|
||||
@@ -184,13 +192,7 @@ export const app = new Elysia()
|
||||
|
||||
// COMMERCE
|
||||
.group("/commerce", (app) => app
|
||||
.group("/products",
|
||||
(app) => app
|
||||
.resolve(async ({ jwt, cookie: { auth } }) => {
|
||||
const token = auth.value && await jwt.verify(auth.value);
|
||||
if (!token || !token.sessionId) throw status(401, "Unauthorized");
|
||||
return { sessionId: token.sessionId.toString() };
|
||||
})
|
||||
.group("/products", { auth: true }, (app) => app
|
||||
.group("/sync", (app) => app
|
||||
// Sync status
|
||||
.get("/", async ({ sessionId }) => {
|
||||
|
||||
Reference in New Issue
Block a user