File tree Expand file tree Collapse file tree 4 files changed +72
-18
lines changed
Expand file tree Collapse file tree 4 files changed +72
-18
lines changed Original file line number Diff line number Diff line change 11import { router } from './router'
22import { RouterProvider } from 'react-router-dom'
33import 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
521function 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 />
Original file line number Diff line number Diff line change 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' ,
Original file line number Diff line number Diff line change 11import { useContext } from 'react'
22import { 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
66Icon . add ( { receipt } )
77
88const 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 )
Original file line number Diff line number Diff line change @@ -9,14 +9,12 @@ const hasAuthConfig = process.env.REACT_APP_AUTH === '1'
99const root = ReactDOM . createRoot ( document . getElementById ( 'root' ) as HTMLElement )
1010root . 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)
You can’t perform that action at this time.
0 commit comments