Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
DChorn-ANS committed Apr 16, 2024
1 parent 1e0b5cb commit e99747c
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/components/utilities/CippauthCheck.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useLoadClientPrincipalQuery } from 'src/store/api/auth'
import { useDispatch } from 'react-redux'
import { updateAccessToken } from 'src/store/features/auth'
import { Navigate } from 'react-router-dom'

export const useAuthCheck = (allowedRoles) => {
const dispatch = useDispatch()
const { data: profile, isFetching } = useLoadClientPrincipalQuery()
if (isFetching) {
return { isLoading: true, component: null }
}
dispatch(updateAccessToken(profile))
let roles = profile?.clientPrincipal?.userRoles || []

if (!profile?.clientPrincipal) {
return {
component: (
<Navigate to={`/login?redirect_uri=${encodeURIComponent(window.location.href)}`} />
),
result: false,
}
}
if (allowedRoles && !allowedRoles.some((role) => roles.includes(role))) {
return { component: <Navigate to="/403" />, result: true }
}
return { component: null, result: false }
}

0 comments on commit e99747c

Please sign in to comment.