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 need to implement SSR login or disable SSR to solve the authentication issue. Make sure to check cookies and local storage for saving credentials. The problem might be that the user is not logged in when trying to fetch account information.
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

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