S
Supabase•3y ago
j4

How to refresh a session in v2

Since this got merged, I can't figure out how to refresh a session. The new setSession() will only refresh your tokens if the access_token has expired. Before this merge, setSession() would give you new tokens even if the access_token was still valid. Or am I just missing something? (the most likely scenario) https://github.com/supabase/gotrue-js/pull/467
GitHub
feat: update setSession by kangmingtay · Pull Request #467 · supaba...
What kind of change does this PR introduce? Fixes setSession to make it safe to use in a server-side or SSR context setSession should take in an access token and refresh token and only perform a r...
7 Replies
j4
j4OP•3y ago
Believe I figured this out. For setSession() to properly refresh tokens, pass the autoRefreshToken and persistSession options. It may be enough to only set autoRefreshToken - didn't experiment. It'll refresh once the tokens are expired.
{
auth: {
autoRefreshToken: false,
persistSession: false
}
}
{
auth: {
autoRefreshToken: false,
persistSession: false
}
}
rphlmr âš¡
rphlmr ⚡•3y ago
Thanks, I have the same "issue" :p I have the same auth config (using supabase in Remix, server-side) but it doesn't solve my problem. What works is to set an empty string for the access_token 😂
// getSupabaseAdmin() is just a per request supabase client with service key
const { data, error } = await getSupabaseAdmin().auth.setSession({
access_token: "",
refresh_token: refreshToken,
});
// getSupabaseAdmin() is just a per request supabase client with service key
const { data, error } = await getSupabaseAdmin().auth.setSession({
access_token: "",
refresh_token: refreshToken,
});
My use case : I want to force refresh x minutes before it expires because I don't want it expires in fiew ms in my api using the access token 😅
j4
j4OP•3y ago
Oh wow! I'll test that as well, because I also want the token to refresh before it expires. Either way, it seems like there's some work they need to do on this. Yes, that worked for me too! If this is the way it's designed to work, they should have a better explanation in the docs.
Shelby
Shelby•3y ago
Does anyone know the reason why my cookies aren't set when I sign in
rphlmr âš¡
rphlmr ⚡•3y ago
Depends on what framework you use. If it's Remix, I can help, otherwise, I think I saw an issue on GitHub for Nextjs I ask, just in case ^^" https://github.com/supabase/gotrue-js/pull/467#issuecomment-1279024940
Shelby
Shelby•3y ago
Nextjs Do you have the link for the issue Please
rphlmr âš¡
rphlmr ⚡•3y ago
sorry I miss a notification. Hum I can't find something specific to your issue in particular 😬. Dependening what you are doing and what you use but v2 has some breaking changes https://supabase.com/docs/reference/javascript/release-notes
Release Notes | Supabase
Supabase.js v2 release notes.

Did you find this page helpful?