Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Learn more at https://developers.cloudflare.com/workers/
*/
import { Hono } from 'hono';
import { cors } from 'hono/cors';

import {
handleCreateRegOptions,
Expand All @@ -22,6 +23,18 @@ import {

const app = new Hono();

// Set up CORS headers
app.use('*', async (ctx, next) => {
const corsMiddleware = cors({
origin: [
'http://localhost:8787',
// TODO: Populate other origins from `ctx.env` later
],
});

return corsMiddleware(ctx, next);
});

app.get('/registration/options', handleCreateRegOptions);
app.get('/authentication/options', handleCreateAuthOptions);
app.post('/registration/verify', handleVerifyRegResponse);
Expand Down
6 changes: 6 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@ describe('Routing tests', () => {
const response = await SELF.fetch('https://example.com', { method: 'PUT' });
expect(response.status).toBe(404);
});

it('sets cors headers', async () => {
const response = await SELF.fetch('https://example.com/registration/options');

expect(response.headers.get('Access-Control-Allow-Origin')).toMatch('http://localhost:8787');
});
});