
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 ?

clients
"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);
},
};
};

auth.js

middleware.js
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"],
};
Recommended threads
- Error getting session: AppwriteException...
I get this error `Error getting session: AppwriteException: User (role: guests) missing scope (account)` when running in prod. As soon as I try running my app o...
- PR Review and Issue Assign?
I am not familiar with how things work here. I know that Issue have to be assigned before solving problem, It is for not wasting contributors time but I like t...
- OTP Session template not working and is ...
Okay so it has been a long while with the issue with OTP Session template, and currently I tried self-hosting and found out that it is linked with Verification ...
