UPDATE March 2024: Note that WeWeb added the refresh session workflow step
Thanks for the info in this thread; I used it to solve my own use-case. Posting what I did here in case it helps someone. Supabase now has a beta feature Supabase Auth Hooks Beta custom access tokens to add/remove claims based on auth hooks, e.g. logging in.
Also if you update a custom claim on the server side you may want to manually refresh, in multi-tenant cases where you want to “log out of an organization” and into another by adding a custom claim to the JWT.
So to do it with javascript you can do this to manually get a new access token with the updated claims in it (assuming you already set the custom claims with the auth hook or somehow server side or with a WeWeb update user workflow action-- however on the update user action remember they can do that themselves so you can’t 100% trust that on the back end).
const supabase = wwLib.wwPlugins.supabase.instance;
const { data, error } = await supabase.auth.refreshSession();
// Optionally, handle any error
if (error) {
console.error("Error refreshing session:", error);
// Handle the error appropriately
}
// To return the data for further processing, add return data;
However, it appears WeWeb is not notified about this so then you need a “fetch user” supabase workflow step right after to tell WeWeb’s plugin to get the new access token. So then you’re all set- you have the new JWT in WeWeb with the updated claims for real. The claim is just in the JWT, not the actual user app metadata, so WeWeb does not seem to extract it from the JWT and make the claims available in the front end but if you copy the actual access token and decode the JWT you’ll see the claims in there-- so it’s best used for validation on the back end unless WeWeb updates it to make it where the actual decoded JWT overrides the fetch user data.
Note that according to Supabase this invalidates the old access token because refreshSession()
method returns a new access token and refresh token pair, which replaces the old pair. The old access token is no longer valid and should not be used for any further requests (that’s another reason why we need to fetch user again to get the new token too). Since refresh tokens can only be used once, after calling refreshSession()
, the old refresh token is also invalidated and cannot be used again.
Side note if you use the of the update user Supabase workflow action which updates the user metatadta in the auth.users table, the existing access token in the front end does not get refreshed (at the time of writing, WeWeb puts the new update in the browser and is referenceable as a variable, but it does not actually get a new token with the claim in the JWT-- I have a ticket in with WeWeb about that).
Also at the time of this writing, wwLib.wwPlugins.supabase.instance.refreshSession() does not work to do the supabase.auth.refreshSession but if you do what I have above with const supabase then awat supabase auth.refreshSession() it works