Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove user from stuck state if auth token refresh fails #189

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/python/app/rest/auth_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"httponly": True,
"samesite": ("None" if os.getenv("PREVIEW_DEPLOY") else "Strict"),
"secure": (os.getenv("FLASK_CONFIG") == "production"),
"max_age": 60 * 60 * 24 , # 1 day
}


Expand Down
2 changes: 1 addition & 1 deletion frontend/src/APIClients/AuthAPIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
FetchResult<
{
logout: null;
},
},

Check warning on line 137 in frontend/src/APIClients/AuthAPIClient.ts

View workflow job for this annotation

GitHub Actions / run-lint

Delete `·`
Record<string, unknown>,
Record<string, unknown>
>
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/APIClients/BaseAPIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,25 @@
(typeof decodedToken === "string" ||
decodedToken.exp <= Math.round(new Date().getTime() / 1000))
) {
try {
const { data } = await axios.post(

Check warning on line 46 in frontend/src/APIClients/BaseAPIClient.ts

View workflow job for this annotation

GitHub Actions / run-lint

Insert `··`
`${process.env.REACT_APP_BACKEND_URL}/auth/refresh`,

Check warning on line 47 in frontend/src/APIClients/BaseAPIClient.ts

View workflow job for this annotation

GitHub Actions / run-lint

Insert `··`
{},

Check warning on line 48 in frontend/src/APIClients/BaseAPIClient.ts

View workflow job for this annotation

GitHub Actions / run-lint

Insert `··`
{ withCredentials: true },

Check warning on line 49 in frontend/src/APIClients/BaseAPIClient.ts

View workflow job for this annotation

GitHub Actions / run-lint

Insert `··`
);

Check warning on line 50 in frontend/src/APIClients/BaseAPIClient.ts

View workflow job for this annotation

GitHub Actions / run-lint

Insert `··`

const accessToken = data.accessToken || data.access_token;

Check warning on line 52 in frontend/src/APIClients/BaseAPIClient.ts

View workflow job for this annotation

GitHub Actions / run-lint

Insert `··`
setLocalStorageObjProperty(

Check warning on line 53 in frontend/src/APIClients/BaseAPIClient.ts

View workflow job for this annotation

GitHub Actions / run-lint

Insert `··`
AUTHENTICATED_USER_KEY,

Check warning on line 54 in frontend/src/APIClients/BaseAPIClient.ts

View workflow job for this annotation

GitHub Actions / run-lint

Insert `··`
"accessToken",

Check warning on line 55 in frontend/src/APIClients/BaseAPIClient.ts

View workflow job for this annotation

GitHub Actions / run-lint

Insert `··`
accessToken,
);

newConfig.headers.Authorization = `Bearer ${accessToken}`;
} catch (error) {
localStorage.removeItem(AUTHENTICATED_USER_KEY);
window.location.href = "/login";
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions frontend/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const authLink = setContext(async (_, { headers }) => {
(typeof decodedToken === "string" ||
decodedToken.exp <= Math.round(new Date().getTime() / 1000))
) {
try {
const { data } = await axios.post(
`${process.env.REACT_APP_BACKEND_URL}/graphql`,
{ query: REFRESH_MUTATION },
Expand All @@ -62,6 +63,10 @@ const authLink = setContext(async (_, { headers }) => {
accessToken,
);
token = accessToken;
} catch {
localStorage.removeItem(AUTHENTICATED_USER_KEY);
window.location.reload();
}
}
}
// return the headers to the context so httpLink can read them
Expand Down
Loading