-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend): add oauth with microsoft (#71)
This PR introduces authentication sequence for oauth based authentication.
- Loading branch information
Showing
28 changed files
with
810 additions
and
161 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
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
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,31 @@ | ||
import React, { useContext, useEffect, useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import useClient from './utils/useClient'; | ||
|
||
const ConfigContext = React.createContext({}); | ||
|
||
ConfigContextProvider.propTypes = { | ||
children: PropTypes.oneOfType([ | ||
PropTypes.arrayOf(PropTypes.node), | ||
PropTypes.node, | ||
]).isRequired, | ||
}; | ||
|
||
export default ConfigContext; | ||
|
||
export function ConfigContextProvider({ children }) { | ||
const client = useClient(); | ||
const [config, setConfig] = useState(null); | ||
useEffect(() => { | ||
client.get('config').then((res) => { | ||
setConfig(JSON.parse(res.text)); | ||
}); | ||
}, [setConfig, client]); | ||
return ( | ||
<ConfigContext.Provider value={config}>{children}</ConfigContext.Provider> | ||
); | ||
} | ||
|
||
export function useConfig() { | ||
return useContext(ConfigContext); | ||
} |
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,70 @@ | ||
import React, { useContext, useEffect, useState } from 'react'; | ||
import useClient from '../utils/useClient'; | ||
import { useConfig } from '../ConfigContext'; | ||
import parseApiErrorMessage from '../utils/parseApiErrorMessage'; | ||
import { PublicClientApplication } from '@azure/msal-browser'; | ||
import MicrosoftIcon from '../images/MicrosoftIcon.png'; | ||
import classes from '../styles/SignInPage.module.css'; | ||
|
||
export default function AzureButton({ onFailure, onSuccess }) { | ||
const config = useConfig(); | ||
const client = useClient(); | ||
const msalInstance = new PublicClientApplication({ | ||
auth: { | ||
clientId: config?.auth?.OAUTH_AZURE_CLIENT_ID, | ||
authority: config?.auth?.OAUTH_AZURE_AUTHORITY, | ||
redirectUri: config?.auth?.OAUTH_AZURE_REDIRECTURI, | ||
navigateToLoginRequestUrl: true, | ||
}, | ||
cache: { | ||
cacheLocation: config?.auth?.OAUTH_AZURE_CACHELOCATION, | ||
storeAuthStateInCookie: config?.auth?.OAUTH_AZURE_STOREAUTHSTATEINCOOKIE, | ||
}, | ||
}); | ||
|
||
useEffect(() => { | ||
const initialize = async () => { | ||
await msalInstance.initialize(); | ||
await msalInstance | ||
.handleRedirectPromise() | ||
.then((response) => { | ||
if (response?.accessToken) { | ||
client | ||
.post('sessions', { | ||
azure: response, | ||
}) | ||
.then((response) => { | ||
onSuccess(response?.body?.email, response); | ||
}) | ||
.catch((error) => { | ||
onFailure(parseApiErrorMessage(error)); | ||
}); | ||
} | ||
}) | ||
.catch((error) => { | ||
onFailure(parseApiErrorMessage(error)); | ||
}); | ||
}; | ||
try { | ||
initialize(); | ||
} catch (e) { | ||
onFailure('Error log in in with Azure'); | ||
} | ||
}, []); | ||
|
||
const handleLogin = async (evt) => { | ||
evt.preventDefault(); | ||
|
||
await msalInstance.initialize(); | ||
await msalInstance.loginRedirect(); | ||
}; | ||
|
||
return ( | ||
<> | ||
<button className={`${classes.orloginbutton} my-3`} onClick={handleLogin}> | ||
<img className={`${classes.loginorimg} mx-1`} src={MicrosoftIcon} /> | ||
Sign in with Microsoft | ||
</button> | ||
</> | ||
); | ||
} |
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 React from 'react'; | ||
import { renderWithContext, renderWithRouter } from '../utils/testUtils'; | ||
import AzureButton from './AzureButton'; | ||
|
||
describe('<AzureButton>', () => { | ||
it('render azure authenticate option on login and register screen', async () => { | ||
const { getByText } = renderWithContext(<AzureButton />); | ||
const buttonComponent = getByText('Continue With Microsoft'); | ||
expect(buttonComponent).toBeInTheDocument(); | ||
}); | ||
}); |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Oops, something went wrong.