-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
12 changed files
with
163 additions
and
80 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,5 +1,5 @@ | ||
export type LazyLoadingState = 'loading' | 'error' | 'data'; | ||
export interface LazyLoading<T> { | ||
loading: boolean; | ||
state: LazyLoadingState; | ||
data: T; | ||
error: boolean; | ||
} |
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
20 changes: 20 additions & 0 deletions
20
packages/client/src/routes/ConditionRoute/Condition.types.d.ts
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,20 @@ | ||
import { RouteProps } from 'react-router'; | ||
|
||
import { User } from '../../providers/api/CurrentUserProvider/CurrentUserProvider.types'; | ||
|
||
export interface ConditionFuncProps { | ||
currentUser: User | undefined; | ||
} | ||
|
||
export type ConditionFunc = (props: ConditionFuncProps) => boolean; | ||
|
||
export interface BasicConditionProps { | ||
condition: ConditionFunc; | ||
redirectTo: string; | ||
} | ||
|
||
export interface BasicRouteProps extends RouteProps { | ||
path: string; | ||
} | ||
|
||
export interface ConditionRouteProps extends BasicConditionProps, BasicRouteProps {} |
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 React, { useEffect, useState } from 'react'; | ||
import { Redirect, Route } from 'react-router-dom'; | ||
|
||
import { PageLoader } from '../../elements'; | ||
import { useAuth } from '../../hooks'; | ||
import { LazyLoading } from '../../interfaces/lazyLoading'; | ||
import { ConditionRouteProps } from './Condition.types'; | ||
|
||
const ConditionRoute = ({ redirectTo, condition, ...props }: ConditionRouteProps) => { | ||
const { checkAuth } = useAuth(); | ||
const [{ data, state }, setLazyLoading] = useState<LazyLoading<boolean>>({ | ||
data: false, | ||
state: 'loading', | ||
}); | ||
|
||
useEffect(() => { | ||
let isActive = true; | ||
|
||
checkAuth() | ||
.then((currentUser) => { | ||
if (isActive) { | ||
const routeCondition = condition({ currentUser }); | ||
setLazyLoading(() => ({ state: 'data', data: routeCondition })); | ||
} | ||
}) | ||
.catch(() => { | ||
if (isActive) { | ||
setLazyLoading((prev) => ({ ...prev, state: 'error' })); | ||
} | ||
}); | ||
|
||
return () => { | ||
isActive = false; | ||
}; | ||
}, [checkAuth, condition]); | ||
|
||
switch (true) { | ||
case state === 'loading': | ||
return <PageLoader />; | ||
case state === 'data' && data: | ||
return <Route {...props} />; | ||
default: | ||
return <Redirect to={{ pathname: redirectTo }} />; | ||
} | ||
}; | ||
|
||
export default ConditionRoute; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,43 @@ | ||
import { routes } from '../constants'; | ||
import { Role } from '../enums/role.enum'; | ||
import { BasicConditionProps, ConditionFunc } from './ConditionRoute/Condition.types'; | ||
|
||
type onlyAuthenticatedConditionType = (requiredRoles?: [Role]) => ConditionFunc; | ||
|
||
export const onlyAuthenticatedCondition: onlyAuthenticatedConditionType = ( | ||
requiredRoles?: [Role], | ||
) => { | ||
return ({ currentUser }) => { | ||
if (!currentUser) { | ||
return false; | ||
} | ||
if (!requiredRoles || !requiredRoles.length) { | ||
return true; | ||
} | ||
|
||
return ( | ||
currentUser && | ||
currentUser.roles && | ||
requiredRoles.some((role) => currentUser.roles.includes(role)) | ||
); | ||
}; | ||
}; | ||
|
||
const onlyUnauthenticatedCondition: ConditionFunc = ({ currentUser }) => !currentUser; | ||
|
||
const allowAllCondition: ConditionFunc = () => true; | ||
|
||
export const onlyAuthenticated: BasicConditionProps = { | ||
condition: onlyAuthenticatedCondition(), | ||
redirectTo: routes.LOGIN, | ||
}; | ||
|
||
export const onlyUnauthenticated: BasicConditionProps = { | ||
condition: onlyUnauthenticatedCondition, | ||
redirectTo: routes.HOME, | ||
}; | ||
|
||
export const allowAll: BasicConditionProps = { | ||
condition: allowAllCondition, | ||
redirectTo: routes.HOME, | ||
}; |
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