Back

Nextjs auth using middleware (error)

  • 0
  • Self Hosted
  • Web
Badeck
20 Mar, 2024, 19:00

Hello everyone,

I am trying to use a middleware to protect some routes on my NextJS project.

I am running into an error. Could someone help me understand what it means ?

NB : I am using appwrite 1.5 (self-hosted)

edit: typo

TL;DR
Developers encountering an error with Next.js auth middleware usage, specifically with node runtime limitations. A workaround is suggested by checking if a cookie is set for auth. However, this may have security implications. Waiting on Next.js support for node runtime in middleware. Two relevant appwrite GitHub pull requests are linked.
Badeck
20 Mar, 2024, 19:30

I found the following in the NextJS documentation :

"Middleware currently only supports the Edge runtime. The Node.js runtime can not be used."

So "import { Client, Account } from "node-appwrite"; is probably the issue here.

What do you think about just checking if there is a cookie ?

Probably the worst idea ever right ? Because everyone could just generate a custom cookie... Right ?

Soooo basically, I am f***ed and need to wait for Node.js runtime support by NextJS for middleware ?

Ryan
20 Mar, 2024, 19:42

This is a known issue for the SDK, and I'm fairly sure they're working on a fix to see if they can polyfill to allow it to be used for Edge runtime. For my project using this, I do just check if a cookie is set and then do the auth checks in a layout instead until I'm able to move it to middleware.

https://github.com/appwrite/sdk-generator/pull/795 https://github.com/appwrite/sdk-generator/pull/799

Badeck_Phone
20 Mar, 2024, 19:58

Thanks Ryan for your time and your answer.

I will follow this closely. ❤️

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more