Back

[SOLVED] Web SDK - Create Email Session

  • 0
  • Accounts
  • Web
toyman114
28 May, 2023, 06:31

What's weird is when I run this part of the code, the response URL triggered is success. which is "http://localhost:5173/".

TypeScript
      "auth0",
      "http://localhost:5173/",
      "http://localhost:5173/error"
    );```

When I execute this code below, this is where I'm getting status code 401:
```account
      .get()
      .then((response) => {
        console.log(response); // Success
      })
      .catch((error) => {
        console.log(error); // Failure
      });```
TL;DR
The user was trying to use the `createEmailSession` function with the email and password, but received a 401 error with the message "Invalid credentials. Please check the email and password." They then tried using Postman to test the request and received a successful API response. The user also shared some code they created and tried, which includes using the `getAccount` and `createAuth` functions. They mentioned that when they run `createOAuth2Session`, it refreshes the page, and they are still facing the same issue. Other users suggested using the `await` keyword with `createOAuth2Session` and
safwan
28 May, 2023, 06:32

Please use 3 backticks to format your code ( ` ) πŸ˜„

toyman114
28 May, 2023, 06:32

Sure, got it..

toyman114
28 May, 2023, 06:33

So, is there any missing implementation on the code I have?

safwan
28 May, 2023, 06:35

trying to check it

toyman114
28 May, 2023, 06:35

I also notice that when I check the nodemodule/appwrite implementation it is using TypeScript which is not same, since Im using only JSX πŸ™‚

safwan
28 May, 2023, 06:35

Could you try await-ing the createOAuth2Session

safwan
28 May, 2023, 06:35

That shouldn't be an issue

toyman114
28 May, 2023, 06:36

Yes, I did tried also this approach but still same error

safwan
28 May, 2023, 06:36

What might be happening is that you the code run createOAuth2Session, and immediately goes to account.get without waiting for the createOAuth2Session to finish.

safwan
28 May, 2023, 06:38

Okay so without the account.get, it works perfectly?

toyman114
28 May, 2023, 06:39

By the way, I also suspect this thing you mentioned. so another approached I tried is triggering account.get using function after few seconds createOAuth2Session

toyman114
28 May, 2023, 06:39

Still getting the same issue.

toyman114
28 May, 2023, 06:40

When I also run createOAuth2Session it refreshes the Page. is this normal ?

safwan
28 May, 2023, 06:41

which page refreshes? createOAuth2Session should open a new tab/window to handle the login flow. After which it should take you back to the original page

toyman114
28 May, 2023, 06:44

Sorry, It does not open a new page... It only uses the same page app

safwan
28 May, 2023, 06:46

That's fair. What you can do is do useEffect

safwan
28 May, 2023, 06:46

so whenever the page loads, you check if account.get returns anything. If it does, then go to whichever page. Else run createOAuth2Session

toyman114
28 May, 2023, 06:49

Sure, Let me try this approach

toyman114
28 May, 2023, 06:53

This is the code I just created, and seems to got having the same issue.

TypeScript

  useEffect(() => {
    // You can await here
    const client = new Client()
      .setEndpoint("http://20.0.0.150/v1")
      .setProject("645a9ddbef60e4b0ecc9");
    // setClient(client);
    const account = new Account(client);
    setAccount(account);
  }, []);

  const getAccount = async () => {
    const hasAccount = await account.get();

    hasAccount
      .then((response) => {
        console.log(response); // Success
      })
      .catch(() => {
        createAuth();
      });
  };
  const createAuth = async () => {
    return await account.createOAuth2Session(
      "auth0",
      "http://localhost:5173",
      "http://localhost:5173/error"
    );
  };```
toyman114
28 May, 2023, 06:53

<button className="m-3" onClick={getAccount}> View Client Credentials </button>

toyman114
28 May, 2023, 06:54
toyman114
28 May, 2023, 07:50

Hi, I tried another option which is using createEmailSession

TypeScript
        function (response) {
          console.log(response); // Success
        },
        function (error) {
          console.log("Err ", error); // Failure
        }
      );```
I got this error response:
```{
    "message": "Invalid credentials. Please check the email and password.",
    "code": 401,
    "type": "user_invalid_credentials",
    "version": "1.3.4"
}```
Request Payload

{ "email": "&#101;&#109;&#x61;&#105;&#x6c;&#64;&#103;&#x6d;&#x61;&#x69;&#x6c;&#46;&#x63;&#x6f;&#x6d;", "password": "password" }```

So, I tried using Postman app to test the request.

TypeScript
Request URL: http://20.0.0.150/v1/account/sessions/email
Request Body: {email: "email@gmail.com", password: "password"}

API request Response:

TypeScript
{"$id":"6472d9d2929b43fab336","$createdAt":"2023-05-28T04:34:27.594+00:00","userId":"645a9dc3ad09e4f03719","expire":"2024-05-27T04:34:26.600+00:00","provider":"email","providerUid":"email@gmail.com","providerAccessToken":"","providerAccessTokenExpiry":"","providerRefreshToken":"","ip":"172.18.0.1","osCode":"","osName":"","osVersion":"","clientType":"library","clientCode":"","clientName":"Postman Desktop","clientVersion":"7.31","clientEngine":"","clientEngineVersion":"","deviceName":"","deviceBrand":"","deviceModel":"","countryCode":"--","countryName":"Unknown","current":true}
toyman114
28 May, 2023, 09:11

Hello, Thank you for assistance, Everything works fine now when Im trying to use the registered users account, which I thought that organizations owner account is the one will authorized the login

Drake
28 May, 2023, 15:17

[SOLVED] Web SDK - Create Email Session

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