Skip to content
Back

Issue with node-appwrite SDK and Edge Runtime in Next.js on Vercel

  • 0
  • Auth
  • Web
aivan6162
12 Feb, 2025, 22:38

Hi guys , for my Next.js and Appwrite project, I’m using Auth.js for authentication. I’m facing an issue because I’m using the node-appwrite SDK in server functions and also to create the Admin and Session clients. These functions and clients (which rely on node-appwrite) are used in the authentication process within my auth.js file. Finally, in middleware.js, the auth function (from auth.js) runs, but when I try to deploy to Vercel, I get an error stating that the Edge Runtime doesn’t support node-appwrite.” Do you know how to solve this issue with edge run time of Next.js ?

TL;DR
Developers are encountering an issue with using the node-appwrite SDK in their Next.js project on Vercel's Edge Runtime, specifically with server functions and clients. They are getting an error that Edge Runtime doesn't support node-appwrite. To resolve this, they might need to find an alternative method for authentication that is compatible with Vercel's Edge Runtime.
aivan6162
12 Feb, 2025, 22:54

clients

TypeScript
"use server";

import { Account, Avatars, Client, Databases, Storage } from "node-appwrite";
import { appwriteConfig } from "./appwriteConfig";
import { cookies } from "next/headers";

export const createAdminClient = async () => {
  const client = new Client()
    .setEndpoint(appwriteConfig.endpoint)
    .setProject(appwriteConfig.projectId)
    .setKey(appwriteConfig.apiKey);

  return {
    get account() {
      return new Account(client);
    },

    get databases() {
      return new Databases(client);
    },
    get storage() {
      return new Storage(client);
    },
  };
};

// Session client
export const createSessionClient = async () => {
  const client = new Client()
    .setEndpoint(appwriteConfig.endpoint)
    .setProject(appwriteConfig.projectId);

  const cookieStore = await cookies();

  const session = cookieStore.get("session");

  if (!session || !session.value) {
    throw new Error("No session");
  }

  if (session) {
    client.setSession(session.value);
  }

  return {
    get account() {
      return new Account(client);
    },

    get databases() {
      return new Databases(client);
    },
    get avatars() {
      return new Avatars(client);
    },
    get storage() {
      return new Storage(client);
    },
  };
};
aivan6162
12 Feb, 2025, 22:56

auth.js

aivan6162
12 Feb, 2025, 22:59

middleware.js

TypeScript
import { NextResponse } from "next/server";
import { auth } from "./lib/auth";

// export const middleware = auth;

export default auth(async (req) => {
  const { auth: session, nextUrl } = req;
  const currentPath = nextUrl.pathname;
  const searchParams = req.nextUrl.searchParams;
  const validPeriods = ["7", "28", "60", "90", "180", "365"];

  console.log("middleware");

  console.log(req);
  console.log("auth", req.auth);

  if (currentPath === "/calculadora") {
    const period = searchParams.get("period");

    const hasInvalidParams = Array.from(searchParams.keys()).some(
      (param) => param !== "period",
    );

    if (hasInvalidParams) {
      return NextResponse.redirect(new URL("/404", req.url));
    }

    if (period && !validPeriods.includes(period)) {
      return NextResponse.redirect(new URL("/404", req.url));
    }
  }

  const isAccountRoute = currentPath.startsWith("/account");
  const protectedRoutes = ["/account"];
  const authRoutes = ["/login", "/signup"];

  if (!session?.user) {
    if (protectedRoutes.includes(currentPath) || isAccountRoute) {
      return NextResponse.redirect(new URL("/login", req.url));
    }
  } else {
    if (authRoutes.includes(currentPath)) {
      return NextResponse.redirect(new URL("/", req.url));
    }
  }

  return NextResponse.next();
});

export const config = {
  matcher: ["/login", "/signup", "/account", "/account/:path*", "/calculadora"],
};
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