Skip to content

Commit 51c9870

Browse files
feat: expand and visualize auth states
1 parent 37f6008 commit 51c9870

File tree

4 files changed

+72
-18
lines changed

4 files changed

+72
-18
lines changed

web/src/App.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,47 @@
11
import { router } from './router'
22
import { RouterProvider } from 'react-router-dom'
33
import Header from './common/components/Header'
4+
import { AuthContext } from 'react-oauth2-code-pkce'
5+
import { useContext } from 'react'
6+
import { Button, Progress, Typography } from '@equinor/eds-core-react'
7+
import styled from 'styled-components'
8+
9+
const hasAuthConfig = process.env.REACT_APP_AUTH === '1'
10+
11+
const CenterContainer = styled.div`
12+
display: flex;
13+
gap: 12px;
14+
flex-direction: column;
15+
justify-content: center;
16+
align-items: center;
17+
width: 100vw;
18+
height: 100vh;
19+
`
420

521
function App() {
22+
const { token, error, login, loginInProgress } = useContext(AuthContext)
23+
24+
if (hasAuthConfig && error) {
25+
return <CenterContainer>{error}</CenterContainer>
26+
}
27+
28+
if (hasAuthConfig && loginInProgress) {
29+
return (
30+
<CenterContainer>
31+
<Typography>Login in progress.</Typography>
32+
<Progress.Dots color="primary" />
33+
</CenterContainer>
34+
)
35+
}
36+
37+
if (hasAuthConfig && !token) {
38+
return (
39+
<CenterContainer>
40+
<Button onClick={login}>Log in</Button>
41+
</CenterContainer>
42+
)
43+
}
44+
645
return (
746
<>
847
<Header />

web/src/auth.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
export const authConfig = {
1+
import { TAuthConfig } from 'react-oauth2-code-pkce'
2+
3+
export const authConfig: TAuthConfig = {
24
clientId: process.env.REACT_APP_AUTH_CLIENT_ID || '',
35
authorizationEndpoint: process.env.REACT_APP_AUTH_ENDPOINT || '',
46
tokenEndpoint: process.env.REACT_APP_TOKEN_ENDPOINT || '',
57
scope: process.env.REACT_APP_AUTH_SCOPE || '',
68
redirectUri: process.env.REACT_APP_AUTH_REDIRECT_URI || '',
79
logoutEndpoint: process.env.REACT_APP_LOGOUT_ENDPOINT || '',
10+
autoLogin: false,
811
preLogin: () =>
912
localStorage.setItem(
1013
'preLoginPath',

web/src/common/components/Header.tsx

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
import { useContext } from 'react'
22
import { AuthContext } from 'react-oauth2-code-pkce'
3-
import { Icon, TopBar, Typography } from '@equinor/eds-core-react'
4-
import { receipt } from '@equinor/eds-icons'
3+
import { Button, Icon, TopBar, Typography } from '@equinor/eds-core-react'
4+
import { log_out, receipt } from '@equinor/eds-icons'
55

66
Icon.add({ receipt })
77

88
const Header = () => {
9-
const { tokenData } = useContext(AuthContext)
9+
const { tokenData, logOut } = useContext(AuthContext)
10+
11+
// unique_name might be azure-specific, different tokenData-fields might
12+
// be available if another OAuth-provider is used.
13+
const username = tokenData?.unique_name
1014
return (
1115
<TopBar>
1216
<TopBar.Header>
1317
<Icon name="receipt" />
1418
Todo App
1519
</TopBar.Header>
16-
<TopBar.Actions>
17-
<Typography variant="caption">
18-
{tokenData
19-
? `Logged in as ${tokenData?.['unique_name']}`
20-
: 'Unauthenticated'}
21-
</Typography>
20+
<TopBar.Actions style={{ gap: 8 }}>
21+
{tokenData ? (
22+
<>
23+
<Typography>Logged in as {username}</Typography>
24+
<Button
25+
variant="ghost_icon"
26+
aria-label="Log out"
27+
title="Log out"
28+
onClick={logOut}
29+
>
30+
<Icon data={log_out} title="Log out" />
31+
</Button>
32+
</>
33+
) : (
34+
<Typography variant="caption">Unauthenticated</Typography>
35+
)}
2236
</TopBar.Actions>
2337
</TopBar>
2438
)

web/src/index.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ const hasAuthConfig = process.env.REACT_APP_AUTH === '1'
99
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement)
1010
root.render(
1111
<React.StrictMode>
12-
<>
13-
{hasAuthConfig ? (
14-
<AuthProvider authConfig={authConfig}>
15-
<App />
16-
</AuthProvider>
17-
) : (
12+
{hasAuthConfig ? (
13+
<AuthProvider authConfig={authConfig}>
1814
<App />
19-
)}
20-
</>
15+
</AuthProvider>
16+
) : (
17+
<App />
18+
)}
2119
</React.StrictMode>
2220
)

0 commit comments

Comments
 (0)