Skip to content

Commit

Permalink
feat: add redirect to authRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismclarke committed Mar 3, 2022
1 parent 5ceeda1 commit 981fb2d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/modules/admin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function AdminModuleContainer() {
return (
<AdminStoreContext.Provider value={new AdminStore()}>
<AdminSubheader />
<AuthRoute component={adminRoutes} roleRequired="admin" />
<AuthRoute component={adminRoutes} roleRequired="admin" redirect="/" />
<div>Coming Soon...</div>
</AdminStoreContext.Provider>
)
Expand Down
22 changes: 14 additions & 8 deletions src/pages/common/AuthRoute.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react'
import { Route, RouteProps } from 'react-router'
import { Redirect, Route, RouteProps } from 'react-router'
import { observer } from 'mobx-react'
import { UserRole } from 'src/models/user.models'
import Text from 'src/components/Text'
Expand All @@ -14,6 +14,8 @@ import { AuthWrapper } from 'src/components/Auth/AuthWrapper'
interface IProps extends RouteProps {
component: React.ComponentType<any>
roleRequired?: UserRole
/** Page to redirect if role not satisfied (default shows message) */
redirect?: string
}
interface IState {}
@observer
Expand All @@ -26,13 +28,17 @@ export class AuthRoute extends React.Component<IProps, IState> {
<AuthWrapper
roleRequired={roleRequired}
fallback={
<Flex justifyContent="center" mt="40px" data-cy="auth-route-deny">
<Text regular>
{roleRequired
? `${roleRequired} role required to access this page`
: 'Please login to access this page'}
</Text>
</Flex>
this.props.redirect ? (
<Redirect to={this.props.redirect} />
) : (
<Flex justifyContent="center" mt="40px" data-cy="auth-route-deny">
<Text regular>
{roleRequired
? `${roleRequired} role required to access this page`
: 'Please login to access this page'}
</Text>
</Flex>
)
}
>
<Route {...rest} render={props => <Component {...props} />} />
Expand Down

0 comments on commit 981fb2d

Please sign in to comment.