QLiêmOriginal post
Web Developer
I am implementing SSR authentication using Next.js Server Actions and node-appwrite.
The session is created successfully, but session.secret is empty.
Here is my server-side login code:
"use server";
import { Client, Account } from "node-appwrite";
import { cookies } from "next/headers";
export const signInUser = async ({ email, password }) => {
try {
const client = new Client()
.setEndpoint(process.env.APPWRITE_ENDPOINT)
.setProject(process.env.APPWRITE_PROJECT_ID);
const account = new Account(client);
const session = await account.createEmailPasswordSession(email, password);
console.log(session);
if (!session.secret) {
console.log("Secret Empty");
}
const cookieStore = await cookies();
cookieStore.set("appwrite-session", session.secret, {
path: "/",
httpOnly: true,
sameSite: "strict",
secure: true,
});
return { success: true, sessionId: session.$id };
} catch (error) {
console.error("Appwrite Login Error:", error);
}
};
Summary
Issue: session.secret is empty when using node-appwrite in Next.js Server Action.
Solution: The session is created successfully, but the session.secret is empty. This may be because the cookies are not being properly stored. Ensure that the cookieStore is correctly instantiated before setting the cookie. It's important to handle possible errors that may occur during the cookie setting process.