CrisOG
CrisOG
Explore posts from servers
HHono
Created by CrisOG on 8/31/2024 in #help
Middleware errors are not being returned in response type
I have a user route using an auth middleware,
export const usersRouter = new Hono()
.use(authorize)
.get('/', async (c) => {
const user = await User.getByUserID(c.var.auth.userID);

if (!user) {
return c.json({ error: 'User not found' }, 404);
}

return c.json(user, 200);
})
export const usersRouter = new Hono()
.use(authorize)
.get('/', async (c) => {
const user = await User.getByUserID(c.var.auth.userID);

if (!user) {
return c.json({ error: 'User not found' }, 404);
}

return c.json(user, 200);
})
And this is the auth middleware,
import { createMiddleware } from 'hono/factory';
import { lucia } from './lucia';

export const authorize = createMiddleware<{
Variables: {
auth: {
userID: string;
sessionID: string;
};
};
}>(async (c, next) => {
const cookieHeader = c.req.header('Cookie');

if (!cookieHeader) {
return c.json({ error: 'Unauthorized' }, 401);
}
// Code omitted ...
});
import { createMiddleware } from 'hono/factory';
import { lucia } from './lucia';

export const authorize = createMiddleware<{
Variables: {
auth: {
userID: string;
sessionID: string;
};
};
}>(async (c, next) => {
const cookieHeader = c.req.header('Cookie');

if (!cookieHeader) {
return c.json({ error: 'Unauthorized' }, 401);
}
// Code omitted ...
});
But, the response type has only 2 possible status code.
Argument of type '401' is not assignable to parameter of type '200 | 404'.ts(2345)
Argument of type '401' is not assignable to parameter of type '200 | 404'.ts(2345)
This is a problem because I write some tests that expect this error code when accessing an auth protected route without auth.
2 replies