How to create a JWT for a specific user from an Edge Function?
Hi everyone,
I’m trying to create an Edge Function in Supabase that can generate a JWT for a specific user (identified by user_id or email) so that it can be used for server-to-server requests.
My use case: I want to build a feature where a user can schedule a task. The scheduled task will need to run with a valid JWT token on behalf of that user.
What I want to achieve:
1. Input: user_id or email
2. Output: A valid JWT for the authenticated role (usable to query Supabase API on behalf of that user)
Is there any built-in method or recommended approach to generate such a token from an Edge Function?
Thanks in advance 🙏
2 Replies
There is no built in method. With the old JWTs and secret you could mint a JWT with a JWT library from auth.users data. If you just needed auth.uid() then you would only need the user id and the rest of claims (role, etc) would could be constants.
I've not researched how you mint JWT's with the new assymetric JWT's yet.
There is another approach using auth.admin.generateLink to get a token like the user is signing in with email. Theen use verifyOtp to actually sign them in. Maybe be an issue though as they could signout on their client and impact things.
Thank you for the explanation! My project is still using symmetric JWT, so I think I’ll try the first option. I’ll share an update here once I manage to get it working.