Back

cant create session

  • 0
  • Web
DeFacedFace
26 Sep, 2024, 04:46

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

TypeScript
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
)
TL;DR
Developers are facing an issue where the signup process successfully creates an account on Appwrite but fails to redirect to the dashboard. The problem lies in the code not redirecting users after successful signup. To solve this, include `return` before `redirect("/dashboard");` in the handleRegister function.
DeFacedFace
26 Sep, 2024, 04:47

my appwrite code

TypeScript
// 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);
    },
  };
}
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more