-
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.
- Loading branch information
1 parent
2ac8d60
commit 74be2e1
Showing
11 changed files
with
239 additions
and
11 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 +1 @@ | ||
REACT_APP_ARCGIS_REST_CLIENT_ID=abcdef | ||
REACT_APP_ARCGIS_REST_CLIENT_ID=abcd |
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
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,58 @@ | ||
import Background from "../../../public/images/tools-background.webp"; | ||
import styled from "styled-components"; | ||
import { | ||
getCurrentLayoutProperty, | ||
useAppLayout, | ||
} from "../../utils/hooks/layout"; | ||
|
||
import { ArcGISIdentityManager } from '@esri/arcgis-rest-request'; | ||
import { getAuthOptions } from "../../utils/arcgis-auth"; | ||
|
||
export type LayoutProps = { | ||
layout: string; | ||
}; | ||
|
||
const AuthContainer = styled.div<LayoutProps>` | ||
display: flex; | ||
flex-direction: column; | ||
width: 100%; | ||
overflow: auto; | ||
overflow-x: hidden; | ||
background: url(${Background}); | ||
background-attachment: fixed; | ||
background-size: cover; | ||
height: ${getCurrentLayoutProperty({ | ||
desktop: "calc(100vh - 65px)", | ||
tablet: "calc(100vh - 65px)", | ||
mobile: "calc(100vh - 58px)", | ||
})}; | ||
margin-top: ${getCurrentLayoutProperty({ | ||
desktop: "65px", | ||
tablet: "65px", | ||
mobile: "58px", | ||
})}; | ||
`; | ||
|
||
export const AuthApp = () => { | ||
const layout = useAppLayout(); | ||
|
||
const { redirectUrl, clientId } = getAuthOptions(); | ||
if (!clientId) { | ||
console.error("The ClientId is not defined in .env file."); | ||
} else { | ||
const options = { | ||
clientId: clientId, | ||
redirectUri: redirectUrl, | ||
popup: true, | ||
pkce: true | ||
} | ||
ArcGISIdentityManager.completeOAuth2(options); | ||
} | ||
return ( | ||
<AuthContainer id="dashboard-container" layout={layout}> | ||
</AuthContainer> | ||
); | ||
|
||
} |
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,46 @@ | ||
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit"; | ||
import { RootState } from "../store"; | ||
import { getAuthenticatedUser, arcGisRequestLogin, arcGisRequestLogout } from '../../utils/arcgis-auth'; | ||
|
||
// Define a type for the slice state | ||
export interface ArcGisAuthState { | ||
user: string; | ||
} | ||
|
||
const initialState: ArcGisAuthState = { | ||
user: getAuthenticatedUser(), | ||
} | ||
|
||
const arcGisAuthSlice = createSlice({ | ||
name: "arcGisAuth", | ||
initialState, | ||
reducers: { | ||
}, | ||
extraReducers: (builder) => { | ||
builder.addCase(arcGisLogin.fulfilled, (state, action) => { | ||
state.user = action.payload || ''; | ||
}) | ||
.addCase(arcGisLogout.fulfilled, (state) => { | ||
state.user = ''; | ||
}) | ||
}, | ||
}); | ||
|
||
export const arcGisLogin = createAsyncThunk( | ||
'arcGisLogin', | ||
async () => { | ||
return await arcGisRequestLogin(); | ||
} | ||
); | ||
|
||
export const arcGisLogout = createAsyncThunk( | ||
'arcGisLogout', | ||
async () => { | ||
return await arcGisRequestLogout(); | ||
} | ||
); | ||
|
||
export const selectUser = (state: RootState): string => | ||
state.arcGisAuth.user; | ||
|
||
export default arcGisAuthSlice.reducer; |
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,72 @@ | ||
import { ArcGISIdentityManager } from '@esri/arcgis-rest-request'; | ||
|
||
const ARCGIS_REST_USER_SESSION = '__ARCGIS_REST_USER_SESSION__'; | ||
const ARCGIS_REST_USER_INFO = '__ARCGIS_REST_USER_INFO__'; | ||
|
||
const updateSessionInfo = async (session?: ArcGISIdentityManager): Promise<string> => { | ||
let email: string = ''; | ||
if (session) { | ||
localStorage.setItem(ARCGIS_REST_USER_SESSION, session.serialize()); | ||
const user = await session.getUser(); | ||
email = user.email || ''; | ||
localStorage.setItem(ARCGIS_REST_USER_INFO, email); | ||
} else { | ||
localStorage.removeItem(ARCGIS_REST_USER_SESSION); | ||
localStorage.removeItem(ARCGIS_REST_USER_INFO); | ||
} | ||
return email; | ||
} | ||
|
||
/** | ||
* Gets the redirection URL and the client ID to use in the ArcGIS authentication workflow. | ||
* @returns the redirection URL and the client ID. | ||
*/ | ||
export const getAuthOptions = () => { | ||
return { | ||
redirectUrl: `${window.location.protocol}//${window.location.hostname}:${window.location.port}/auth`, | ||
clientId: process.env.REACT_APP_ARCGIS_REST_CLIENT_ID | ||
} | ||
} | ||
|
||
/** | ||
* Gets the email of the currently logged in user. | ||
* @returns the user's email or an empty string if the user is not logged in. | ||
*/ | ||
export const getAuthenticatedUser = (): string => { | ||
return localStorage.getItem(ARCGIS_REST_USER_INFO) || ''; | ||
} | ||
|
||
/** | ||
* | ||
* @returns | ||
*/ | ||
export const arcGisRequestLogin = async () => { | ||
const { redirectUrl, clientId } = getAuthOptions(); | ||
|
||
if (!clientId) { | ||
console.error("The ClientId is not defined in .env file."); | ||
return ''; | ||
} | ||
const options = { | ||
clientId: clientId, | ||
redirectUri: redirectUrl, | ||
popup: true, | ||
pkce: true | ||
} | ||
|
||
let email = ''; | ||
let session: ArcGISIdentityManager | undefined; | ||
try { | ||
session = await ArcGISIdentityManager.beginOAuth2(options); | ||
} | ||
finally { | ||
// In case of an exception the session is not set. | ||
// So the following call will remove any session stored in the local storage. | ||
email = await updateSessionInfo(session); | ||
} | ||
return email; | ||
}; | ||
|
||
export const arcGisRequestLogout = async () => { | ||
return await updateSessionInfo(); | ||
}; |
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