So I'm attempting to re-do my signup process because I ran into issues adding to the database at the same time.
But im having issues. I read the docs and it absolutely cannot see what im doing wrong. It creates a account just fine on appwrite but wont redirect me to my dashboard.
sign up form code
import {
getLoggedInUser,
createUserClient,
createSessionClient,
} from "@/lib/server/appwrite";
import { redirect } from "next/navigation";
import { ID } from "appwrite";
import { cookies } from "next/headers";
import Link from "next/link";
export default async function RegisterPage() {
const user = await getLoggedInUser();
if (user) redirect("/dashboard");
async function handleRegister(formData) {
"use server";
const organization = formData.get("organization");
const email = formData.get("email");
const password = formData.get("password");
const repeatPassword = formData.get("repeatPassword");
const missionStatement = formData.get("missionStatement");
const organizationWebsite = formData.get("organizationWebsite");
if (password !== repeatPassword) {
// handle this error better
console.log("Passwords do not match");
}
const { account } = await createSessionClient();
try {
await account.create(ID.unique(), email, password, organization);
const session = await account.createEmailPasswordSession(email, password);
cookies().set("my-custom-session", session.secret, {
path: "/",
httpOnly: true,
sameSite: "Strict",
secure: true,
});
redirect("/dashboard");
// const data = { organization, missionStatement, organizationWebsite };
// await saveToDatabase(data);
} catch (error) {
console.log("Registration failed: " + error.message);
}
}
return (
form code stuff
)
my appwrite code
// src/lib/server/appwrite.js
"use server";
import { Client, Account } from "node-appwrite";
import { cookies } from "next/headers";
export async function createSessionClient() {
const client = new Client()
.setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT)
.setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT);
const session = cookies().get("my-custom-session");
if (!session || !session.value) {
console.log("No session");
}
client.setSession(session.value);
return {
get account() {
return new Account(client);
},
};
}
export async function createUserClient() {
const client = new Client()
.setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT)
.setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT);
return {
client,
account: new Account(client),
};
}
export async function getLoggedInUser() {
try {
const { account } = await createSessionClient();
return await account.get();
} catch (error) {
console.log(error);
return null;
}
}
export async function logout() {
try {
const { account } = await createSessionClient();
await account.deleteSession("current");
cookies().delete("my-custom-session");
} catch (error) {
console.error("Error during logout:", error.message);
throw error;
}
}
export async function createAdminClient() {
const client = new Client()
.setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT)
.setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT)
.setKey(process.env.NEXT_APPWRITE_KEY);
return {
get account() {
return new Account(client);
},
};
}
Recommended threads
- Rate límit reset
I've made an error in the code by not setting a limit on how much rows should i get per request because i was working with a small db for testing. Last night Iv...
- How to set permissions using the createO...
Hi, I'm trying to create a set of registers in a table using the tableDB.createOperations function, but it seems like it doesnt support/expect the usage of the ...
- Fulltext index creation fails due to Inn...
I'm running a self-hosted Appwrite 1.8.1 instance and encountering an issue when creating fulltext indexes on a collection. **The Problem:** Fulltext index cre...