-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from ChrisFajardo-TRI/web-ui-uses-cognito-hos…
…ted-ui add cognitohosted UI
- Loading branch information
Showing
3 changed files
with
96 additions
and
3 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
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,86 @@ | ||
import { Amplify, Auth, Hub } from 'aws-amplify'; | ||
import styles from './login.module.css'; | ||
|
||
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'; | ||
|
||
Amplify.configure(AWS_CONFIG); | ||
|
||
type UserAuthProps = { | ||
token: string; | ||
userEmail: string; | ||
signOut: () => void; | ||
}; | ||
|
||
const UserAuthContext = createContext({} as UserAuthProps); | ||
|
||
// @ts-ignore | ||
export default function CognitoProvider({ children }) { | ||
const [token, setToken] = useState(''); | ||
const [userEmail, setUserEmail] = useState('[email protected]'); | ||
const signOut = useCallback(() => { | ||
Auth.signOut().catch((error) => console.log(error)); | ||
}, []); | ||
|
||
useEffect(() => { | ||
const unsubscribe = Hub.listen('auth', ({ payload: { event, data } }) => { | ||
switch (event) { | ||
case 'signIn': | ||
case 'cognitoHostedUI': | ||
setToken(data.signInUserSession.idToken.jwtToken); | ||
setUserEmail(data.signInUserSession.idToken.payload.email); | ||
break; | ||
case 'signIn_failure': | ||
case 'cognitoHostedUI_failure': | ||
console.log('Error', data); | ||
break; | ||
} | ||
}); | ||
|
||
// fetch the jwt token for use of api calls later | ||
Auth.currentAuthenticatedUser() | ||
.then((currentUser) => { | ||
setToken(currentUser.signInUserSession.idToken.jwtToken); | ||
setUserEmail(currentUser.signInUserSession.idToken.payload.email); | ||
}) | ||
.catch(() => console.log('Not signed in')); | ||
|
||
return unsubscribe; | ||
}, []); | ||
|
||
const value = useMemo( | ||
() => ({ | ||
token, | ||
userEmail, | ||
signOut | ||
}), | ||
[token, userEmail, signOut] | ||
); | ||
|
||
if (!AMPLIFY_ENABLED) { | ||
return <>{children}</>; | ||
} | ||
|
||
// the Phase Mapper app needs a token to work, show the login screen | ||
if (token === '') { | ||
return ( | ||
<div className={styles.app}> | ||
You have to login to use Synthesis App (Piro). | ||
<button className={styles.login} onClick={() => Auth.federatedSignIn()}> | ||
Login | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
return <UserAuthContext.Provider value={value}>{children}</UserAuthContext.Provider>; | ||
} | ||
|
||
function useUserAuthContext() { | ||
const context = useContext(UserAuthContext); | ||
if (context === undefined) { | ||
throw new Error('useUserAuth must be used within a UserAuthProvider'); | ||
} | ||
return context; | ||
} | ||
|
||
export { useUserAuthContext }; |
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 @@ | ||
.app { | ||
padding: 1em; | ||
} | ||
.login { | ||
margin-top: 1em; | ||
display: block; | ||
} |