#-

38 messages · Page 1 of 1 (latest)

fallen echoBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

      🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in <id:customize>

      ✅ You can mark a message as the answer for your post with `Right click -> Apps -> Mark Solution`
      (if you don't see the option, try refreshing Discord with Ctrl + R)
small vigil
#

you can make a nodejs route handler/api route and fetch that api route in your middleware

#

oh yeah forgot middleware captures that as well...

use the middleware matcher to exclude that api route

lucid sapphire
#

@opal mango is there any specific reason why u want to use prisma in middleware?

#

I dont think this is the right way to do that..generally middlewares are for anything to protect before actually handling the api endpoints..u can do this in api endpoints itself

#

What u mean by repetitive?

#

U can add conditions

#

Okay got it

#

So u need to access the db before the endpoint

#

Currently what I could think of is creating a middleware function which can handle ur prisma..and can wrap api function

#

Exclude from middleware?

#

U can simple do if else …if the url is that u want to exclude then simply
return NextResponse.next()

lucid sapphire
#

Pathname only gives the path like /api/auth

#

U dont have to write complete url

#

Show ur complete middleware

#

Do one thing

#

Fetch before the if else

#

What that auth/session is doing first of all can u show?

#

It matters ..u have added a simple fetch

#

But u are not storing it in a variable?

#

Just get?

#

Ok if ít doesn’t matters then simple what u could do is…just fetch that api and on next line return nextresponse.next

#

No need of if else

#

Just add then a condition if url not equal to /auth/session then only fetch other wise just just return next

#

No this condition is different

#
import { NextRequest, NextResponse } from 'next/server'

export async function middleware(request: NextRequest) {
  if (!request.url.startsWith('http://localhost:3000/api/auth/session')) {
   await fetch('http://localhost:3000/api/auth/session')

return NextResponse.next()

  } 
   
    return NextResponse.next()

}
#

Try this..still im not sure..this should work

lucid sapphire
#

The url is false check !

safe hill
#

how about next-connect if you want kinda middleware for node,js runtime?

safe hill
#

i think so. it’s just lib so it works self hosted servers as well as AWS lambda.

#

Edge Runtime is subset of Node.js

#

i understand your frustration

#

or host on AWS, GCP and so on

#

prisma is implemented in Rust tokio so it’s not that simple to work in Edge Runtime, Cloudflare Worker

lucid sapphire
#

@opal mango the current best approah is..u need to create a middleware function for ur api route..that what I said previously.

opal mango