Back

Need Help on Authentication problem

  • 1
  • Auth
  • Web
Mohiuddin
20 Sep, 2024, 14:08

I have created an account with appwrite then logged it. both of them are working fine. but when i'm trying to get the user by using this method:

TypeScript
export const getAccount = async () => {
  try {
    const user = await account.get();
    return user;
  } catch (error: any) {
    console.error("An error occurred while fetching user:", error);
  }
};

it's not getting any user info. how can i fix this issue?

TL;DR
Developers are discussing authentication issues in a NextJS app using Appwrite. They are having trouble retrieving user information despite successful account creation and login. A potential solution could be to verify if the user is properly logged in and to check the authentication setup in the code.
D5
20 Sep, 2024, 14:41

You're getting error?

D5
20 Sep, 2024, 14:41

If so, it could be that the user is not logged in

D5
20 Sep, 2024, 14:41

What error are you getting?

Mohiuddin
20 Sep, 2024, 14:42

I am not getting any error. it's just not giving me the user info. here is my auth related code.

TypeScript

// Create APPWRITE ACCOUNT
export const createAccount = async (user: CreateAccountParams) => {
  try {
    const newUser = await account.create(
      ID.unique(),
      user.email,
      user.password,
      user.name,
    );

    await loginAccount({
      email: user.email,
      password: user.password
    });

    return parseStringify(newUser);

  } catch (error: any) {
    console.error("An error occurred while creating a new user:", error);
    throw error; // Re-throw the error to handle it further up the call stack if needed
  }
};

// Login APPWRITE ACCOUNT
export const loginAccount = async (user: LoginAccountParams) => {
  try {
    const loginUser = await account.createEmailPasswordSession(
      user.email,
      user.password
    );
    console.log(loginUser, "loginUser");
    return parseStringify(loginUser);
  } catch (error: any) {
    console.error("An error occurred while fetching user:", error.message);
  }
};

// GET APPWRITE Account
export const getAccount = async () => {
  try {
    const user = await account.get();
    return user;
  } catch (error: any) {
    console.error("An error occurred while fetching user:", error);
  }
};
Mohiuddin
20 Sep, 2024, 14:44

I also check the cookies and localstorage. it's not saving my credential.

Mohiuddin
20 Sep, 2024, 14:44

like secret key

D5
20 Sep, 2024, 14:44

Are you using some web framework like NextJS?

Mohiuddin
20 Sep, 2024, 14:44

next js. yes

D5
20 Sep, 2024, 14:48

Then probably it's because of SSR. You will probably need to disble SSR or implement SSR login: https://appwrite.io/docs/tutorials/nextjs-ssr-auth/step-1

Mohiuddin
21 Sep, 2024, 16:11

@D5Hey man, I have read the documentation and watched the tutorial from Appwrite, but I want to use the CSR method. I don’t want to change the whole config file to SSR. Is that possible?

D5
21 Sep, 2024, 17:02

Yes, I think you can use client in NextJS

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