Skip to content

Commit

Permalink
RequireAuth with requireSuperUser & redirect props
Browse files Browse the repository at this point in the history
  • Loading branch information
thoomasbro committed Jun 6, 2024
1 parent aeea225 commit 976b7f0
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 48 deletions.
8 changes: 4 additions & 4 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function App() {

<Route
element={
<RequireAuth>
<RequireAuth redirect requireSuperUser>
<BackOfficePage />
</RequireAuth>
}
Expand All @@ -61,7 +61,7 @@ export function App() {

<Route
element={
<RequireAuth>
<RequireAuth redirect requireSuperUser>
<SideWindow />
</RequireAuth>
}
Expand All @@ -70,7 +70,7 @@ export function App() {

<Route
element={
<RequireAuth>
<RequireAuth redirect>
<HomePage />
</RequireAuth>
}
Expand All @@ -79,7 +79,7 @@ export function App() {

<Route
element={
<RequireAuth>
<RequireAuth redirect requireSuperUser>
<HomePage />
</RequireAuth>
}
Expand Down
23 changes: 1 addition & 22 deletions frontend/src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import { getOIDCUser } from 'auth/getOIDCUser'

import { setAuthorizationHeader } from './utils/setAuthorizationHeaders'
import { normalizeRtkBaseQuery } from '../utils/normalizeRtkBaseQuery'
import { sha256 } from '../utils/sha256'

import type { BackendApiErrorResponse } from './types'

Expand All @@ -18,26 +17,6 @@ export const geoserverApi = createApi({
// =============================================================================
// Monitorenv Private API

const AUTHORIZATION_HEADER = 'authorization'
const CORRELATION_HEADER = 'X-Correlation-Id'

const setAuthorizationHeader = async headers => {
const user = getOIDCUser()
const token = user?.access_token

// If we have a token set in state, we pass it.
if (token) {
headers.set(AUTHORIZATION_HEADER, `Bearer ${token}`)

if (crypto?.subtle) {
const hashedToken = await sha256(token)

headers.set(CORRELATION_HEADER, hashedToken)
}
}

return headers
}
// We'll need that later on for authentication.
const monitorenvPrivateApiQuery = fetchBaseQuery({
baseUrl: '/bff',
Expand Down
23 changes: 23 additions & 0 deletions frontend/src/api/utils/setAuthorizationHeaders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { sha256 } from '@utils/sha256'
import { getOIDCUser } from 'auth/getOIDCUser'

const AUTHORIZATION_HEADER = 'authorization'
const CORRELATION_HEADER = 'X-Correlation-Id'

export const setAuthorizationHeader = async headers => {
const user = getOIDCUser()
const token = user?.access_token

// If we have a token set in state, we pass it.
if (token) {
headers.set(AUTHORIZATION_HEADER, `Bearer ${token}`)

if (crypto?.subtle) {
const hashedToken = await sha256(token)

headers.set(CORRELATION_HEADER, hashedToken)
}
}

return headers
}
14 changes: 11 additions & 3 deletions frontend/src/components/RequireAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getOIDCConfig } from 'auth/getOIDCConfig'
import { useAuth } from 'react-oidc-context'
import { Navigate } from 'react-router-dom'

export function RequireAuth({ children, requireSuperUser = false }) {
export function RequireAuth({ children, redirect = false, requireSuperUser = false }) {
const oidcConfig = getOIDCConfig()
const auth = useAuth()
const { data: user } = useGetCurrentUserAuthorizationQuery(undefined, { skip: !auth?.isAuthenticated })
Expand All @@ -13,11 +13,19 @@ export function RequireAuth({ children, requireSuperUser = false }) {
}

if (!auth.isAuthenticated) {
return <Navigate replace to="/login" />
if (redirect) {
return <Navigate replace to="/login" />
}

return null
}

if (requireSuperUser && !user?.isSuperUser) {
return <Navigate replace to="/register" />
if (redirect) {
return <Navigate replace to="/register" />
}

return null
}

return children
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/features/Account/components/Account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ButtonWrapper } from '@features/MainWindow/components/RightMenu/ButtonW
import { useAppDispatch } from '@hooks/useAppDispatch'
import { useAppSelector } from '@hooks/useAppSelector'
import { Accent, Button, Icon, MapMenuDialog, Size } from '@mtes-mct/monitor-ui'
import { getOIDCConfig } from 'auth/getOIDCConfig'
import { globalActions } from 'domain/shared_slices/Global'
import { useAuth } from 'react-oidc-context'
import styled from 'styled-components'
Expand All @@ -14,6 +15,8 @@ export function Account() {
const isAccountVisible = useAppSelector(state => state.global.isAccountDialogVisible)
const auth = useAuth()

const oidcConfig = getOIDCConfig()

const logout = () => {
auth.signoutRedirect()
}
Expand All @@ -22,6 +25,10 @@ export function Account() {
dispatch(globalActions.setDisplayedItems({ isAccountDialogVisible: !isAccountVisible }))
}

if (!oidcConfig.IS_OIDC_ENABLED) {
return null
}

return (
<ButtonWrapper topPosition={MARGIN_TOP}>
{isAccountVisible && (
Expand Down
28 changes: 9 additions & 19 deletions frontend/src/pages/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import { LoadingSpinnerWall } from 'ui/LoadingSpinnerWall'
export function Login() {
const oidcConfig = getOIDCConfig()
const auth = useAuth()
const {
data: user,
isError,
isSuccess
} = useGetCurrentUserAuthorizationQuery(undefined, { skip: !auth?.isAuthenticated })
const { data: user, isSuccess } = useGetCurrentUserAuthorizationQuery(undefined, { skip: !auth?.isAuthenticated })

const logout = () => {
auth.removeUser()
Expand Down Expand Up @@ -45,19 +41,6 @@ export function Login() {
break
}

if (auth.isLoading) {
return (
<Wrapper>
Chargement...
<LoadingSpinnerWall isVesselShowed />
</Wrapper>
)
}

if (auth.error || isError) {
return <div>Oops... {auth?.error?.message}</div>
}

if (auth.isAuthenticated) {
return (
<Wrapper>
Expand All @@ -70,7 +53,14 @@ export function Login() {

return (
<Wrapper>
<Button onClick={() => auth.signinRedirect()}>Se connecter</Button>
{auth.isLoading && (
<>
Chargement...
<LoadingSpinnerWall isVesselShowed />
</>
)}
{auth.error && <div>Oops... {auth.error?.message}</div>}
{!auth.isLoading && <Button onClick={() => auth.signinRedirect()}>Se connecter</Button>}
<ToastContainer />
</Wrapper>
)
Expand Down

0 comments on commit 976b7f0

Please sign in to comment.