Back

issue with creating JWT with REST API

  • 1
  • Accounts
  • Web
  • Cloud
otik
29 Aug, 2023, 15:45

Only one more question. When i have session and i send it to server, is there any way to use that session to authorizate the user? Not by creating JWT.

TL;DR
User is asking for help with creating JWT using REST API. They mention using the server-side SDK and ask if they can set cookies with the Appwrite SDK. Another user explains that they can authenticate as a user using either JWT or cookies. They suggest storing the cookie and reusing it for subsequent requests. The user asks how to authenticate the user on the server-side without using JWT, and another user clarifies that they can use the cookie for authentication. The conversation continues with additional questions and explanations. Solution: Store the cookie from the authentication process and reuse it for subsequent requests to authenticate the user on the server-side.
Binyamin
29 Aug, 2023, 15:49

Yes, for that you'll need to do all the login process with a cookie jar. Check this example for a demo.

Binyamin
29 Aug, 2023, 15:53

Btw

Binyamin
29 Aug, 2023, 15:53

What is your use case?

otik
29 Aug, 2023, 15:53

I am sorry for missunderstanding. But this still works by creating JWT on server-side

Binyamin
29 Aug, 2023, 15:54

You're correct in that example you can see 2 things

  1. The cookie jar
  2. The JWT which you can ignore
otik
29 Aug, 2023, 15:55

i log in by email on server-side then session saves in cookies on client. Then client sends cookies in headers to server (i am using SSR-sveltekit) and then server should authorizate the user

Binyamin
29 Aug, 2023, 16:01

Okay, that should work

Binyamin
29 Aug, 2023, 16:01

Something like this?

otik
29 Aug, 2023, 16:02

yes exactly, then server sends fallback cookie to user and user back to server during fetch

Binyamin
29 Aug, 2023, 16:04

Good So store that cookie some whare and you can reuse it to auth the user in subsequent requests

Binyamin
29 Aug, 2023, 16:04

And you won't need the JWT

otik
29 Aug, 2023, 16:06

yes but how will i be able to act like user on server-side without JWT (only with session) ? You were creating JWT in your example

Binyamin
29 Aug, 2023, 16:09

Yes, but that is only because in that example he wanted to have JWT. It's like this πŸ‘‡ When you want to access Appwrite resources as a user you'll need to be authenticate, you can do it in one of two ways

  • JWT, which you need to regenerate every 15 minutes
  • Cookie, which you need to regenerate after the session timeout has passed which is very long. As you're already doing by adding the X-Fallback-Cookies header like you can see here, you're actually being authenticate as the user. meaning, you can do any REST action on behalf of that user.
Binyamin
29 Aug, 2023, 16:09

Is this make sense?

otik
29 Aug, 2023, 16:11

Yes i am sorry. But can i set cookies on your appwrite SDK?

otik
29 Aug, 2023, 16:11

Thank you very much. This is what i am doing for REST API

Binyamin
29 Aug, 2023, 16:11

What you mean? using the Web SDK?

otik
29 Aug, 2023, 16:11

no no server node.js SDK

Binyamin
29 Aug, 2023, 16:12

Nope, The server SDK supports only the Server side endpoints, so you'll have to use the either the client-side Web sdk or the pure REST like you're already doing.

Binyamin
29 Aug, 2023, 16:13

Also, Check the <:svelte:877959666238386257> Almost-ssr by Meldiron which uses the SDK to login from the server side.

otik
29 Aug, 2023, 16:13

Yes, thank you very much. Maybe i will be looking for another sollutions

otik
29 Aug, 2023, 16:14

Yes thank you, i know i used it

otik
29 Aug, 2023, 16:14

Thank you very much for all your help <:appwriteheart2:1073243188954935387>

Binyamin
29 Aug, 2023, 16:16

<a:agooglethumbsup:635256484682530825>

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