Back

Fetching appwrite server

  • 0
  • Accounts
  • Web
  • Cloud
otik
3 Jul, 2023, 15:41

Hi, i would like to ask you guys. How can i fetch appwrite cloud, that i could create email session.

I made this:

const response = await fetch(${process.env.APPWRITE_ENDPOINT}account/sessions/email, { method: 'POST', headers: { 'x-appwrite-project': process.env.APPWRITE_PROJECT_ID, 'X-appwrite-Key': process.env.APPWRITE_API_KEY,

TypeScript
            },
        })

But i dont know how can i add email and password, i tried to add it into the body, like: JSON.stringify({email: ...., password: ....}), but it does not work.

TL;DR
User is seeking assistance with fetching Appwrite server to create an email session. They are unsure of how to add the email and password to the request body. Solution: User should add the email and password to the request body using JSON.stringify(), like this: ``` const response = await fetch(`${process.env.APPWRITE_ENDPOINT}account/sessions/email`, { method: 'POST', headers: { 'x-appwrite-project': process.env.APPWRITE_PROJECT_ID, 'X-appwrite-Key': process.env.APPWRITE_API_KEY, 'Content-Type': 'application/json', }, body: JSON.stringify({ email
Drake
3 Jul, 2023, 15:42

Btw, it's best to use 3 back ticks with multi-line code. See https://www.markdownguide.org/extended-syntax/#syntax-highlighting

Binyamin
3 Jul, 2023, 15:42

You can do that by adding a stringify body of your data, like so:

TypeScript
const response = await fetch(${process.env.APPWRITE_ENDPOINT}account/sessions/email, {
                method: 'POST',
                headers: {
                    'x-appwrite-project': process.env.APPWRITE_PROJECT_ID,
                    'X-appwrite-Key': process.env.APPWRITE_API_KEY,
                },
                body: JSON.stringify({email:'',password:''})
            })

Check this for more information https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

Drake
3 Jul, 2023, 15:43

You probably don't need to use an API key in this request. Be careful not to use an API key client side

Binyamin
3 Jul, 2023, 15:43

But i dont know how can i add email and password, i tried to add it into the body, like: JSON.stringify({email: ...., password: ....}), but it does not work.

Share the error that you get.

otik
3 Jul, 2023, 15:51

this is my response from appwrite cloud:

otik
3 Jul, 2023, 15:51

type: 'basic', url: 'https://cloud.appwrite.io/v1/account/sessions/email', redirected: false, status: 401, ok: false, statusText: 'Unauthorized',

Binyamin
3 Jul, 2023, 15:53

Are you trying to create a user or to login the user using previous username & password?

otik
3 Jul, 2023, 15:54

to login created user with email and password

Binyamin
3 Jul, 2023, 15:55

Try

  • Remove X-appwrite-Key
  • Check that x-appwrite-project is being sent in the payload
  • Check that the body data being sent in the payload is correct.
otik
3 Jul, 2023, 16:03

I think that i have everything right. BTW I can create anonymous session like this, but create session with email is worse

otik
3 Jul, 2023, 16:04

i removed api key, project is really there and body is correct

Binyamin
3 Jul, 2023, 16:04

The wrong email is in purpose right?

otik
3 Jul, 2023, 16:05

yes, I have same result with my correct email

Binyamin
3 Jul, 2023, 16:08

Try to add Content-Type: application/json

TypeScript
const response = await fetch(${process.env.APPWRITE_ENDPOINT}account/sessions/email, {
                method: 'POST',
                headers: {
                    'x-appwrite-project': process.env.APPWRITE_PROJECT_ID,
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({email:'',password:''})
            })
Binyamin
3 Jul, 2023, 16:08

And let me know.

otik
3 Jul, 2023, 16:10

It is working, thank you very very much <:appwriteheart2:1073243188954935387>

Binyamin
3 Jul, 2023, 16:10

πŸ‘

Binyamin
3 Jul, 2023, 16:11

Btw, why aren't you using the Web sdk?

otik
3 Jul, 2023, 16:14

I want to use SSR authentication with sveltekit. I need tokens for authorization. I found example: https://builtwith.appwrite.io/projects/645ce5988e8badb53cd7/ But there are only anonymous sessions.

Binyamin
3 Jul, 2023, 16:14

Ohh nice πŸ‘

Binyamin
3 Jul, 2023, 16:14

Here you can see an example of how to achieve a server side auth using a cookie jar.

https://discord.com/channels/564160730845151244/1123704308966379552/1123972307057119273

otik
3 Jul, 2023, 16:33

Thank you

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