Skip to content

Commit

Permalink
Adding more resilience to refresh token logic (#5704)
Browse files Browse the repository at this point in the history
  • Loading branch information
malhotra5 authored Dec 20, 2024
1 parent 7a0488c commit 4af84a2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
9 changes: 7 additions & 2 deletions frontend/src/api/open-hands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,16 @@ class OpenHands {
*/
static async refreshToken(
appMode: GetConfigResponse["APP_MODE"],
userId: string,
): Promise<string> {
if (appMode === "oss") return "";

const response =
await openHands.post<GitHubAccessTokenResponse>("/api/refresh-token");
const response = await openHands.post<GitHubAccessTokenResponse>(
"/api/refresh-token",
{
userId,
},
);
return response.data.access_token;
}

Expand Down
17 changes: 16 additions & 1 deletion frontend/src/context/auth-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface AuthContextType {
gitHubToken: string | null;
setToken: (token: string | null) => void;
setGitHubToken: (token: string | null) => void;
setUserId: (userId: string) => void;
clearToken: () => void;
clearGitHubToken: () => void;
refreshToken: () => Promise<boolean>;
Expand All @@ -34,6 +35,10 @@ function AuthProvider({ children }: React.PropsWithChildren) {
() => localStorage.getItem("ghToken"),
);

const [userIdState, setUserIdState] = React.useState<string>(
() => localStorage.getItem("userId") || "",
);

const clearToken = () => {
setTokenState(null);
localStorage.removeItem("token");
Expand All @@ -43,7 +48,9 @@ function AuthProvider({ children }: React.PropsWithChildren) {

const clearGitHubToken = () => {
setGitHubTokenState(null);
setUserIdState("");
localStorage.removeItem("ghToken");
localStorage.removeItem("userId");

removeOpenHandsGitHubTokenHeader();
removeGitHubAuthTokenHeader();
Expand Down Expand Up @@ -72,6 +79,11 @@ function AuthProvider({ children }: React.PropsWithChildren) {
}
};

const setUserId = (userId: string) => {
setUserIdState(userIdState);
localStorage.setItem("userId", userId);
};

const logout = () => {
clearGitHubToken();
posthog.reset();
Expand All @@ -84,7 +96,7 @@ function AuthProvider({ children }: React.PropsWithChildren) {
return false;
}

const newToken = await OpenHands.refreshToken(config.APP_MODE);
const newToken = await OpenHands.refreshToken(config.APP_MODE, userIdState);
if (newToken) {
setGitHubToken(newToken);
return true;
Expand All @@ -97,9 +109,11 @@ function AuthProvider({ children }: React.PropsWithChildren) {
React.useEffect(() => {
const storedToken = localStorage.getItem("token");
const storedGitHubToken = localStorage.getItem("ghToken");
const userId = localStorage.getItem("userId") || "";

setToken(storedToken);
setGitHubToken(storedGitHubToken);
setUserId(userId);
setupGithubAxiosInterceptors(refreshToken, logout);
}, []);

Expand All @@ -109,6 +123,7 @@ function AuthProvider({ children }: React.PropsWithChildren) {
gitHubToken: gitHubTokenState,
setToken,
setGitHubToken,
setUserId,
clearToken,
clearGitHubToken,
refreshToken,
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/hooks/query/use-github-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useAuth } from "#/context/auth-context";
import { useConfig } from "./use-config";

export const useGitHubUser = () => {
const { gitHubToken } = useAuth();
const { gitHubToken, setUserId } = useAuth();
const { data: config } = useConfig();

const user = useQuery({
Expand All @@ -18,6 +18,7 @@ export const useGitHubUser = () => {

React.useEffect(() => {
if (user.data) {
setUserId(user.data.id.toString());
posthog.identify(user.data.login, {
company: user.data.company,
name: user.data.name,
Expand Down

0 comments on commit 4af84a2

Please sign in to comment.