#need help with server/middleware

18 messages · Page 1 of 1 (latest)

open hatch
#

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.. 🙂

normal goblet
#

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

open hatch
#

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..

wraith rain
open hatch
#

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.. 😦

normal goblet
normal goblet
normal goblet
wraith rain
#

if you return in your middleware, the request will return 😄

#

(at the middleware, not in your route handler)

normal goblet
wraith rain
#

and btw, how would you pass the data to the route handler if you return the value?

wraith rain
#

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 🫡

normal goblet
# wraith rain you do it with createError ```ts throw createError({statusCode: 401, statusMess...

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