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
- Realtime api and labels as permission
in my tables i set labels as permission and real-time capabilities stopped working. Before when i was having "any" role everything was working. Note: user have...
- Register Disable
this is possible disable register but keep oauth login?
- "Restore project" button fails: "Invalid...
In the dashboard, it clicking "Restore project" fails. The request sent to `PATCH https://cloud.appwrite.io/v1/projects/:project_id` with payload `{status: "act...