Skip to content
Back

[URGENT] Suddenly unable to stay logged in.

  • 0
  • Auth
  • Cloud
Popt
11 Apr, 2025, 14:20

I'm not sure if this is a problem with my application or with appWrite, but I suddenly can't get the status and I don't know why. This problem is a little hard to explain.

For example, const { account } = createSessionClient(sessionSecret); When the value of cookieSession is correct, it will be in a state of loading forever.

TL;DR
Developers experiencing issues with staying logged in due to repeated calls to getCurrentUser. They suspect a problem with AppWrite or their application. Issue occurs when session secret is correct, causing infinite loading. Solution: Check the session secret value and update code accordingly to troubleshoot the problem.
Popt
11 Apr, 2025, 14:23

Detailed explanation: First of all this happened out of the blue, I swear I didn't change any code. I just said that when the session secret value is correct it won't give me any response. But when the session secret is wrong appwrite will throw an error, then I will judge that the user is not valid. But the problem is that if the user is logged in and has valid credentials it will just keep loading, I will proceed to the next step.

Popt
11 Apr, 2025, 14:23

[URGENT] Suddenly unable to stay logged in.

Popt
11 Apr, 2025, 14:28

export async function getCurrentUser(): Promise<User | null> { try { const cookieSession = await getCookieSession();

TypeScript
if (!cookieSession) return null;

const { account } = createSessionClient(cookieSession);
const { databases } = createAdminClient();

const currentUser = await account.get();

const users = await databases.listDocuments(
  process.env.NEXT_PUBLIC_APPWRITE_DATABASE_ID!,
  process.env.NEXT_PUBLIC_APPWRITE_COLLECTION_USERS!,
  [Query.equal("userId", currentUser.$id), Query.limit(1)],
);

console.log({ users });

if (users.total === 0) return null;

const user = users.documents[0] as UserDetails;

const onboardings = await databases.listDocuments(
  process.env.NEXT_PUBLIC_APPWRITE_DATABASE_ID!,
  process.env.NEXT_PUBLIC_APPWRITE_COLLECTION_ONBOARDING!,
  [Query.equal("userRefId", user.$id), Query.limit(1)],
);

const notifications = await getNotifications();

const createdOrganizations = await Promise.all(
  (
    (
      await databases.listDocuments(
        process.env.NEXT_PUBLIC_APPWRITE_DATABASE_ID!,
        process.env.NEXT_PUBLIC_APPWRITE_COLLECTION_ORGANIZATIONS!,
        [Query.equal("creatorRefId", user.$id)],
      )
    ).documents as Organization[]
  ).map(async (organization) => {
    const members = (
      await databases.listDocuments(
        process.env.NEXT_PUBLIC_APPWRITE_DATABASE_ID!,
        process.env.NEXT_PUBLIC_APPWRITE_COLLECTION_MEMBERS!,
        [Query.equal("organizationRefId", organization.$id)],
      )
    ).documents;

    return { ...organization, members };
  }),
);
Popt
11 Apr, 2025, 14:28

const joinedOrganizations = await getJoinedOrganizations( user.joinedOrganizationsRefId, );

TypeScript
return {
  ...currentUser,
  details: {
    ...user,
    notifications,
    joinedOrganizations,
    onboarding: onboardings.documents[0],
    createdOrganizations: createdOrganizations,
  },
} as unknown as User;

} catch (error) { console.log("Failed to get current user", error); return null; } }

Popt
11 Apr, 2025, 14:28

The above is the complete code

Popt
11 Apr, 2025, 14:28

You can see that I put a console.log

Popt
11 Apr, 2025, 14:31

It keeps outputting repeatedly, which proves that getCurrentUser is being called repeatedly.

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