extended-salmon
extended-salmon6mo ago

refresh user token in supabase

I'm using supabase as my auth provider. My user is still logged in, but his token has expired. Shouldn't the auth provider automatically refresh the user's token as they are navigating the site?
2 Replies
kapa.ai
kapa.ai6mo 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: ---------------------- Yes, the auth provider should automatically refresh the user's token. However, this requires a specific setup. In the context of Refine and Supabase, you can implement a refresh token mechanism using the axios-auth-refresh package. Here is an example of how you can set it up:
import { Refine, AuthProvider } from "@refinedev/core";
import axios from "axios";
import createAuthRefreshInterceptor from "axios-auth-refresh";

const axiosInstance = axios.create();

// Function that will be called to refresh authorization
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();
});

// Instantiate the interceptor
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();

// Function that will be called to refresh authorization
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();
});

// Instantiate the interceptor
createAuthRefreshInterceptor(axiosInstance, refreshAuthLogic);

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

return (
<Refine
authProvider={authProvider}
dataProvider={dataProvider(API_URL, axiosInstance)}
>
/* ... */
</Refine>
);
};
In this example, axios-auth-refresh is used to automatically refresh the token when it has expired. The refreshAuthLogic function is called whenever a request fails due to an expired token. It sends a request to the server to refresh the token, then updates the failed request with the new token and retries it. Please note that this is a general example and you might need to adjust it according to your specific setup and requirements. For more information, you can refer to the Refine documentation on implementing a refresh token mechanism (https://refine.dev/docs/tutorial/understanding-authprovider/create-authprovider#implementing-refresh-token-mechanism). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai6mo ago
<@783439443410813008> kapa.ai is still learning and improving, please let us know how it did by reacting below