How are you guys refreshing the user's access_token for infinite sessions? (Supabase auth)

  • benjamin.h-1353045504153620584

    Ben H

    2 months ago

    I'm not using the Supabase SDK on the frontend, so it seems like I need to call supabase.auth.refreshSession() from an edge function? Is this how you guys are doing it? I.e. every time the user goes to the dashboard, call this endpoint to refresh their auth? Or could store the expiry date in local storage to avoid calling the endpoint as often.
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
  • Tod-1353045505982201989

    Tod

    2 months ago

    Great energy @Ben H! Your continuous contribution to the toddle Community just made you advance to Community Level 6!
  • benjamin.h-1354281835798073454

    Ben H

    2 months ago

    Anyone have thoughts on this?
  • paspom-1354400955613057044

    Pascal

    2 months ago

    thats a good question
    I'm looking for a good solution too
    you could look up on page load, wheter the token is like 30 minutes or less away from expiration (assuming a user will be no longer than 30 minutes on one page, so that it is always "right on time")
    and if true, given that on page load, fire the refresh
    👍1
  • paspom-1354401306852462622

    Pascal

    2 months ago

    only problem would be the access token, that is exchanged by that function, which could cause jwt problems when replacing a cookie suddenly without page reload
    👍1
  • benjamin.h-1356326989216092301

    Ben H

    2 months ago

    I'm storing the refresh_token as an http cookie, but I'm having trouble passing it to my edge function. Is there anything special I need to do in my API request to pass the cookie? In the network tab under "Request Headers" I can see the refresh_token is there along with the access_token, but the cookie shows up as null in my edge function log:

    console.log('Raw Cookie Header Received:', req.headers.get('cookie'));
    1356326988595597526-image.png
    1356326989082001500-image.png
  • lucasg-1356331702590505082

    Lucas G

    2 months ago

    The auth header appears to be passing in the access_token, not the refresh one
  • Are you trying to pass both? If so they might need their own headers or to pass it in the body if that’s where the edge function is expecting it
  • lucasg-1356332285028204697

    Lucas G

    2 months ago

    You can’t pass multiple cookies in an auth header
  • benjamin.h-1356347618598457456

    Ben H

    1 month ago

    Thx Lucas. I was trying to pass both but I realized I don't actually need the access token. Putting the refresh token in the header seems to be working!
  • henk_08753-1373362197379547278

    Henk_BLUE

    13 days ago

    Hi @Ben H, how were you able to store both the refresh_token and access_token as httponly cookie in the first place? I gather that using the NC proxy is necessary for using http-cookies, and that a Set-Cookie header from server side will not work without it. But it seems i am only able to set session cookies once, and only as access_token?

    Not using Supabase though, so might be different there 🙂
    1373362197358710874-Schermafbeelding_2025-05-17_om_20.10.14.png
  • benjamin.h-1373403071798837268

    Ben H

    12 days ago

    you aren't able to set the refresh token using the NC set session cookies action (it can only set the access token). in my case, when a user logs in, supabase gives you the access token and refresh token in the response. i save the access token using the built in NC action. both are set as http only cookies
  • benjamin.h-1373403921749377194

    Ben H

    12 days ago

    @Henk_BLUE actually now that i'm looking at it it looks like i initially send the refresh token in the request body to the edge function that sets the initial refresh token. however i have a different edge function that i call to refresh the session using the stored refresh token, and that takes the refresh token in the auth header. been awhile since i looked at these lol
  • henk_08753-1373406464546181221

    Henk_BLUE

    12 days ago

    aaaaah that makes more sense. And you set the refresh token trough the edge function via a Set-Cookie header, I presume? That is where my Xano setup interferes, for that matter. Xano already has a default Set-Cookie header, if i add a refresh token as a second Set-Cookie header, NC doesn't read and save it. And if I merge the refresh token with the default Xano cookie, NC only takes the first cookie, which happens to be the Xano one..

    Thanks for explaining!
  • benjamin.h-1373408524394299493

    Ben H

    12 days ago

    yep the edge function uses a set-cookie header:

    // Set the refresh token cookie and return success
    return new Response(JSON.stringify({
    success: true,
    message: 'Refresh token cookie set'
    }), {
    headers: {
    ...corsHeaders,
    'Content-Type': 'application/json',
    'Set-Cookie': cookieStr
    },
    });

    unfortunately i can't comment on the xano issue as i'm not very familiar with it
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
    🙌1
  • saillmone-1377276521609302148

    saillmone

    2 days ago

    I'm also trying to do what you did: an edge function to add cookies directly.
    The function always returns a 200 code, but no cookies are added...

    I tried with credentials: "include", SameSite=None, but nothing works.

    Didn't you have trouble getting there? I'd appreciate any help!
    Thanks