sensitive-blue
sensitive-blue10mo ago

Auto refresh supabase token

I'm using a supabase auth provider (with remix). Shouldn't it automatically refresh a user session token when it expires? My user is still logged in, but if I try to get the user from the token (from a cookie) on the backend (in a remix loader function), supabase says the token has expired.
2 Replies
kapa.ai
kapa.ai10mo ago
Hi @oshiri3676. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the extracts provided, there's specific guidance on implementing a refresh token mechanism with Supabase and Refine, but it focuses on using the axios-auth-refresh package for refreshing tokens in a general context, not specifically for Supabase tokens. Here's the relevant code snippet for implementing a refresh token mechanism:
import { Refine, AuthProvider } from "@refinedev/core";
import axios from "axios";
import createAuthRefreshInterceptor from "axios-auth-refresh";

const axiosInstance = axios.create();

const refreshAuthLogic = (failedRequest) =>
axiosInstance.post(`${API_URL}/auth/token/refresh`).then((tokenRefreshResponse) => {
localStorage.setItem("token", tokenRefreshResponse.data.token);

failedRequest.response.config.headers["Authorization"] = "Bearer " + tokenRefreshResponse.data.token;

return Promise.resolve();
});

createAuthRefreshInterceptor(axiosInstance, refreshAuthLogic);

const App = () => {
const authProvider: AuthProvider = {
/* ... */
};

return (
<Refine
authProvider={authProvider}
dataProvider={dataProvider(API_URL, axiosInstance)}
>
/* ... */
</Refine>
);
};
import { Refine, AuthProvider } from "@refinedev/core";
import axios from "axios";
import createAuthRefreshInterceptor from "axios-auth-refresh";

const axiosInstance = axios.create();

const refreshAuthLogic = (failedRequest) =>
axiosInstance.post(`${API_URL}/auth/token/refresh`).then((tokenRefreshResponse) => {
localStorage.setItem("token", tokenRefreshResponse.data.token);

failedRequest.response.config.headers["Authorization"] = "Bearer " + tokenRefreshResponse.data.token;

return Promise.resolve();
});

createAuthRefreshInterceptor(axiosInstance, refreshAuthLogic);

const App = () => {
const authProvider: AuthProvider = {
/* ... */
};

return (
<Refine
authProvider={authProvider}
dataProvider={dataProvider(API_URL, axiosInstance)}
>
/* ... */
</Refine>
);
};
(Source: Implementing Refresh Token Mechanism (https://refine.dev/docs/tutorial/understanding-authprovider/create-authprovider#implementing-refresh-token-mechanism)) However, this example does not directly address how Supabase handles token refresh within the context of Refine or Remix. Supabase's authentication mechanism typically manages session tokens and their renewal automatically on the client side. If you're encountering issues with token expiration on the backend, it might be related to how the session token is being managed or transferred between the client and server, rather than an issue with automatic token refreshment by Supabase itself. For a more specific solution to your problem, I recommend checking the official Supabase documentation or community resources on handling token refreshment with Supabase in a server-side context like Remix. Unfortunately, the extracts provided do not offer a detailed solution for integrating Supabase's automatic token refresh in a Remix application. For more detailed guidance on working with authentication in Refine, including custom Auth Providers, you might find the following link useful: Refine Auth Provider (https://refine.dev/docs/authentication/auth-provider/). However, please note that this link was mentioned in the extracts and is provided for reference; it may not directly address your question about token refreshment in Remix with Supabase. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai10mo ago
<@783439443410813008> kapa.ai is still learning and improving, please let us know how it did by reacting below