Authentication in serverless function #1067
-
Hello 👋 First, I need to pass jwt token from browser to my API. ← How can I get one? I've been reading the documentations but couldn't find something related to this. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 8 replies
-
Hi there,
You can use the |
Beta Was this translation helpful? Give feedback.
-
I'm going to rephrase/correct my question. On the browser, I have access to these values. Let's say I have an API on my server async function doSomething(req, res) {
const { bearerToken } = JSON.parse(req.body);
const userId = await validateTokenAndGetUserId(bearerToken);
// do something with the userId
} Which API of supabase should I hit to implement The reason why I'm trying to do it is
|
Beta Was this translation helpful? Give feedback.
-
ah yes ok if you have authenticated a user in the browser like: let { user, session, error } = await supabase
.auth.signIn({ email: 'ant@boo.com', password: 'smoothmoves11'}) then access_token : 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJhdXRoZW50aWNhdGVkIiwiZXhwIjoxNjE3Nzc1MTQ2LCJzdWIiOiIwMDE3YmZlZC0wZThmLTRkOTgtYTI3MC1lYmE5ZjUwOWI2MDciLCJlbWFpbCI6ImFudEBib28uY29tIiwiYXBwX21ldGFkYXRhIjp7InByb3ZpZGVyIjoiZW1haWwifSwidXNlcl9tZXRhZGF0YSI6e30sInJvbGUiOiJhdXRoZW50aWNhdGVkIn0.X-uCLBUGtNcO586ZIgo-RsOC6uoBaATYZeVvCVLbwz4' if you put this into https://jwt.io/ you will see it decodes to: {
"aud": "authenticated",
"exp": 1617775146,
"sub": "0017bfed-0e8f-4d98-a270-eba9f509b607",
"email": "ant@boo.com",
"app_metadata": {
"provider": "email"
},
"user_metadata": {},
"role": "authenticated"
}
so you can send this to validate the token on the backend you need to use a standard jwt library like jsonwebtoken (or find the equivalent for your language) and verify like so: var decoded = jwt.verify(access_token, JWT_SECRET);
console.log(decoded.sub) // this will be the user's ID the hope this is helpful! we also have a 5 part guide if you want to dig deeper into JWTs in Supabase |
Beta Was this translation helpful? Give feedback.
-
Does that mean that the logic here is now out of date and no longer works? |
Beta Was this translation helpful? Give feedback.
-
We need an example for a serverless function (NOT in next or express) where we can create a session. i'm getting the user ID just fine with the suggest JWT flow, but supabase won't return any rows because I have RLS set and it needs to know the session of the caller, not just the id.... |
Beta Was this translation helpful? Give feedback.
ah yes ok
if you have authenticated a user in the browser like:
then
session
will containsession.access_token
which will look something like:access_token : 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJhdXRoZW50aWNhdGVkIiwiZXhwIjoxNjE3Nzc1MTQ2LCJzdWIiOiIwMDE3YmZlZC0wZThmLTRkOTgtYTI3MC1lYmE5ZjUwOWI2MDciLCJlbWFpbCI6ImFudEBib28uY29tIiwiYXBwX21ldGFkYXRhIjp7InByb3ZpZGVyIjoiZW1haWwifSwidXNlcl9tZXRhZGF0YSI6e30sInJvbGUiOiJhdXRoZW50aWNhdGVkIn0.X-uCLBUGtNcO586ZIgo-RsOC6uoBaATYZeVvCVLbwz4'
if you put this into https://jwt.io/ you will see it decodes to: