-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
71 additions
and
15 deletions.
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './lib/msal-react'; | ||
export * from './lib/button-login/button-login'; | ||
export * from './lib/button-logout/button-logout'; | ||
export * from './lib/account-context/account-context'; |
7 changes: 7 additions & 0 deletions
7
libs/lib-msal-react/src/lib/account-context/account-context.module.css
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,7 @@ | ||
/* | ||
* Replace this with your own classes | ||
* | ||
* e.g. | ||
* .container { | ||
* } | ||
*/ |
10 changes: 10 additions & 0 deletions
10
libs/lib-msal-react/src/lib/account-context/account-context.spec.tsx
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 { render } from '@testing-library/react'; | ||
|
||
import AccountContext from './account-context'; | ||
|
||
describe('AccountContext', () => { | ||
it('should render successfully', () => { | ||
const { baseElement } = render(<AccountContext />); | ||
expect(baseElement).toBeTruthy(); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
libs/lib-msal-react/src/lib/account-context/account-context.tsx
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,42 @@ | ||
import { createContext, useContext, useEffect, useState } from 'react'; | ||
import { useMsal } from '@azure/msal-react'; | ||
|
||
type AccountState = { | ||
username: string | null; | ||
loading: boolean; | ||
}; | ||
|
||
/** | ||
* Provides a React context for managing authentication using MSAL. | ||
* This component wraps your application and provides access to the MSAL instance and authentication state. | ||
* It also handles authentication and token acquisition using MSAL. | ||
* @see https://react.dev/learn/passing-data-deeply-with-context | ||
*/ | ||
export const AccountContext = createContext<AccountState>({ username: null, loading: true }); | ||
|
||
// Create a provider component | ||
export const AccountProvider: React.FC<{ children: React.ReactNode }> = ({ | ||
children, | ||
}) => { | ||
const { inProgress, accounts } = useMsal(); | ||
const [account, setAccount] = useState<AccountState>({ username: null, loading: true }); | ||
|
||
useEffect(() => { | ||
if (inProgress === 'none' && accounts.length > 0) { | ||
setAccount({ username: accounts[0].username, loading: false }); | ||
} | ||
}, [inProgress, accounts]); | ||
|
||
return ( | ||
<AccountContext.Provider | ||
value={account} | ||
> | ||
{children} | ||
</AccountContext.Provider> | ||
); | ||
}; | ||
|
||
// Create a hook to use the context | ||
export const useAccount = () => { | ||
return useContext(AccountContext); | ||
}; |
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