Back

Refresh JWT via REST

  • 0
  • Self Hosted
  • Accounts
  • Web
cheelahim
15 Feb, 2023, 22:59

Hello. Should it be possible to create new JWT via REST, using current valid JWT?

I'm trying to execute POST http://localhost/v1/account/jwt with X-Appwrite-JWT containing the latest JWT, but I'm getting this error:

TypeScript
{message: 'The current user session could not be found.', code: 404, type: 'user_session_not_found', version: '1.2.0'}
TL;DR
The user is trying to refresh a JWT token using a browser extension service worker. They have attempted to send credentials to the background service worker and capture the `x-fallback-cookies` header, but it did not work. They are looking for recommendations on how to use Appwrite from a browser extension and maintain a valid JWT inside the service worker. A suggestion is made to use an API key instead of trying to refresh the JWT. No solution is provided in the support thread.
VincentGe
15 Feb, 2023, 23:11

Hmmmm, afaik, it has to be attached to a session. You won't be able to create new JWTs with existing JWTs. These are really meant to pass to Server SDKs to perform actions on your behalf.

cheelahim
15 Feb, 2023, 23:33

I see. Well, the argument can be made that creating new JWT is the action on my behalf, so the server should be able to do it. πŸ™‚ But I see how that migth be a bad idea from the security standpoint.

Can you advise how to maintain the valid JWT inside service worker, since the client SDK doesn't work there?

VincentGe
15 Feb, 2023, 23:33

Service worker?

VincentGe
15 Feb, 2023, 23:33

Hmmmmm

VincentGe
15 Feb, 2023, 23:34

Like this isn't somehting client facing

VincentGe
15 Feb, 2023, 23:34

right?

VincentGe
15 Feb, 2023, 23:34

Use an API key

VincentGe
15 Feb, 2023, 23:34
VincentGe
15 Feb, 2023, 23:35

If it is client facing, you should have an active session πŸ˜‰ If it's like a background tasks that persists, API keys are your friend.

VincentGe
15 Feb, 2023, 23:35

Bottom of your overview page

cheelahim
15 Feb, 2023, 23:46

Yeah, I understand both workflows. The service worker I'm talking about is a browser extension background process, so it's technically a client facing code. I believe api key cannot be used in this scenario. I wonder if there are any recommendations on how to use appwrite from a browser extension. πŸ€”

cheelahim
15 Feb, 2023, 23:49

@VincentGe Thank you for help. I will keep poking around. πŸ™‚

Drake
15 Feb, 2023, 23:56

What you're facing would be the same as frameworks using SSR like sveltekit, NextJS, nuxt, etc.

Drake
15 Feb, 2023, 23:59

This might be helpful: https://discord.com/channels/564160730845151244/564160731327758347/1075481441791639683

In short, you need the service worker to use the session cookie

VincentGe
16 Feb, 2023, 00:16

πŸ‘€ is this actually advisable?

Drake
16 Feb, 2023, 00:48

Ideally we wrap it with something that would make it easier but I don't think we've had the time to build that

VincentGe
16 Feb, 2023, 01:12

πŸ‘€ I guess we can propose it as a temporary work around

cheelahim
16 Feb, 2023, 13:19

Thanks. Actually I have a nextJS app too, where I do something like this. I went through @Meldiron very helpful examples, and I was able to make it work for NextJS. It's just my current use-case is different due to the browser extension service worker constrains. There is no, traditional request/response cycle like in SSR. In this case it's an isolated long-living process, which I want maintain user session in. Both appwrite and node-appwrite failed to run there, but that's for another time. Anyways, I'm gonna look into this later and update you on my findings πŸ™‚

Drake
16 Feb, 2023, 15:34

You could manually log in using fetch, grab the cookie, and store it

cheelahim
19 Feb, 2023, 12:03

I ended up doing it somewhat similar to NextJS SSR flow. Sent credentials to the background SW via runtime messaging, created session, captured x-fallback-cookies header and stored in the chrome storage. After that set up timer to refresh JWT periodically using chrome alarms.

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