Skip to content
This repository was archived by the owner on Jul 11, 2025. It is now read-only.

Commit 94b1c23

Browse files
Refactor: move auth middleware (#116)
2 parents ad1d1d9 + 20aef2b commit 94b1c23

4 files changed

Lines changed: 17 additions & 18 deletions

File tree

src/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { OpenAPIHono } from '@hono/zod-openapi';
22
import type { Context } from 'hono';
33
import { cors } from 'hono/cors';
44
import packageJson from '../package.json';
5-
import { authMiddleware } from './middleware/auth.ts';
65
import { authRoutes } from './routes/auth/index.ts';
76
import { protectedProfileRoutes, publicProfileRoutes } from './routes/profile/index.ts';
87
import { privateRolesRoutes, publicRoleRoutes } from './routes/role/index.ts';
@@ -55,18 +54,16 @@ app.doc('/open-api.json', {
5554
}
5655
});
5756

58-
// Public routes
59-
app.route('/auth', authRoutes);
60-
app.route('/roles', publicRoleRoutes);
6157
// Handle static assets using Cloudflare Workers
6258
app.get('/assets/*', async (context: Context<EnvironmentBindings>) => context.env.ASSETS.fetch(context.req.raw));
6359

6460
// Public routes
61+
app.route('/auth', authRoutes);
62+
app.route('/roles', publicRoleRoutes);
6563
app.route('/profiles', publicProfileRoutes);
6664
app.route('/teams', publicTeamRoutes);
6765

68-
// Protected routes (after auth middleware)
69-
app.use('/*', authMiddleware);
66+
// Private routes
7067
app.route('/profiles', protectedProfileRoutes);
7168
app.route('/teams', protectedTeamRoutes);
7269
app.route('/roles', privateRolesRoutes);

src/routes/profile/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
2+
import { authMiddleware } from 'src/middleware/auth.ts';
23
import { z } from 'zod';
34
import { DBTables } from '../../constants/db.ts';
45
import { canModifyProfile } from '../../middleware/canModifyProfile.ts';
@@ -128,7 +129,7 @@ protectedProfileRoutes.openapi(
128129
content: { 'application/json': { schema: StatusResponseSchema } }
129130
}
130131
},
131-
middleware: [authorizeOrganizer] as const
132+
middleware: [authMiddleware, authorizeOrganizer] as const
132133
}),
133134
async (context) => {
134135
const body = context.req.valid('json');
@@ -176,7 +177,7 @@ protectedProfileRoutes.openapi(
176177
content: { 'application/json': { schema: StatusResponseSchema } }
177178
}
178179
},
179-
middleware: [authorizeVolunteer, canModifyProfile] as const
180+
middleware: [authMiddleware, authorizeVolunteer, canModifyProfile] as const
180181
}),
181182
async (context) => {
182183
const { id } = context.req.valid('param');
@@ -224,7 +225,7 @@ protectedProfileRoutes.openapi(
224225
content: { 'application/json': { schema: StatusResponseSchema } }
225226
}
226227
},
227-
middleware: [authorizeAdmin] as const
228+
middleware: [authMiddleware, authorizeAdmin] as const
228229
}),
229230
async (context) => {
230231
const { id } = context.req.valid('param');

src/routes/role/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
2+
import { authMiddleware } from 'src/middleware/auth.ts';
23
import { z } from 'zod';
34
import { DBTables } from '../../constants/db.ts';
45
import { authorizeOrganizer } from '../../middleware/createMiddleware.ts';
@@ -130,7 +131,7 @@ privateRolesRoutes.openapi(
130131
content: { 'application/json': { schema: StatusResponseSchema } }
131132
}
132133
},
133-
middleware: [authorizeOrganizer] as const
134+
middleware: [authMiddleware, authorizeOrganizer] as const
134135
}),
135136
async (context) => {
136137
const body = context.req.valid('json');
@@ -171,7 +172,7 @@ privateRolesRoutes.openapi(
171172
content: { 'application/json': { schema: StatusResponseSchema } }
172173
}
173174
},
174-
middleware: [authorizeOrganizer] as const
175+
middleware: [authMiddleware, authorizeOrganizer] as const
175176
}),
176177
async (context) => {
177178
const { id } = context.req.valid('param');
@@ -217,7 +218,7 @@ privateRolesRoutes.openapi(
217218
content: { 'application/json': { schema: StatusResponseSchema } }
218219
}
219220
},
220-
middleware: [authorizeOrganizer] as const
221+
middleware: [authMiddleware, authorizeOrganizer] as const
221222
}),
222223
async (context) => {
223224
const { id } = context.req.valid('param');

src/routes/team/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
2+
import { authMiddleware } from 'src/middleware/auth.ts';
23
import { z } from 'zod';
34
import { DBTables } from '../../constants/db.ts';
4-
import { authorizeAdmin, authorizeOrganizer, authorizeVolunteer } from '../../middleware/createMiddleware.ts';
5+
import { authorizeAdmin, authorizeOrganizer } from '../../middleware/createMiddleware.ts';
56
import {
67
type DataResponse,
78
generateDataResponeSchema,
@@ -76,8 +77,7 @@ publicTeamRoutes.openapi(
7677
description: 'Successful response',
7778
content: { 'application/json': { schema: generatePaginatedResponseSchema(z.array(TeamSchema)) } }
7879
}
79-
},
80-
middleware: [authorizeVolunteer] as const
80+
}
8181
}),
8282
async (context) => {
8383
const teams = await getAllTeams(context.env.database);
@@ -134,7 +134,7 @@ protectedTeamRoutes.openapi(
134134
content: { 'application/json': { schema: StatusResponseSchema } }
135135
}
136136
},
137-
middleware: [authorizeAdmin] as const
137+
middleware: [authMiddleware, authorizeAdmin] as const
138138
}),
139139
async (context) => {
140140
const { id } = context.req.valid('param');
@@ -177,7 +177,7 @@ protectedTeamRoutes.openapi(
177177
content: { 'application/json': { schema: StatusResponseSchema } }
178178
}
179179
},
180-
middleware: [authorizeAdmin] as const
180+
middleware: [authMiddleware, authorizeAdmin] as const
181181
}),
182182
async (context) => {
183183
const body = context.req.valid('json');
@@ -218,7 +218,7 @@ protectedTeamRoutes.openapi(
218218
content: { 'application/json': { schema: StatusResponseSchema } }
219219
}
220220
},
221-
middleware: [authorizeOrganizer] as const
221+
middleware: [authMiddleware, authorizeOrganizer] as const
222222
}),
223223
async (context) => {
224224
const { id } = context.req.valid('param');

0 commit comments

Comments
 (0)