-
Notifications
You must be signed in to change notification settings - Fork 6
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
Feat/react app/msal/current account #1646
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7b32453
fix(module-msal): re-export AuthenticationResult from @azure/msal-bro…
odinr 7e73a0c
feat(app-react-msal): create utils for msal
odinr d40f77e
feat(cookbooks/app-react-msal): create a cookbook for react app msal
odinr 7e75969
Update packages/react/app/src/msal/useToken.ts
odinr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@equinor/fusion-framework-module-msal': patch | ||
--- | ||
|
||
re-export `AuthenticationResult` from `@azure/msal-browser` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'@equinor/fusion-framework-react-app': minor | ||
--- | ||
|
||
Created namespace for MSAL: | ||
|
||
- Created hooks for accessing current authenticated account | ||
- Created hooks for acquiring token | ||
- Created hooks for acquiring access token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@equinor/fusion-framework-cookbook-app-react-msal': major | ||
--- | ||
|
||
created a cookbook for reading msal account and token |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { defineAppManifest, mergeManifests } from '@equinor/fusion-framework-cli'; | ||
|
||
export default defineAppManifest((env, { base }) => { | ||
if (env.command === 'serve') { | ||
return mergeManifests(base, { | ||
key: 'msal', | ||
}); | ||
} | ||
return base; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "@equinor/fusion-framework-cookbook-app-react-msal", | ||
"version": "0.0.0", | ||
"description": "", | ||
"private": true, | ||
"type": "module", | ||
"main": "src/index.ts", | ||
"scripts": { | ||
"build": "fusion-framework-cli app build", | ||
"dev": "fusion-framework-cli app dev", | ||
"docker": "cd .. && sh docker-script.sh app-react" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"@equinor/fusion-framework-cli": "^9.3.0", | ||
"@equinor/fusion-framework-react-app": "^4.1.3", | ||
"@types/react": "^18.2.20", | ||
"@types/react-dom": "^18.2.7", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"typescript": "^5.1.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { useFramework } from '@equinor/fusion-framework-react-app/framework'; | ||
import { | ||
useAccessToken, | ||
useToken, | ||
useCurrentAccount, | ||
} from '@equinor/fusion-framework-react-app/msal'; | ||
import { useEffect, useMemo, useState } from 'react'; | ||
|
||
export const App = () => { | ||
/** | ||
* Retrieves the current user account. | ||
* @returns The current user account. | ||
*/ | ||
const user = useCurrentAccount(); | ||
const framework = useFramework(); | ||
/** | ||
* This state variable is used to manage the scopes for MSAL authentication. | ||
*/ | ||
const [scopes, setScopes] = useState<string[]>([]); | ||
useEffect(() => { | ||
/** | ||
* get default scope from the framework service discovery module | ||
*/ | ||
framework.modules.serviceDiscovery | ||
.resolveService('portal') | ||
.then((x) => x.defaultScopes) | ||
.then(setScopes); | ||
}, [framework]); | ||
|
||
return ( | ||
<div | ||
style={{ | ||
display: 'flex', | ||
justifyContent: 'center', | ||
flexFlow: 'column', | ||
padding: '0 2rem', | ||
}} | ||
> | ||
<div style={{ maxWidth: 'calc(100% - 4rem)', overflow: 'scroll' }}> | ||
<div> | ||
<h1>😎 Current user:</h1> | ||
<pre>{JSON.stringify(user, null, 2)}</pre> | ||
</div> | ||
{scopes.length && <AccessToken scopes={scopes} />} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
/** | ||
* Component for rendering an access token. | ||
* @param scopes - The scopes required for the access token. | ||
*/ | ||
const AccessToken = ({ scopes }: { scopes: string[] }) => { | ||
const { token } = useToken(useMemo(() => ({ scopes }), [scopes])); | ||
const { token: accessToken } = useAccessToken(useMemo(() => ({ scopes }), [scopes])); | ||
return ( | ||
<div> | ||
<h2>🧩 Token:</h2> | ||
<b>access token</b> | ||
<br /> | ||
<code style={{ lineBreak: 'anywhere' }}>{accessToken}</code> | ||
<br /> | ||
<br /> | ||
<b>token response</b> | ||
<code> | ||
<pre style={{ overflow: 'scroll' }}>{JSON.stringify(token, null, 4)}</pre> | ||
</code> | ||
</div> | ||
); | ||
}; | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { AppModuleInitiator } from '@equinor/fusion-framework-react-app'; | ||
|
||
export const configure: AppModuleInitiator = (configurator, env) => { | ||
/** print render environment arguments */ | ||
console.log('configuring application', env); | ||
|
||
/** callback when configurations is created */ | ||
configurator.onConfigured((config) => { | ||
console.log('application config created', config); | ||
}); | ||
|
||
/** callback when the application modules has initialized */ | ||
configurator.onInitialized((instance) => { | ||
console.log('application config initialized', instance); | ||
}); | ||
}; | ||
|
||
export default configure; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { createElement } from 'react'; | ||
import { createRoot } from 'react-dom/client'; | ||
|
||
import { ComponentRenderArgs, makeComponent } from '@equinor/fusion-framework-react-app'; | ||
|
||
import configure from './config'; | ||
import App from './App'; | ||
|
||
/** create a render component */ | ||
const appComponent = createElement(App); | ||
|
||
/** create React render root component */ | ||
const createApp = (args: ComponentRenderArgs) => makeComponent(appComponent, args, configure); | ||
|
||
/** Render function */ | ||
export const renderApp = (el: HTMLElement, args: ComponentRenderArgs) => { | ||
/** make render element */ | ||
const app = createApp(args); | ||
|
||
/** create render root from provided element */ | ||
const root = createRoot(el); | ||
|
||
/** render Application */ | ||
root.render(createElement(app)); | ||
|
||
/** Teardown */ | ||
return () => root.unmount(); | ||
}; | ||
|
||
export default renderApp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"jsx": "react-jsx", | ||
}, | ||
"references": [ | ||
{ | ||
"path": "../../packages/react/app" | ||
}, | ||
{ | ||
"path": "../../packages/cli" | ||
}, | ||
], | ||
"include": [ | ||
"src/**/*" | ||
], | ||
"exclude": [ | ||
"node_modules", | ||
"lib" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { useCurrentAccount } from './useCurrentAccount'; | ||
export { useAccessToken } from './useAccessToken'; | ||
export { useToken } from './useToken'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { AuthRequest } from '@equinor/fusion-framework-module-msal/client'; | ||
import { useToken } from './useToken'; | ||
|
||
/** | ||
* Custom hook that retrieves an access token for the specified authentication request. | ||
* | ||
* @param req - The authentication request. | ||
* @returns An object containing the access token, pending state, and error. | ||
*/ | ||
export const useAccessToken = ( | ||
req: AuthRequest, | ||
): { token?: string; pending: boolean; error: unknown } => { | ||
const { token, error, pending } = useToken(req); | ||
return { token: token?.accessToken, pending, error }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { AccountInfo } from '@equinor/fusion-framework-module-msal'; | ||
import useAppModule from '../useAppModule'; | ||
|
||
/** | ||
* Retrieves the current account information from the MSAL provider. | ||
* @returns The current account information or undefined if no account is available. | ||
*/ | ||
export const useCurrentAccount = (): AccountInfo | undefined => { | ||
const msalProvider = useAppModule('auth'); | ||
return msalProvider.defaultAccount; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { AuthRequest, AuthenticationResult } from '@equinor/fusion-framework-module-msal/client'; | ||
import useAppModule from '../useAppModule'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
/** | ||
* Custom hook for acquiring an authentication token using MSAL. | ||
* @param req - The authentication request. | ||
* @returns An object containing the acquired token, pending state, and error. | ||
*/ | ||
export const useToken = ( | ||
req: AuthRequest, | ||
): { token?: AuthenticationResult; pending: boolean; error: unknown } => { | ||
const msalProvider = useAppModule('auth'); | ||
const [token, setToken] = useState<AuthenticationResult | undefined>(undefined); | ||
const [pending, setPending] = useState<boolean>(false); | ||
const [error, setError] = useState<unknown>(null); | ||
useEffect(() => { | ||
setPending(true); | ||
setToken(undefined); | ||
msalProvider | ||
.acquireToken(req) | ||
.then((token) => token && setToken(token)) | ||
.catch(setError) | ||
.finally(() => setPending(false)); | ||
}, [msalProvider, req]); | ||
return { token, pending, error }; | ||
}; | ||
|
||
export default useToken; | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Insert
⏎