Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit be741b9

Browse files
#630 - Replace auth0 based login with the topcoders own login
1 parent a894f3d commit be741b9

File tree

13 files changed

+1660
-1633
lines changed

13 files changed

+1660
-1633
lines changed

client/package-lock.json

Lines changed: 1548 additions & 1435 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6-
"@auth0/auth0-spa-js": "^1.9.0",
76
"@testing-library/jest-dom": "^4.2.4",
87
"@testing-library/react": "^9.5.0",
98
"@testing-library/user-event": "^7.2.1",
9+
"@topcoder-platform/tc-auth-lib": "topcoder-platform/tc-auth-lib#1.0.1",
1010
"axios": "^0.19.2",
1111
"core-js": "^3.6.5",
1212
"form-data": "^3.0.0",
1313
"history": "^4.10.1",
1414
"husky": "^4.2.5",
1515
"js-cookies": "^1.0.4",
1616
"lint-staged": "^10.2.6",
17-
"lodash": "^4.17.15",
17+
"lodash": "^4.17.20",
1818
"node-sass": "^4.14.1",
1919
"prettier": "^2.0.5",
2020
"prop-types": "^15.7.2",
2121
"react": "^16.13.1",
2222
"react-autosuggest": "^10.0.2",
2323
"react-dom": "^16.13.1",
2424
"react-router-dom": "^5.1.2",
25-
"react-scripts": "3.4.1",
25+
"react-scripts": "^3.4.3",
2626
"regenerator-runtime": "^0.13.5"
2727
},
2828
"lint-staged": {

client/src/Router.jsx

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,45 @@
44

55
import React from "react";
66
import { Router, Route, Switch } from "react-router-dom";
7+
import {
8+
configureConnector,
9+
getFreshToken,
10+
} from "@topcoder-platform/tc-auth-lib";
711

812
import Error404 from "./pages/Error404";
913
import SearchPage from "./pages/Search";
1014

11-
import { useAuth0 } from "./react-auth0-spa";
1215
import history from "./lib/history";
1316
import loader from "./assets/images/loading.svg";
17+
import config from "./config";
18+
19+
configureConnector({
20+
connectorUrl: config.AUTH.ACCOUNTS_APP_CONNECTOR,
21+
frameId: "tc-accounts-iframe",
22+
});
1423

1524
export default function AppRouter() {
16-
const { loading } = useAuth0();
25+
const [loading, setLoading] = React.useState(true);
26+
27+
React.useEffect(() => {
28+
if (!loading) {
29+
return;
30+
}
31+
32+
(async () => {
33+
try {
34+
await getFreshToken();
35+
36+
setLoading(false);
37+
} catch (error) {
38+
console.log("Error in router");
39+
console.log(error);
40+
let url = `retUrl=${encodeURIComponent(config.AUTH.APP_URL)}`;
41+
url = `${config.AUTH.TC_AUTH_URL}?${url}`;
42+
window.location.href = url;
43+
}
44+
})();
45+
}, [loading]);
1746

1847
if (loading) {
1948
return (

client/src/components/AddToGroupModal/index.jsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import Group from "./Group";
77
import Modal from "../Modal";
88
import { ReactComponent as ZoomIcon } from "../../assets/images/zoom-icon.svg";
99
import api from "../../services/api";
10-
import { useAuth0 } from "../../react-auth0-spa";
1110
import * as groupLib from "../../lib/groups";
1211

1312
import style from "./style.module.scss";
@@ -17,7 +16,6 @@ import { getNickname } from "../../lib/common";
1716
export default function AddToGroupModal({ onCancel, updateUser, user }) {
1817
const apiClient = api();
1918
const [loadingGroups, setIsLoadingGroups] = React.useState(true);
20-
const { isLoading, isAuthenticated, user: auth0User } = useAuth0();
2119
const [myGroups, setMyGroups] = React.useState([]);
2220
const [otherGroups, setOtherGroups] = React.useState([]);
2321
const [filter, setFilter] = React.useState("");
@@ -36,14 +34,11 @@ export default function AddToGroupModal({ onCancel, updateUser, user }) {
3634
});
3735

3836
React.useEffect(() => {
39-
if (isLoading || !isAuthenticated) {
40-
return;
41-
}
42-
4337
(async () => {
38+
const nickname = await getNickname();
4439
const groups = await groupLib.getGroups(
4540
apiClient,
46-
getNickname(auth0User),
41+
nickname,
4742
cancelTokenSource.token
4843
);
4944

@@ -70,7 +65,7 @@ export default function AddToGroupModal({ onCancel, updateUser, user }) {
7065
}
7166
})();
7267
// eslint-disable-next-line react-hooks/exhaustive-deps
73-
}, [isLoading, isAuthenticated, auth0User]);
68+
}, []);
7469

7570
const switchSelected = async (toggledGroup) => {
7671
let updatedGroup;

client/src/components/Header/index.jsx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ReactComponent as GroupsTabIcon } from "../../assets/images/groups-tab-
88
import { ReactComponent as UploadsTabIcon } from "../../assets/images/uploads-tab-icon.svg";
99
import { ReactComponent as ZoomIcon } from "../../assets/images/zoom-icon.svg";
1010

11-
import { useAuth0 } from "../../react-auth0-spa";
11+
import config from "../../config";
1212
import { clearOrg } from "../../services/user-org";
1313

1414
import style from "./style.module.scss";
@@ -30,8 +30,17 @@ export default function Header({
3030
const searchContext = useSearch();
3131
const [searchText, setSearchText] = React.useState("");
3232
const [showAccountDropdown, setShowAccountDropdown] = React.useState(false);
33+
const [nickname, setNickname] = React.useState("");
3334

3435
const profileDropdownEl = React.useRef(null);
36+
React.useEffect(() => {
37+
(async () => {
38+
const n = await getNickname();
39+
40+
setNickname(n);
41+
})();
42+
});
43+
3544
React.useEffect(() => {
3645
if (showAccountDropdown) {
3746
profileDropdownEl.current.focus();
@@ -49,21 +58,14 @@ export default function Header({
4958
onSearch && onSearch(value.trim());
5059
};
5160

52-
const { user, isAuthenticated, loginWithRedirect, logout } = useAuth0();
53-
5461
const logoutWithRedirect = () => {
5562
clearOrg();
56-
logout({
57-
returnTo: window.location.origin,
58-
});
63+
const url = `${
64+
config.AUTH.TC_AUTH_URL
65+
}?logout=true&retUrl=${encodeURIComponent(config.AUTH.APP_URL)}`;
66+
window.location.href = url;
5967
};
6068

61-
if (!isAuthenticated) {
62-
loginWithRedirect({});
63-
64-
return null;
65-
}
66-
6769
const reset = () => {
6870
setSearchText("");
6971
onSearch && onSearch("");
@@ -103,7 +105,7 @@ export default function Header({
103105
className={style.accountMenu}
104106
onMouseDown={() => setShowAccountDropdown(!showAccountDropdown)}
105107
>
106-
{getNickname(user)}
108+
{nickname}
107109
{organization ? <>&nbsp;({organization.name})</> : ""}
108110
{showAccountDropdown ? (
109111
<div className={`${iconStyles.chevronUpG} ${style.arrow}`}></div>

client/src/config.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,17 @@ export default {
3232
isAvailable: process.env.REACT_APP_ATTRIBUTE_ID_ISAVAILABLE,
3333
},
3434

35-
AUTH0: {
36-
domain: process.env.REACT_APP_AUTH0_DOMAIN,
37-
clientId: process.env.REACT_APP_AUTH0_CLIENTID,
38-
audience: process.env.REACT_APP_AUTH0_AUDIENCE,
35+
// TODO - Search and remove references before deleting below code
36+
// AUTH0: {
37+
// domain: process.env.REACT_APP_AUTH0_DOMAIN,
38+
// clientId: process.env.REACT_APP_AUTH0_CLIENTID,
39+
// audience: process.env.REACT_APP_AUTH0_AUDIENCE,
40+
// handleClaims: process.env.REACT_APP_AUTH0_CLAIMS_HANDLE,
41+
// },
42+
AUTH: {
43+
ACCOUNTS_APP_CONNECTOR: process.env.REACT_APP_ACCOUNTS_APP_CONNECTOR,
44+
TC_AUTH_URL: process.env.REACT_APP_TC_AUTH_URL,
45+
APP_URL: process.env.REACT_APP_APP_URL,
3946
handleClaims: process.env.REACT_APP_AUTH0_CLAIMS_HANDLE,
4047
},
4148
};

client/src/index.js

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,13 @@ import * as serviceWorker from "./serviceWorker";
1515
import { SearchContextProvider } from "./lib/search";
1616
import { ModalContextProvider } from "./lib/modal";
1717

18-
import { Auth0Provider } from "./react-auth0-spa";
19-
import history from "./lib/history";
20-
import config from "./config";
21-
22-
const onRedirectCallback = (appState) => {
23-
history.push(
24-
appState && appState.targetUrl
25-
? appState.targetUrl
26-
: window.location.pathname
27-
);
28-
};
29-
3018
ReactDOM.render(
3119
<React.StrictMode>
32-
<Auth0Provider
33-
domain={config.AUTH0.domain}
34-
client_id={config.AUTH0.clientId}
35-
audience={config.AUTH0.audience}
36-
redirect_uri={window.location.origin}
37-
onRedirectCallback={onRedirectCallback}
38-
>
39-
<ModalContextProvider>
40-
<SearchContextProvider>
41-
<Router />
42-
</SearchContextProvider>
43-
</ModalContextProvider>
44-
</Auth0Provider>
20+
<ModalContextProvider>
21+
<SearchContextProvider>
22+
<Router />
23+
</SearchContextProvider>
24+
</ModalContextProvider>
4525
,
4626
</React.StrictMode>,
4727
document.getElementById("root")

client/src/lib/common.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
import { getFreshToken, decodeToken } from "@topcoder-platform/tc-auth-lib";
12
import config from "../config";
23

34
/**
45
* Returns the nickname of the logged in user
5-
* @param {Object} auth0User The auth0 user object
66
*/
7-
export function getNickname(auth0User) {
8-
return auth0User[config.AUTH0.handleClaims];
7+
export async function getNickname() {
8+
try {
9+
const token = await getFreshToken();
10+
11+
const user = decodeToken(token);
12+
return user[config.AUTH.handleClaims];
13+
} catch (error) {
14+
console.log(
15+
"An error occurred trying to retrieve the nickname of the logged in user"
16+
);
17+
console.log(error);
18+
alert("You are not logged in");
19+
}
920
}

client/src/pages/Search/Global.jsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import ProfileCard from "../../components/ProfileCard";
88
import Pagination from "../../components/Pagination";
99

1010
import * as helper from "./helper";
11-
import { useAuth0 } from "../../react-auth0-spa";
1211
import { getAttributes } from "../../lib/company-attributes";
1312
import { useSearch, FILTERS } from "../../lib/search";
1413
import { makeColorIterator, avatarColors } from "../../lib/colors";
@@ -45,7 +44,6 @@ function usePrevious(value) {
4544
}
4645

4746
export default function SearchGlobal({ keyword }) {
48-
const { isLoading, isAuthenticated, user: auth0User } = useAuth0();
4947
const apiClient = api();
5048
const searchContext = useSearch();
5149
const [isSearching, setIsSearching] = React.useState(false);
@@ -75,10 +73,6 @@ export default function SearchGlobal({ keyword }) {
7573

7674
// Non-static data and Non-user related data
7775
React.useEffect(() => {
78-
if (isLoading || !isAuthenticated) {
79-
return;
80-
}
81-
8276
let isSubscribed = true;
8377

8478
(async () => {
@@ -118,14 +112,10 @@ export default function SearchGlobal({ keyword }) {
118112
cancelTokenSource.cancel();
119113
};
120114
// eslint-disable-next-line react-hooks/exhaustive-deps
121-
}, [isLoading, isAuthenticated, auth0User]);
115+
}, []);
122116

123117
// For the Search tab only (non static data)
124118
React.useEffect(() => {
125-
if (isLoading || !isAuthenticated) {
126-
return;
127-
}
128-
129119
const criteria = {};
130120
if (
131121
searchContext.filters[FILTERS.LOCATIONS].active &&
@@ -261,7 +251,7 @@ export default function SearchGlobal({ keyword }) {
261251
})();
262252

263253
// eslint-disable-next-line react-hooks/exhaustive-deps
264-
}, [isLoading, isAuthenticated, keyword, orderBy, searchContext]);
254+
}, [keyword, orderBy, searchContext]);
265255

266256
/**
267257
* Update's the style for the sort order element, based on the current width of the page

client/src/pages/Search/Groups.jsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import ProfileCardGroupWrapper from "../../components/ProfileCardGroupWrapper";
55
import Pagination from "../../components/Pagination";
66

77
import { makeColorIterator, avatarColors } from "../../lib/colors";
8-
import { useAuth0 } from "../../react-auth0-spa";
98
import config from "../../config";
109
import api from "../../services/api";
1110
import * as groupLib from "../../lib/groups";
@@ -17,7 +16,6 @@ const colorIterator = makeColorIterator(avatarColors);
1716

1817
export default function SearchGroups() {
1918
const apiClient = api();
20-
const { user: auth0User } = useAuth0();
2119
const [myGroups, setMyGroups] = React.useState([]);
2220
const [otherGroups, setOtherGroups] = React.useState([]);
2321
const [loadingGroups, setLoadingGroups] = React.useState(false);
@@ -36,9 +34,10 @@ export default function SearchGroups() {
3634
setLoadingGroups(true);
3735

3836
(async () => {
37+
const nickname = await getNickname();
3938
const groups = await groupLib.getGroups(
4039
apiClient,
41-
getNickname(auth0User),
40+
nickname,
4241
cancelTokenSource.token
4342
);
4443

0 commit comments

Comments
 (0)