Which project does this relate to?#
Start
Describe the bug#
Classic code I have in my better auth with TS project for a long time :
export const authUserRequiredMiddleware = createMiddleware({
type: 'function'
}).server(async ({ next }) => {
const { headers } = getWebRequest()
const session = await auth.api.getSession({
headers,
query: {
disableCookieCache: true
}
})
if (!session) {
setResponseStatus(401)
throw new Error('Unauthorized')
}
return next({ context: { user: session.user } })
})
I'm pretty sure I had the correct status code (401) with my Unauthorized message.
Now, I got a 500 in any case.. I would like to throw with a custom status code. Not sure if there is a regression or is it me ?
Your Example Website or App#
https://codesandbox.io/p/devbox/bold-johnson-3ly6mc?workspaceId=ws_VUPqXCcptK4abDwy6H7mEq
Steps to Reproduce the Bug or Issue#
- Go to
https://codesandbox.io/p/devbox/bold-johnson-3ly6mc?workspaceId=ws_VUPqXCcptK4abDwy6H7mEq
- Click on the button
- Check network tab
- Check that we got 500 instead of 401
Expected behavior#
As a user (not logged), I should get a 401 status instead of 500.
Screenshots or Videos#
No response
Platform#
- Router / Start Version: 1.131.36
- OS: Mac
- Browser: Chrome
- Browser Version: 139.0.7258.155
- Bundler: Vite
- Bundler Version: 7.0.0
Additional context#
I think i worked a few weeks ago. 80% sure.
does this issue still exist on the latest version?