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
- Anonymous Sessions using Node SDKs
I am attempting to use anonymous sessions with the Node SDK but I have simply no idea how to persist the session to a cookie, pass it to the session client, etc...
- Auth working for emulator but not for ph...
hey guys i'm using appwrite for expo react native it is working perfectly for emulator but does not working for physical devices can u explain any one and it is...
- general_unauthorized_scope
localhost oauth not working. general_unauthorized_scope error showing. { "message": "User (role: guests) missing scopes ([\"account\"])", "code": 401, ...