Deduplicate api auth
This commit is contained in:
+51
-49
@@ -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,47 +192,41 @@ 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
|
.group("/sync", (app) => app
|
||||||
.resolve(async ({ jwt, cookie: { auth } }) => {
|
// Sync status
|
||||||
const token = auth.value && await jwt.verify(auth.value);
|
.get("/", async ({ sessionId }) => {
|
||||||
if (!token || !token.sessionId) throw status(401, "Unauthorized");
|
const latestSyncState = await s.Commerce.ProductSync.getLatestSyncState(sessionId);
|
||||||
return { sessionId: token.sessionId.toString() };
|
if (!latestSyncState) throw new NotFoundError("No product sync found for the provided session");
|
||||||
|
return latestSyncState;
|
||||||
})
|
})
|
||||||
.group("/sync", (app) => app
|
// Run sync
|
||||||
// Sync status
|
.post("/:pProductId?", async ({ params: { pProductId }, sessionId }) => {
|
||||||
.get("/", async ({ sessionId }) => {
|
await s.Commerce.ProductSync.Queue.enqueue({
|
||||||
const latestSyncState = await s.Commerce.ProductSync.getLatestSyncState(sessionId);
|
type: "product_sync_update",
|
||||||
if (!latestSyncState) throw new NotFoundError("No product sync found for the provided session");
|
source: "portal",
|
||||||
return latestSyncState;
|
payload: {
|
||||||
})
|
session: { id: sessionId, name: "Portal" },
|
||||||
// Run sync
|
filter: {
|
||||||
.post("/:pProductId?", async ({ params: { pProductId }, sessionId }) => {
|
pProductIds: pProductId ? [pProductId] : undefined
|
||||||
await s.Commerce.ProductSync.Queue.enqueue({
|
|
||||||
type: "product_sync_update",
|
|
||||||
source: "portal",
|
|
||||||
payload: {
|
|
||||||
session: { id: sessionId, name: "Portal" },
|
|
||||||
filter: {
|
|
||||||
pProductIds: pProductId ? [pProductId] : undefined
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}, { params: t.Object({ pProductId: t.Optional(t.Numeric()) }) }),
|
});
|
||||||
)
|
}, { params: t.Object({ pProductId: t.Optional(t.Numeric()) }) }),
|
||||||
.get("/:pProductId", async ({ params: { pProductId } }) => {
|
)
|
||||||
const pProduct = await s.Commerce.Printful.Products.get(pProductId);
|
.get("/:pProductId", async ({ params: { pProductId } }) => {
|
||||||
if (!pProduct) throw new NotFoundError("Missing printful product");
|
const pProduct = await s.Commerce.Printful.Products.get(pProductId);
|
||||||
|
if (!pProduct) throw new NotFoundError("Missing printful product");
|
||||||
|
|
||||||
const wProductId = pProduct.sync_product.external_id.split("-")[0];
|
const wProductId = pProduct.sync_product.external_id.split("-")[0];
|
||||||
if (!wProductId) throw new NotFoundError("Missing webflow product ID");
|
if (!wProductId) throw new NotFoundError("Missing webflow product ID");
|
||||||
|
|
||||||
const wProduct = await s.Commerce.Webflow.Products.get(wProductId);
|
const wProduct = await s.Commerce.Webflow.Products.get(wProductId);
|
||||||
|
|
||||||
return { pProduct, wProduct };
|
return { pProduct, wProduct };
|
||||||
}, {
|
}, {
|
||||||
params: t.Object({ pProductId: t.Numeric() })
|
params: t.Object({ pProductId: t.Numeric() })
|
||||||
})
|
})
|
||||||
))
|
))
|
||||||
|
|
||||||
// WEBHOOKS
|
// WEBHOOKS
|
||||||
|
|||||||
Reference in New Issue
Block a user