// controller.ts
const app = new Hono<{ Bindings: Bindings; Variables: Variables }>();
app.use("/*", async (c, next) => {
const jwtMiddleware = jwt({
secret: c.env.JWT_SECRET,
});
return jwtMiddleware(c, next);
});
app.use("/*", async (c, next) => {
const jwtPayload = c.get("jwtPayload") as JWTPayload; // without cast is any
if (jwtPayload.role < Role.MANAGER) {
throw error(401, "Unauthorized");
}
});
app.get("/:id", async ({ get, env, req, json }) => {
const { id } = get("jwtPayload") as JWTPayload; // without cast is any
// More code below
// ...
});
// ===========================================
// auth.ts
app.post("/auth", async(c) => {
// Verification of google oauth omitted
// ...
// Once user is authed
const newUserToken = await sign({
id: user.id,
email: user.email,
name: user.name,
profilePicture: user.profilePicture,
role: user.role,
},
env.JWT_SECRET,
);
// Code that returns JWT to client to be included in Authorization header on subsequent requests
// ...
});
// controller.ts
const app = new Hono<{ Bindings: Bindings; Variables: Variables }>();
app.use("/*", async (c, next) => {
const jwtMiddleware = jwt({
secret: c.env.JWT_SECRET,
});
return jwtMiddleware(c, next);
});
app.use("/*", async (c, next) => {
const jwtPayload = c.get("jwtPayload") as JWTPayload; // without cast is any
if (jwtPayload.role < Role.MANAGER) {
throw error(401, "Unauthorized");
}
});
app.get("/:id", async ({ get, env, req, json }) => {
const { id } = get("jwtPayload") as JWTPayload; // without cast is any
// More code below
// ...
});
// ===========================================
// auth.ts
app.post("/auth", async(c) => {
// Verification of google oauth omitted
// ...
// Once user is authed
const newUserToken = await sign({
id: user.id,
email: user.email,
name: user.name,
profilePicture: user.profilePicture,
role: user.role,
},
env.JWT_SECRET,
);
// Code that returns JWT to client to be included in Authorization header on subsequent requests
// ...
});