Hi. i want to create middleware that would run when i am making request to /api/login so it would check my inputs and if they doesnt met requirments it would not continue to /api/login. is tehre any way to do that?As documentation say, server/middleware should not have any return. it can just modify request or something like that.. using nuxt 2 + express i cando that , i can also do that in node + express, but i can't in nuxt 3?? or i just can't find solution???? google or chatGPT has no knowledge how to do that also.. 🙂
#need help with server/middleware
18 messages · Page 1 of 1 (latest)
Hey, i found a way like that:
server/middleware/auth.ts:
import { getServerSession, getServerToken } from "#auth"
export default defineEventHandler(async (event) => {
const session = await getServerSession(event, authOptions)
const jwt = await getServerToken(event, authOptions)
if (getRequestURL(event).pathname.startsWith('/api') && (!session || !jwt)) {
return { error: true, message: "Unauthorized" }
}
});```
But i don't know is it best approach or not, ping me if you find something.
also its returning a object in middleware, i don't think its best approach
well with authentification is easier. i am going to use route middleware for that.. but in this server middleware i basicaly want to check data that is going from front end to /api/login in body.. and if fields are empty or password is too short and stuff like that then return some error message.. but in this server middleware there shouldn;t be any return... is it me or nuxt 2 was better at middleware and some other stuff? i found hard time doing simple stuff with nuxt 3..
if you need to pass data from middleware to the route endpoint, you can use the context:
export default defineEventHandler((event) => {
event.context.foo = 'bar'
})
In your route handler, you can access event.context.foo to retrieve bar
yes this part is easy. i need to get username, email and password from body, i know how to do that, i need to check if they are valid, i also know how to do that and then i need to do nothing if they are valid, or stop it there and send response back to front end with message that inputs are not valid.. i dont know i can send response back from middleware. i dont want to throw error. and i think it is not possible to send response... so kind of stuck there.. now i am running same input validation code on my /api/login and /api/register.. 😦
why the hell they removed express support for this kind of stuff and make almost useless server middleware.. 😦
maybe you can use utils instead of middleware because it sounds like code duplication issue right?
yeah that's true, i don't like the nuxt default server thing, its complicated than express or koa
👍 i refactored my code like this:
server/middleware/auth.ts
if (itsInMyCase) event.context.isAuth = true;
in api/order.get.ts
if (!event.context.isAuth) return {error: true, message: "Unauthorized"}
btw why returning value from middleware is not recommending in docs? i didn't get that part, its causes any issue or something?
usually a returns means that you're finished with the job (in this case the function). The middleware extends the server route basically. It modifies the request before it's processed
if you return in your middleware, the request will return 😄
(at the middleware, not in your route handler)
yes thats true but if user is unauthorized, it should be return a error before router right?
and btw, how would you pass the data to the route handler if you return the value?
you do it with createError
throw createError({statusCode: 401, statusMessage: whatever})
if you do so, the app is not going to be navigable though, server middlewares run on every request, even page requests
that's false, gonna delete 🫡
yeah but another thing with returning or throwing error it can overide to every route.
For solving that issue i added:
getRequestURL(event).pathname.startsWith('/api')
but its seems like sketchy, i changed middleware to like you preffered method, its more natural which is;
server/middleware/auth.ts
if (itsInMyCase) event.context.isAuth = true;
in api/order.get.ts
if (!event.context.isAuth) return {error: true, message: "Unauthorized"}
🙏 thank you again