Skip to content

Commit b6ff645

Browse files
committed
refactor: remove "auth"
ref issue topcoder-archive#44
1 parent 8a68e70 commit b6ff645

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

src/components/LoadingIndicator/index.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import "./styles.module.scss";
1111
const LoadingIndicator = ({ error }) => {
1212
return (
1313
<div styleName="loading-indicator">
14-
{!error ? "Loading..." : _.get(error, "response.data.message") || error}
14+
{!error
15+
? "Loading..."
16+
: _.get(error, "response.data.message", error.toString())}
1517
</div>
1618
);
1719
};

src/hoc/withAuthentication.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* wrap component for authentication
55
*/
6-
import React, { useCallback, useState, useEffect } from "react";
6+
import React, { useState, useEffect } from "react";
77
import { getAuthUserTokens, login } from "@topcoder/micro-frontends-navbar-app";
88
import LoadingIndicator from "../components/LoadingIndicator";
99

@@ -13,27 +13,25 @@ export default function withAuthentication(Component) {
1313
let [authError, setAuthError] = useState(false);
1414

1515
useEffect(() => {
16-
if (props.auth) {
17-
getAuthUserTokens()
18-
.then(({ tokenV3 }) => {
19-
if (!!tokenV3) {
20-
setIsLoggedIn(!!tokenV3);
21-
} else {
22-
login();
23-
}
24-
})
25-
.catch((err) => {
26-
setAuthError(err);
27-
});
28-
}
29-
}, [props.auth]);
16+
getAuthUserTokens()
17+
.then(({ tokenV3 }) => {
18+
if (!!tokenV3) {
19+
setIsLoggedIn(!!tokenV3);
20+
} else {
21+
login();
22+
}
23+
})
24+
.catch(setAuthError);
25+
}, []);
3026

3127
return (
3228
<>
33-
{authError && <LoadingIndicator error={authError.toString()} />}
34-
{!props.auth || (props.auth && isLoggedIn === true) ? (
35-
<Component {...props} />
36-
) : null}
29+
{/* Show loading indicator until we know if user is logged-in or no.
30+
In we got error during this process, show error */}
31+
{isLoggedIn === null && <LoadingIndicator error={authError} />}
32+
33+
{/* Show component only if user is logged-in */}
34+
{isLoggedIn === true ? <Component {...props} /> : null}
3735
</>
3836
);
3937
};

src/root.component.jsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ export default function Root() {
1414
<div className={styles["topcoder-micro-frontends-teams-app"]}>
1515
<Provider store={store}>
1616
<Router>
17-
<MyTeamsList path="/taas/myteams" auth />
18-
<MyTeamsDetails path="/taas/myteams/:teamId" auth />
19-
<PositionDetails
20-
path="/taas/myteams/:teamId/positions/:positionId"
21-
auth
22-
/>
17+
<MyTeamsList path="/taas/myteams" />
18+
<MyTeamsDetails path="/taas/myteams/:teamId" />
19+
<PositionDetails path="/taas/myteams/:teamId/positions/:positionId" />
2320
</Router>
2421

2522
{/* Global config for Toastr popups */}

0 commit comments

Comments
 (0)