Back

problem with login in nextjs

  • 0
  • Auth
  • Web
adminini
21 Jul, 2024, 01:14

Follwoing the docs https://appwrite.io/docs/references/cloud/server-nodejs/account (email sessions) here & got:

⨯ Error: Login failed: account.createEmailSession is not a function

TL;DR
Developers are facing a login issue in Next.js. To reset the rate limit when debugging, change the `_APP_OPTIONS_ABUSE` value to `disabled` in the `.env` file and restart Docker containers. Ensure proper cookie value when setting it on the 'my-custom-session'. Update to `createEmailPasswordSession()` instead of `createEmailSession()` in newer SDK versions. Fix the 'Logout failed' issue by adjusting user roles and scopes. Remember to update the documentation accordingly.
Ryan
21 Jul, 2024, 01:17

Which version of Appwrite and the node-appwrite SDK are you using?

adminini
21 Jul, 2024, 01:24

"node-appwrite": "^13.0.0",

Ryan
21 Jul, 2024, 01:24

Are you using Cloud or self-hosting?

adminini
21 Jul, 2024, 01:25

self-hosting

Ryan
21 Jul, 2024, 01:25

Which version are you running?

adminini
21 Jul, 2024, 01:26

Version 1.5.7

Ryan
21 Jul, 2024, 01:26

In the newer versions of the SDK, createEmailSession() was changed to now be createEmailPasswordSession(). It still works the same way, just renamed to be clearer

adminini
21 Jul, 2024, 01:27

👍 👍

adminini
21 Jul, 2024, 01:27

I suggest to update the docs 🙂

Ryan
21 Jul, 2024, 01:28

The docs on the page you sent were already updated to show the changes https://appwrite.io/docs/references/1.5.x/server-nodejs/account#createEmailPasswordSession

adminini
21 Jul, 2024, 01:37

Okay thanks for the quick support! Followup question: after login-in and try to sign out i got: `Logout failed: User (role: guests) missing scope (account)

`

adminini
21 Jul, 2024, 01:38

using this code on lib/appwrite: ``` export async function loginUser(email: string, password: string) {

TypeScript
try {
  const session = await account.createEmailPasswordSession(email, password);

  // Log the session to verify
  console.log("Session created:", session);

  // Ensure session.$id is valid
  if (!session.$id || session.$id.length > 36 || !/^[a-zA-Z0-9._-]+$/.test(session.$id)) {
    throw new Error("Invalid session ID");
  }

  // Store session in cookies
  cookies().set("my-custom-session", session.$id, {
    httpOnly: true, // Cookie is accessible only by the web server
    secure: true, // Cookie will be sent only over HTTPS
    sameSite: "strict", // Cookie will be sent only in first-party contexts
  });

  return session;
} catch (error) {
  console.error("Login failed:", error);
  throw new Error("Login failed: " + error.message);
}

}

export const logoutUser = async () => { await account.deleteSession('current'); };

export const getSession = async () => { return await account.getSession('current'); }; ```

Ryan
21 Jul, 2024, 01:47

It looks like you're setting the wrong value for the cookie, you want to be storing the value from session.secret to the cookie instead. The reason it's failing it because it doesn't register as being logged in with the value being incorrect

adminini
21 Jul, 2024, 01:50

Like this: cookies().set(session.secret, session.$id, { httpOnly: true, // Cookie is accessible only by the web server secure: true, // Cookie will be sent only over HTTPS sameSite: "strict", // Cookie will be sent only in first-party contexts }); ?

Ryan
21 Jul, 2024, 01:51

Like this:

TypeScript
cookies().set("my-custom-session", session.secret, {
  httpOnly: true,
  secure: true,
  sameSite: "strict",
});
adminini
21 Jul, 2024, 01:53

Actually when I log-in it returned successfully - I try now with your suggestion thanks - got some rate limit😁 - how long it takes the rate limit?

adminini
21 Jul, 2024, 01:53

Can I reset the rate limit when debugging?

Ryan
21 Jul, 2024, 01:55

There is an environment variable you can change to disable rate limits for self-hosted, set the value of _APP_OPTIONS_ABUSE to disabled in your .env file and then restart the docker containers

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