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

Commit d04f1ba

Browse files
committed
cross imports, updated Topcoder Auth
1 parent d9af72c commit d04f1ba

File tree

22 files changed

+569
-10177
lines changed

22 files changed

+569
-10177
lines changed

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,68 @@ Make sure you have [Heroky CLI](https://devcenter.heroku.com/articles/heroku-cli
5656
- Now you have to configure frame app to use the URL provided by Heroku like `https://<APP-NAME>.herokuapp.com/topcoder-micro-frontends-navbar-app.js` to load this microapp.
5757
- NOTE: Authorization would not work because only predefined list of domain allowed by `accounts-app`.
5858

59+
### Cross microfrontend imports
60+
61+
This app exports functions to be imported by other microapps.
62+
- `login` - redirects to login page
63+
- `logout` - clears session storage and redirects to logout page
64+
- `setAppMenu` - sets sidebar menu for the app by app's `path`
65+
- `getAuthUserTokens` - returns a promise which resolves to object with user tokens `{ tokenV3, tokenV2 }`
66+
- `getAuthUserProfile` - returns a promise which resolves to the user profile object
67+
68+
#### How to export
69+
70+
- To export any function we have to `export` in file [src/topcoder-micro-frontends-navbar-app.js](src/topcoder-micro-frontends-navbar-app.js).
71+
- If we want to prepare some function for exporting, the good place to do so is inside [src/utils/exports.js](src/utils/exports.js).
72+
- We have to bind actions before exporting.
73+
- It's not recommended to export the whole Redux Store to keep only navbar responsible for updating it. It's better to create promises which would return some particular value from the store.
74+
75+
#### How to import
76+
77+
When we want to use methods exported in the navbar microapp in other apps we have to make sure that webpack would not process imports from navbar as it is handled by `importmaps`, see [Cross microfrontend imports](https://single-spa.js.org/docs/recommended-setup/#cross-microfrontend-imports).
78+
79+
##### How to import in React app
80+
81+
For example see https://github.com/topcoder-platform/micro-frontends-react-app
82+
83+
1. Add `@topcoder/micro-frontends-navbar-app` to `externals` in webpack config:
84+
```js
85+
externals: {
86+
"@topcoder/micro-frontends-navbar-app": "@topcoder/micro-frontends-navbar-app",
87+
},
88+
```
89+
90+
2. As `importmaps` only work in browser and don't work in unit test, we have to mock this module in unit tests. For example by creating a file `src/__mocks__/@topcoder/micro-frontends-navbar-app.js` with the content like:
91+
```js
92+
module.exports = {
93+
login: () => {},
94+
logout: () => {},
95+
setAppMenu: () => {},
96+
getAuthUserTokens: () => new Promise(() => {}),
97+
getAuthUserProfile: () => new Promise(() => {}),
98+
};
99+
```
100+
101+
##### How to import in Angular app
102+
103+
For example see https://github.com/topcoder-platform/micro-frontends-angular-app
104+
105+
1. Add `@topcoder/micro-frontends-navbar-app` to `externals` in webpack config:
106+
```js
107+
externals: {
108+
"@topcoder/micro-frontends-navbar-app": "@topcoder/micro-frontends-navbar-app",
109+
},
110+
```
111+
112+
2. Add type definition in `src/typings.d.ts`:
113+
```js
114+
declare module '@topcoder/micro-frontends-navbar-app' {
115+
export const login: any;
116+
export const logout: any;
117+
export const setAppMenu: any;
118+
export const getAuthUserTokens: any;
119+
export const getAuthUserProfile: any;
120+
}
121+
```
122+
123+
3. TODO: How to make e2e tests work for Angular? So far they fail with error `Module not found: Error: Can't resolve '@topcoder/micro-frontends-navbar-app'`

config/development.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
URL: {
3-
ACCOUNTS_APP_CONNECTOR: "https://accounts.topcoder-dev.com/connector.html",
4-
AUTH: "https://accounts.topcoder-dev.com",
3+
ACCOUNTS_APP_CONNECTOR: "https://accounts-auth0.topcoder-dev.com",
4+
AUTH: "https://accounts-auth0.topcoder-dev.com",
55
},
66
API: {
77
V3: "https://api.topcoder-dev.com/v3",

config/production.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
URL: {
3-
ACCOUNTS_APP_CONNECTOR: "https://accounts.topcoder.com/connector.html",
4-
AUTH: "https://accounts.topcoder.com",
3+
ACCOUNTS_APP_CONNECTOR: "https://accounts-auth0.topcoder.com",
4+
AUTH: "https://accounts-auth0.topcoder.com",
55
},
66
API: {
77
V3: "https://api.topcoder.com/v3",

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module.exports = {
33
transform: {
44
"^.+\\.(j|t)sx?$": "babel-jest",
55
},
6-
transformIgnorePatterns: ["node_modules/?!(tc-accounts)"],
6+
transformIgnorePatterns: ["node_modules/?!(tc-auth-lib)"],
77
moduleNameMapper: {
88
"\\.css$": "identity-obj-proxy",
99
"\\.svg$": "<rootDir>/__mocks__/fileMock.js",

0 commit comments

Comments
 (0)