supabase-community/supabase-custom-claims

Provide JS code example

andriusmv opened this issue ยท 2 comments

Provide additional examples for JS, ReactJS, NextJS

Please provide specific examples of what the actual code looks like in a React app when implementing custom claims function.

For example, how does a component that prevents rendering of parts of your application looks like in your code? (with a ternary operator, for eg.)
membershipType=='TRIAL'? <TrialContent />:<PleaseUpgradeToTrialMessage />

Thank you so much burggraf ๐Ÿ™๐Ÿป๐Ÿ™๐Ÿป๐Ÿ™๐Ÿป

I'm working on a complete sample application.
You can check the claims on the client side with no server interaction required by looking at the session object returned from supabase.auth.onAuthStateChange.

For example:

supabase.auth.onAuthStateChange((_event, session) => {
	setSession(session)
})

Once you have the session variable, you can get the current claims stored in the token like this: session?.user?.app_metadata

To get membershipType, it would be session?.user?.app_metadata?.membershipType

A complete sample is is now provided that should help you.