Skip to content

Commit 3d7f594

Browse files
krischarbonneauCharles-Pham
authored andcommitted
Update validation to redirect to login instead (#679)
Signed-off-by: Charles-Pham <[email protected]>
1 parent c0d4913 commit 3d7f594

File tree

9 files changed

+120
-15
lines changed

9 files changed

+120
-15
lines changed

components/Layout.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { lato, notoSans } from '../utils/fonts'
99
import { useRouter } from 'next/router'
1010
import throttle from 'lodash.throttle'
1111
import IdleTimeout from './IdleTimeout'
12+
import { signOut } from 'next-auth/react'
1213

1314
export default function Layout(props) {
1415
const t = props.locale === 'en' ? en : fr
@@ -37,9 +38,10 @@ export default function Layout(props) {
3738
useEffect(() => {
3839
window.addEventListener('visibilitychange', throttledVisiblityChangeEvent)
3940
window.addEventListener('click', throttledOnClickEvent)
40-
//If validateSession call indicates an invalid MSCA session, redirect to logout
41+
//If validateSession call indicates an invalid MSCA session, end next-auth session and redirect to login
4142
if (response?.status === 401) {
42-
router.push(`/${props.locale}/auth/logout`)
43+
signOut()
44+
router.push(`/${props.locale}/auth/login`)
4345
}
4446
//Remove event on unmount to prevent a memory leak with the cleanup
4547
return () => {

pages/api/refresh-msca.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default async function handler(req, res) {
4343
res.status(401).json({ success: sessionValid, id: id })
4444
}
4545
} else {
46-
res.status(500).json({ success: false })
46+
res.status(401).json({ success: false })
4747
logger.error('Authentication is not valid')
4848
}
4949
} else {

pages/contact-us/[id].tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,28 @@ export const getServerSideProps = (async ({ req, res, locale, params }) => {
9595

9696
const token = await getIdToken(req)
9797

98-
//If Next-Auth session is valid, check to see if ECAS session is and redirect to logout if not
98+
//If Next-Auth session is valid, check to see if ECAS session is. If not, clear session cookies and redirect to login
9999
if (!AuthIsDisabled() && (await AuthIsValid(req, session))) {
100100
const sessionValid = await ValidateSession(
101101
process.env.CLIENT_ID,
102102
token?.sid,
103103
)
104104
if (!sessionValid) {
105+
// Clear all session cookies
106+
const isSecure = req.headers['x-forwarded-proto'] === 'https'
107+
const cookiePrefix = `${isSecure ? '__Secure-' : ''}next-auth.session-token`
108+
const cookies = []
109+
for (const cookie of Object.keys(req.cookies)) {
110+
if (cookie.startsWith(cookiePrefix)) {
111+
cookies.push(
112+
`${cookie}=deleted; Max-Age=0; path=/ ${isSecure ? '; Secure ' : ''}`,
113+
)
114+
}
115+
}
116+
res.setHeader('Set-Cookie', cookies)
105117
return {
106118
redirect: {
107-
destination: `/${locale}/auth/logout`,
119+
destination: `/${locale}/auth/login`,
108120
permanent: false,
109121
},
110122
}

pages/contact-us/index.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,28 @@ export const getServerSideProps = (async ({ req, res, locale }) => {
117117

118118
const token = await getIdToken(req)
119119

120-
//If Next-Auth session is valid, check to see if ECAS session is and redirect to logout if not
120+
//If Next-Auth session is valid, check to see if ECAS session is. If not, clear session cookies and redirect to login
121121
if (!AuthIsDisabled() && (await AuthIsValid(req, session))) {
122122
const sessionValid = await ValidateSession(
123123
process.env.CLIENT_ID,
124124
token?.sid,
125125
)
126126
if (!sessionValid) {
127+
// Clear all session cookies
128+
const isSecure = req.headers['x-forwarded-proto'] === 'https'
129+
const cookiePrefix = `${isSecure ? '__Secure-' : ''}next-auth.session-token`
130+
const cookies = []
131+
for (const cookie of Object.keys(req.cookies)) {
132+
if (cookie.startsWith(cookiePrefix)) {
133+
cookies.push(
134+
`${cookie}=deleted; Max-Age=0; path=/ ${isSecure ? '; Secure ' : ''}`,
135+
)
136+
}
137+
}
138+
res.setHeader('Set-Cookie', cookies)
127139
return {
128140
redirect: {
129-
destination: `/${locale}/auth/logout`,
141+
destination: `/${locale}/auth/login`,
130142
permanent: false,
131143
},
132144
}

pages/decision-reviews.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,28 @@ export async function getServerSideProps({ req, res, locale }) {
122122

123123
const token = await getIdToken(req)
124124

125-
//If Next-Auth session is valid, check to see if ECAS session is and redirect to logout if not
125+
//If Next-Auth session is valid, check to see if ECAS session is. If not, clear session cookies and redirect to login
126126
if (!AuthIsDisabled() && (await AuthIsValid(req, session))) {
127127
const sessionValid = await ValidateSession(
128128
process.env.CLIENT_ID,
129129
token?.sid,
130130
)
131131
if (!sessionValid) {
132+
// Clear all session cookies
133+
const isSecure = req.headers['x-forwarded-proto'] === 'https'
134+
const cookiePrefix = `${isSecure ? '__Secure-' : ''}next-auth.session-token`
135+
const cookies = []
136+
for (const cookie of Object.keys(req.cookies)) {
137+
if (cookie.startsWith(cookiePrefix)) {
138+
cookies.push(
139+
`${cookie}=deleted; Max-Age=0; path=/ ${isSecure ? '; Secure ' : ''}`,
140+
)
141+
}
142+
}
143+
res.setHeader('Set-Cookie', cookies)
132144
return {
133145
redirect: {
134-
destination: `/${locale}/auth/logout`,
146+
destination: `/${locale}/auth/login`,
135147
permanent: false,
136148
},
137149
}

pages/my-dashboard.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,28 @@ export async function getServerSideProps({ req, res, locale }) {
141141

142142
const token = await getIdToken(req)
143143

144-
//If Next-Auth session is valid, check to see if ECAS session is and redirect to logout if not
144+
//If Next-Auth session is valid, check to see if ECAS session is. If not, clear session cookies and redirect to login
145145
if (!AuthIsDisabled() && (await AuthIsValid(req, session))) {
146146
const sessionValid = await ValidateSession(
147147
process.env.CLIENT_ID,
148148
token?.sid,
149149
)
150150
if (!sessionValid) {
151+
// Clear all session cookies
152+
const isSecure = req.headers['x-forwarded-proto'] === 'https'
153+
const cookiePrefix = `${isSecure ? '__Secure-' : ''}next-auth.session-token`
154+
const cookies = []
155+
for (const cookie of Object.keys(req.cookies)) {
156+
if (cookie.startsWith(cookiePrefix)) {
157+
cookies.push(
158+
`${cookie}=deleted; Max-Age=0; path=/ ${isSecure ? '; Secure ' : ''}`,
159+
)
160+
}
161+
}
162+
res.setHeader('Set-Cookie', cookies)
151163
return {
152164
redirect: {
153-
destination: `/${locale}/auth/logout`,
165+
destination: `/${locale}/auth/login`,
154166
permanent: false,
155167
},
156168
}

pages/profile.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,28 @@ export async function getServerSideProps({ req, res, locale }) {
9292

9393
const token = await getIdToken(req)
9494

95-
//If Next-Auth session is valid, check to see if ECAS session is and redirect to logout if not
95+
//If Next-Auth session is valid, check to see if ECAS session is. If not, clear session cookies and redirect to login
9696
if (!AuthIsDisabled() && (await AuthIsValid(req, session))) {
9797
const sessionValid = await ValidateSession(
9898
process.env.CLIENT_ID,
9999
token?.sid,
100100
)
101101
if (!sessionValid) {
102+
// Clear all session cookies
103+
const isSecure = req.headers['x-forwarded-proto'] === 'https'
104+
const cookiePrefix = `${isSecure ? '__Secure-' : ''}next-auth.session-token`
105+
const cookies = []
106+
for (const cookie of Object.keys(req.cookies)) {
107+
if (cookie.startsWith(cookiePrefix)) {
108+
cookies.push(
109+
`${cookie}=deleted; Max-Age=0; path=/ ${isSecure ? '; Secure ' : ''}`,
110+
)
111+
}
112+
}
113+
res.setHeader('Set-Cookie', cookies)
102114
return {
103115
redirect: {
104-
destination: `/${locale}/auth/logout`,
116+
destination: `/${locale}/auth/login`,
105117
permanent: false,
106118
},
107119
}

pages/security-settings.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,28 @@ export async function getServerSideProps({ req, res, locale }) {
8888

8989
const token = await getIdToken(req)
9090

91-
//If Next-Auth session is valid, check to see if ECAS session is and redirect to logout if not
91+
//If Next-Auth session is valid, check to see if ECAS session is. If not, clear session cookies and redirect to login
9292
if (!AuthIsDisabled() && (await AuthIsValid(req, session))) {
9393
const sessionValid = await ValidateSession(
9494
process.env.CLIENT_ID,
9595
token?.sid,
9696
)
9797
if (!sessionValid) {
98+
// Clear all session cookies
99+
const isSecure = req.headers['x-forwarded-proto'] === 'https'
100+
const cookiePrefix = `${isSecure ? '__Secure-' : ''}next-auth.session-token`
101+
const cookies = []
102+
for (const cookie of Object.keys(req.cookies)) {
103+
if (cookie.startsWith(cookiePrefix)) {
104+
cookies.push(
105+
`${cookie}=deleted; Max-Age=0; path=/ ${isSecure ? '; Secure ' : ''}`,
106+
)
107+
}
108+
}
109+
res.setHeader('Set-Cookie', cookies)
98110
return {
99111
redirect: {
100-
destination: `/${locale}/auth/logout`,
112+
destination: `/${locale}/auth/login`,
101113
permanent: false,
102114
},
103115
}

public/robots.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#
2+
#
3+
# _____
4+
# | |
5+
# | | | |
6+
# |_____|
7+
# ____ ___|_|___ ____
8+
# ()___) ()___)
9+
# // /| |\ \\
10+
# // / | | \ \\
11+
# (___) |___________| (___)
12+
# (___) (_______) (___)
13+
# (___) (___) (___)
14+
# (___) |_| (___)
15+
# (___) ___/___\___ | |
16+
# | | | | | |
17+
# | | |___________| /___\
18+
# /___\ ||| ||| // \\
19+
# // \\ ||| ||| \\ //
20+
# \\ // ||| ||| \\ //
21+
# \\ // ()__) (__()
22+
# /// \\\
23+
# /// \\\
24+
# _///___ ___\\\_
25+
# |_______| |_______|
26+
#
27+
#
28+
#
29+
User-Agent: *
30+
Allow: /
31+
Disallow: /api/

0 commit comments

Comments
 (0)