Skip to content

Commit 82f34ed

Browse files
authored
Add redirection to previous page after sign in (canada-ca#1499)
1 parent 6c05398 commit 82f34ed

4 files changed

Lines changed: 77 additions & 77 deletions

File tree

frontend/src/App.js

Lines changed: 33 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import { Flex, Link, CSSReset, useToast, Box } from '@chakra-ui/core'
1212
import { SkipLink } from './SkipLink'
1313
// import { TwoFactorNotificationBar } from './TwoFactorNotificationBar'
1414
import { useUserState } from './UserState'
15-
import { RouteIf } from './RouteIf'
1615
import { ErrorBoundary } from 'react-error-boundary'
1716
import { ErrorFallbackMessage } from './ErrorFallbackMessage'
1817
import { FloatingMenu } from './FloatingMenu'
18+
import PrivateRoute from './PrivateRoute'
1919

2020
const PageNotFound = lazy(() => import('./PageNotFound'))
2121
const CreateUserPage = lazy(() => import('./CreateUserPage'))
@@ -141,90 +141,50 @@ export default function App() {
141141
component={ResetPasswordPage}
142142
/>
143143

144-
<Route
145-
alternate="/sign-in"
146-
path="/organizations"
147-
render={({ match: { url } }) => (
148-
<>
149-
<Route path={`${url}`} component={Organizations} exact />
150-
<Route path={`${url}/:orgSlug`} exact>
151-
<ErrorBoundary FallbackComponent={ErrorFallbackMessage}>
152-
<OrganizationDetails />
153-
</ErrorBoundary>
154-
</Route>
155-
</>
156-
)}
157-
/>
144+
<PrivateRoute path="/organizations" exact>
145+
<Organizations />
146+
</PrivateRoute>
158147

159-
<RouteIf
160-
condition={isLoggedIn()}
161-
alternate="/sign-in"
162-
path="/admin"
163-
>
148+
<PrivateRoute path="/organizations/:orgSlug" exact>
149+
<OrganizationDetails />
150+
</PrivateRoute>
151+
152+
<PrivateRoute path="/admin">
164153
<ErrorBoundary FallbackComponent={ErrorFallbackMessage}>
165154
<AdminPage />
166155
</ErrorBoundary>
167-
</RouteIf>
156+
</PrivateRoute>
168157

169-
<RouteIf
170-
condition={true}
171-
alternate="/sign-in"
172-
path="/domains"
173-
render={({ match: { url } }) => (
174-
<>
175-
<Route path={`${url}`} component={DomainsPage} exact />
176-
<Route
177-
path={`${url}/:domainSlug`}
178-
component={DmarcGuidancePage}
179-
exact
180-
/>
181-
<Route
182-
path={`${url}/:domainSlug/dmarc-report/:period?/:year?`}
183-
component={DmarcReportPage}
184-
exact
185-
/>
186-
</>
187-
)}
188-
/>
158+
<PrivateRoute path="/domains" exact>
159+
<DomainsPage />
160+
</PrivateRoute>
189161

190-
<RouteIf
191-
condition={true}
192-
alternate="/sign-in"
193-
path="/dmarc-summaries"
194-
render={({ match: { url } }) => (
195-
<>
196-
<Route
197-
path={`${url}`}
198-
component={DmarcByDomainPage}
199-
exact
200-
/>
201-
</>
202-
)}
203-
/>
162+
<PrivateRoute path="/domains/:domainSlug" exact>
163+
<DmarcGuidancePage />
164+
</PrivateRoute>
204165

205-
<RouteIf
206-
condition={isLoggedIn()}
207-
alternate="/sign-in"
208-
path="/user"
166+
<PrivateRoute
167+
path="/domains/:domainSlug/dmarc-report/:period?/:year?"
168+
exact
209169
>
210-
<UserPage userName={currentUser.userName} />
211-
</RouteIf>
170+
<DmarcReportPage />
171+
</PrivateRoute>
212172

213-
<RouteIf
214-
condition={isLoggedIn()}
215-
alternate="/sign-in"
216-
path="/two-factor-code"
217-
>
173+
<PrivateRoute path="/dmarc-summaries" exact>
174+
<DmarcByDomainPage />
175+
</PrivateRoute>
176+
177+
<PrivateRoute path="/user">
178+
<UserPage username={currentUser.userName} />
179+
</PrivateRoute>
180+
181+
<PrivateRoute path="/two-factor-code">
218182
<QRcodePage userName={currentUser.userName} />
219-
</RouteIf>
183+
</PrivateRoute>
220184

221-
<RouteIf
222-
condition={true}
223-
alternate="/sign-in"
224-
path="/dmarc-report/:period?/:year?"
225-
>
185+
<PrivateRoute path="/dmarc-report/:period?/:year?">
226186
<DmarcReportPage />
227-
</RouteIf>
187+
</PrivateRoute>
228188

229189
<Route component={PageNotFound} />
230190
</Switch>

frontend/src/PrivateRoute.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// From https://reactrouter.com/web/example/auth-workflow
2+
3+
import React from 'react'
4+
import { node } from 'prop-types'
5+
import { Route, Redirect, useLocation } from 'react-router-dom'
6+
import { useUserState } from './UserState'
7+
8+
// A wrapper for <Route> that redirects to the login
9+
// screen if you're not yet authenticated.
10+
export default function PrivateRoute({ children, ...rest }) {
11+
const { isLoggedIn } = useUserState()
12+
const location = useLocation()
13+
return (
14+
<Route
15+
{...rest}
16+
render={() =>
17+
isLoggedIn() ? (
18+
children
19+
) : (
20+
<Redirect
21+
to={{
22+
pathname: '/sign-in',
23+
state: { from: location },
24+
}}
25+
/>
26+
)
27+
}
28+
/>
29+
)
30+
}
31+
32+
PrivateRoute.propTypes = {
33+
children: node,
34+
}

frontend/src/SignInPage.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
Text,
1212
useToast,
1313
} from '@chakra-ui/core'
14-
import { Link as RouteLink, useHistory } from 'react-router-dom'
14+
import { Link as RouteLink, useHistory, useLocation } from 'react-router-dom'
1515
import { useMutation } from '@apollo/client'
1616
import { Formik } from 'formik'
1717
import { SIGN_IN } from './graphql/mutations'
@@ -26,8 +26,11 @@ export default function SignInPage() {
2626
const { login } = useUserState()
2727
const { i18n } = useLingui()
2828
const history = useHistory()
29+
const location = useLocation()
2930
const toast = useToast()
3031

32+
const { from } = location.state || { from: { pathname: '/' } }
33+
3134
const validationSchema = object().shape({
3235
password: string().required(fieldRequirements.password.required.message),
3336
email: string()
@@ -55,7 +58,7 @@ export default function SignInPage() {
5558
userName: signIn.result.authResult.user.userName,
5659
})
5760
// // redirect to the home page.
58-
history.push('/')
61+
history.push(from)
5962
// // Display a welcome message
6063
toast({
6164
title: i18n._(t`Sign In.`),
@@ -72,6 +75,7 @@ export default function SignInPage() {
7275
`/authenticate/${signIn.result.sendMethod.toLowerCase()}/${
7376
signIn.result.authenticateToken
7477
}`,
78+
{ from },
7579
)
7680
} else {
7781
toast({

frontend/src/TwoFactorAuthenticatePage.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { t, Trans } from '@lingui/macro'
33
import { useLingui } from '@lingui/react'
44
import { number, object } from 'yup'
55
import { Box, Heading, useToast, Stack } from '@chakra-ui/core'
6-
import { useHistory, useParams } from 'react-router-dom'
6+
import { useHistory, useParams, useLocation } from 'react-router-dom'
77
import { useMutation } from '@apollo/client'
88
import { Formik } from 'formik'
99
import { useUserState } from './UserState'
@@ -15,9 +15,11 @@ import { TrackerButton } from './TrackerButton'
1515
export default function TwoFactorAuthenticatePage() {
1616
const { login } = useUserState()
1717
const history = useHistory()
18+
const location = useLocation()
1819
const toast = useToast()
1920
const { i18n } = useLingui()
2021
const { sendMethod, authenticateToken } = useParams()
22+
const { from } = location.state || { from: { pathname: '/' } }
2123

2224
const validationSchema = object().shape({
2325
twoFactorCode: number()
@@ -44,7 +46,7 @@ export default function TwoFactorAuthenticatePage() {
4446
userName: authenticate.authResult.user.userName,
4547
})
4648
// // redirect to the home page.
47-
history.push('/')
49+
history.replace(from)
4850
// // Display a welcome message
4951
toast({
5052
title: i18n._(t`Sign In.`),

0 commit comments

Comments
 (0)