https://supabase.com/ logo
Hello, I am trying to implement custom claims with...
# help
s
Hello, I am trying to implement custom claims with my web app. How would I go about setting a custom role of a user on signup. for example if a user signs up from the teacher portal their custom claim role becomes "teacher"?
n
Hello @Sajeev! This thread has been automatically created from your message in #843999948717555735 a few seconds ago. We have already mentioned the @User so that they can see your message and help you as soon as possible! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ``...`` menu) and select "Leave Thread" to unsubscribe from future updates. Want to change the title? Use the ``/title`` command! We have solved your problem? Click the button below to archive it.
s
Please take a look at this repo my colleague created showing how to do custom claims with Supabase https://github.com/supabase-community/supabase-custom-claims
s
yep i looked at that repo, but I am confused on how to set the claim when they first signup. If i try to use: .rpc('set_claim', {uid, claim, value});
isn't that a security risk to set the claim from the client
if I used an edge function and trigger, how would the server know whether to set the role as teacher or student?
s
I don't think you can do that anyway because the
set_claim
function is checking if you are a admin before allowing you to set claims
You could call that function inside of a trigger, so in an after trigger that runs when a new user has been inserted in the
auth.users
table
s
yep, but how would the function know to set the role of "teacher" or "student" since the new user will be inserted in the auth.users table regardless of whether they signup from the teacher or student portal
g
Is there something unique about the teacher portal? You have access to all headers from PostgREST in your rpc function.
So you know the referer for instance...
s
I am new to PostgREST so i am not sure which header I would edit and what I would add to it
would I just add a string "teacher" to one of the headers?
g
No is your teacher portal a different URL? Here is an example of the headers on a select call (would be same for RPC) you can get any of these. Deleted the pix as had some info I did not want public.
It would be best if the header is from the browser so it can't (or is very hard to be?) faked.
s
yea the teacher portal is a different url
g
So referer might work for you.
s
oh so if I set the referer to the url and then check the refer on the server I would be able to add the role
ok thank you!
g
I don't think you set the referer, the browser does, so it can't be faked... but could be wrong. https://postgrest.org/en/stable/api.html#http-context
s
ok
how would I access the refer header in the server function?
g
Follow the link I just sent.
s
oh ok I see it. thank you!
so each time I signup all the headers are null
g
I thought you were doing an rpc call?
auth does not go thru PostgREST
s
oh ok
so I would have to use rpc.signup
or smth like that
where is the documentation for rpc
and If I use auth what would the solution be
g
So on auth signUp you can only pass in the extra data object which goes into user metadata column. The problem is that can be "faked" by the user.
rpc is just a client call. https://supabase.com/docs/reference/javascript/rpc which calls a postgres function, but you can check the headers to see where it came from
s
ok so on the client side when the user clicks the signup button what is supposed to happen
if I use rpc
g
You do a normal signUp and then when it is confirmed you would call the rpc call to set their status as user or teacher.
s
oh ok and I would create the function from the supabase dashboard
as a database function
g
Yes and in that function you can see the url in the header to decide which portal it came from and set your "claim" info.
s
oh ok thank you I will try that now
would this work?
i still need to insert into the table but just to get the header
ok the function works but only problem is the referer is ths:
even though it should be this
g
Is the only difference between your portals a page on the same site? If so what stops a student from using the teacher page?
s
ok figured it out had to do with referrer policy, which i changed in the app to send the whole url instead of just the partial url and it works now
n
Thread was archived by @Sajeev. Anyone can send a message to unarchive it.
2 Views