-
Notifications
You must be signed in to change notification settings - Fork 0
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
8785c25
commit 6950658
Showing
15 changed files
with
444 additions
and
84 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { | ||
Fragment, | ||
type ReactElement, | ||
} from 'react'; | ||
import { Navigate } from 'react-router-dom'; | ||
|
||
import useAuth from '#hooks/domain/useAuth'; | ||
|
||
import { type ExtendedProps } from './routes/common'; | ||
|
||
interface Props { | ||
children: ReactElement, | ||
context: ExtendedProps, | ||
absolutePath: string, | ||
} | ||
function Auth(props: Props) { | ||
const { | ||
context, | ||
children, | ||
absolutePath, | ||
} = props; | ||
|
||
const { isAuthenticated } = useAuth(); | ||
|
||
if (context.visibility === 'is-authenticated' && !isAuthenticated) { | ||
return ( | ||
<Navigate to="/login" /> | ||
); | ||
} | ||
if (context.visibility === 'is-not-authenticated' && isAuthenticated) { | ||
return ( | ||
<Navigate to="/" /> | ||
); | ||
} | ||
|
||
return ( | ||
<Fragment | ||
key={absolutePath} | ||
> | ||
{children} | ||
</Fragment> | ||
); | ||
} | ||
|
||
export default Auth; |
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,47 @@ | ||
import { | ||
type MyInputIndexRouteObject, | ||
type MyInputNonIndexRouteObject, | ||
type MyOutputIndexRouteObject, | ||
type MyOutputNonIndexRouteObject, | ||
wrapRoute, | ||
} from '#utils/routes'; | ||
import { Component as RootLayout } from '#views/RootLayout'; | ||
|
||
import Auth from '../Auth'; | ||
import PageError from '../PageError'; | ||
|
||
export type ExtendedProps = { | ||
title: string, | ||
visibility: 'is-authenticated' | 'is-not-authenticated' | 'anything', | ||
permissions?: ( | ||
params: Record<string, number | string | undefined | null> | undefined | null, | ||
) => boolean; | ||
}; | ||
|
||
interface CustomWrapRoute { | ||
<T>( | ||
myRouteOptions: MyInputIndexRouteObject<T, ExtendedProps> | ||
): MyOutputIndexRouteObject<ExtendedProps> | ||
<T>( | ||
myRouteOptions: MyInputNonIndexRouteObject<T, ExtendedProps> | ||
): MyOutputNonIndexRouteObject<ExtendedProps> | ||
} | ||
|
||
export const customWrapRoute: CustomWrapRoute = wrapRoute; | ||
|
||
// NOTE: We should not use layout or index routes in links | ||
|
||
export const rootLayout = customWrapRoute({ | ||
path: '/', | ||
errorElement: <PageError />, | ||
component: { | ||
eagerLoad: true, | ||
render: RootLayout, | ||
props: {}, | ||
}, | ||
wrapperComponent: Auth, | ||
context: { | ||
title: 'IFRC Alert Hub', | ||
visibility: 'anything', | ||
}, | ||
}); |
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
Oops, something went wrong.