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