Integrating a custom auth flow with Supabase client while taking advantage of RLS #1849
-
In this discussion,
As per my understanding USER_JWT is obtained through
But since we are not using supabase.auth how do I get/create the USER_JWT the right way? What does it look like? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
hey @rkhatkhede ! yes you can mint your own JWTs from anywhere, you need to grab the jwt_secret from the dashboard: in Settings > API and then you can mint your own tokens anywhere using something like jsonwebtoken lib var jwt = require('jsonwebtoken');
var token = jwt.sign({
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022,
"someclaim" : "VaaVaaVoom"
}, jwtSecret); (you can test this on https://jwt.io/ also) and set it as a header in supabase-js in the way that you mentioned: const headers = { Authorization: 'Bearer USER_JWT' }
const supabase = createClient( "https://REF.supabase.co", "KEY", { headers }); additionally you can create your own RLS helper functions to pull out values from the JWT like: -- Gets some custom claim from the request JWT
create or replace function auth.someclaim() returns uuid as $$
select nullif(current_setting('request.jwt.claim.someclaim', true), '')::uuid;
$$ language sql stable; then write a policy like: create policy "my new policy"
on mytable
for select
using (
auth.someclaim() = mytable.somecolumn
); |
Beta Was this translation helpful? Give feedback.
hey @rkhatkhede !
yes you can mint your own JWTs from anywhere, you need to grab the jwt_secret from the dashboard:
in Settings > API
and then you can mint your own tokens anywhere using something like jsonwebtoken lib
(you can test this on https://jwt.io/ also)
and set it as a header in supabase-js in the way that you mentioned:
additionally you can create your own RLS helper functions to pu…