Skip to content
Merged
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
23 changes: 23 additions & 0 deletions web-ui/src/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ export const getMyFetch = async () => {
return myFetch;
};

function windowLogin() {
return new Promise((resolve, reject) => {
const authUrl = `${BASE_API_URL}/login?close=true`;
const loginWindow = window.open(authUrl, "Login", "width=500,height=600");

const interval = setInterval(() => {
try {
if (loginWindow.closed) {
clearInterval(interval);
resolve();
}
} catch (err) {
reject(err);
}
}, 1000);
});
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is only workable for localhost, isn't it?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it works elsewhere because the URL doesn't really matter (except for our route match to close the window). In production, it will see that the JWT has expired (or doesn't exist) and will make the user pick their google account to use.

export const resolve = async payload => {
let { url } = payload;
const { params = null, data = null, ...rest } = payload;
Expand All @@ -77,6 +95,11 @@ export const resolve = async payload => {

resolved.payload = await promise;

if (resolved.payload.status == 401) {
await windowLogin();
resolved.payload = await myFetch(url, rest);
}

if (!resolved.payload.ok) {
try {
resolved.error = await resolved.payload.json();
Expand Down
4 changes: 4 additions & 0 deletions web-ui/src/components/routes/Routes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import KudosPage from '../../pages/KudosPage';
import ManageKudosPage from '../../pages/ManageKudosPage';
import SkillCategoriesPage from '../../pages/SkillCategoriesPage';
import SkillCategoryEditPage from '../../pages/SkillCategoryEditPage';
import CloseWindow from '../../pages/CloseWindow';

export default function Routes() {
const { state } = useContext(AppContext);
Expand Down Expand Up @@ -202,6 +203,9 @@ export default function Routes() {
<Header title="Manage Kudos"></Header>
<ManageKudosPage />
</Route>
<Route path="/login?close=true">
<CloseWindow />
</Route>
</Switch>
);
}
5 changes: 5 additions & 0 deletions web-ui/src/pages/CloseWindow.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
window.close();

export default function CloseWindow() {
return (<></>);
}