Back

How to obtain the access token with user session creation via email?

  • 0
  • Accounts
  • Web
0liveiraVictor
20 Mar, 2023, 22:08

No, I'm using a client sdk. As an individual user.

TL;DR
The user is trying to obtain an access token with a user session creation via email. They are using a client SDK and are encountering an issue where the returned cookie is not being sent back in the results. The user is using Insomnia to simulate the login and wants to understand why the cookie is not returned. A suggestion to resolve the issue is to add a request to get a JWT and send it back with the results, allowing the user to connect using the JWT.
Binyamin
20 Mar, 2023, 22:10

Okay, I think I can spot the issue

Binyamin
20 Mar, 2023, 22:10

When you're running the account.createEmailSession action the returned cookie are not being sent back in the results.

0liveiraVictor
20 Mar, 2023, 22:14

I used this same api in an application using react, and when I created the user session, via email, it automatically saved the cookie in the browser, in a way that I still don't understand well (I'm a beginner).

I'm using insomnia to simulate this login, similarly to what I did in react, however I believe that the return data should be the cookie. Only it is not returned.

Binyamin
20 Mar, 2023, 22:14

You can see here at \appwrite\src\client.ts That you never getting back the Cookies you just get the json And the only you will have those cookies is if you do it in window mode Otherwise you can use the REST option with JWT

Binyamin
20 Mar, 2023, 22:15

In browser it will work But in your code you can see that the client is node

Binyamin
20 Mar, 2023, 22:15
0liveiraVictor
20 Mar, 2023, 22:16

let me watch it calmly one moment

Binyamin
20 Mar, 2023, 22:21

My suggestion for a quick way around it is it just to add request to get a JWT and send it back with the results, and from then you can connect using the JWT

TypeScript
const result = await account.createEmailSession(email, password);
const jwt    = await account.createJWT();

return response.status(201).json({
    message: "...",
    data   : {result, jwt}
});
0liveiraVictor
20 Mar, 2023, 22:23

I'm trying to understand what you're trying to say. What do you mean I would only get the cookie back in window mode? In case, I know that in the browser, somehow the cookie is saved and it works. But for me to test on my backend I would need to have a JWT, is that it? Is there a problem with insomnia requests? I had not tested with postman.

0liveiraVictor
20 Mar, 2023, 22:24

Sorry for the delay, I have difficulties with the English language.

Binyamin
20 Mar, 2023, 22:27

It's bit complex issue But every time you do a request and cookies are returned they are saved inside a Cookie Jar When you running this action inside the browser or any other API tester it will take care of the Jar

But, in your situation is not insomnia issue but the node-fetch one where the account functions running on So when insomnia contacting your endpoint aka create_email_session it have all the cookies, but when your NodeJS app is contacting your AppWrite engine it does that using node-fetch and is not saving the Cookies inside the Jar

Binyamin
20 Mar, 2023, 22:27

Can you show me your node package.json?

0liveiraVictor
20 Mar, 2023, 22:27

Hum, ok. I´ĺl try.

0liveiraVictor
20 Mar, 2023, 22:29

a little complex for me still.

0liveiraVictor
20 Mar, 2023, 22:31

for sure.

0liveiraVictor
20 Mar, 2023, 22:31
Binyamin
20 Mar, 2023, 22:33

Every instance can store the cookies inside the jar and provide them when there is a new request but not the node fetch in your case

Binyamin
20 Mar, 2023, 22:36

And I don't think you can have a way around it without JWT

0liveiraVictor
20 Mar, 2023, 22:45

I understand. I'll test with JWT, I hadn't thought of that context. In case, if someone consumes my custom api, in the browser, it wouldn't need to access via JWT, would it? The JWT would just be for me to test as an authenticated user, right?

Binyamin
20 Mar, 2023, 22:45

No The JWT and all the app data is inside your express code and is not exposed unless you send it somehow

0liveiraVictor
20 Mar, 2023, 22:49

ok, my friend.

Binyamin
20 Mar, 2023, 22:49

👍

0liveiraVictor
20 Mar, 2023, 22:50

Thank you for your support!

Drake
20 Mar, 2023, 23:15

You could try to manually make the API call using fetch or a axios and checking the response headers for the cookie

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