Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent app access after logout by ensuring proper session handling #3077

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/OSC/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const locals = ['fa']
export default function middleware(req: NextRequest) {
const loggedin = req.cookies.get('authToken')
const { pathname, href, host } = req.nextUrl
const response = NextResponse.next()
response.headers.set('Cache-Control', 'no-store')

const urlSplitList = href.split('/')
const hostIndex = urlSplitList.findIndex(item => item === host)
let langSuffix = ''
Expand All @@ -22,7 +25,7 @@ export default function middleware(req: NextRequest) {
return NextResponse.redirect(new URL(redirectUrl, req.url))
}

return NextResponse.next()
return response
}

export const config = {
Expand Down
16 changes: 8 additions & 8 deletions apps/dragon-foods/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
// middleware.ts

import { NextRequest, NextResponse } from 'next/server'

export default function middleware(req: NextRequest) {
const loggedin = req.cookies.get('authToken')
const { pathname } = req.nextUrl

const url = req.nextUrl
const urlObj = new URL(url)
const searchParams = urlObj.searchParams
// Create a response with no-store caching
const response = NextResponse.next()
response.headers.set('Cache-Control', 'no-store')

const searchParams = req.nextUrl.searchParams
const externalUrlParam = searchParams.get('external_url')

// Redirect logged-in users away from sign-in or sign-up pages
if (loggedin && (pathname === '/signIn' || pathname === '/signUp')) {
return NextResponse.redirect(new URL('/', req.url))
}

// Redirect non-logged-in users to the sign-in page
if (!loggedin && pathname !== '/signIn' && pathname !== '/signUp') {
const signInRoute = externalUrlParam ? `/signIn?external_url=${externalUrlParam}` : '/signIn'

return NextResponse.redirect(new URL(signInRoute, req.url))
}

// It's important to return a response for all paths, you might want to return `undefined` or `NextResponse.next()`
// for other cases to let the request continue.
return NextResponse.next()
// Allow the request to proceed if no conditions are met
return response
}

export const config = {
Expand Down
5 changes: 4 additions & 1 deletion apps/dsep/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { NextRequest, NextResponse } from 'next/server'
export default function middleware(req: NextRequest) {
const loggedin = req.cookies.get('authToken')
const { pathname } = req.nextUrl
const response = NextResponse.next()
response.headers.set('Cache-Control', 'no-store')

if (loggedin && (pathname === '/signin' || pathname === '/signUp')) {
return NextResponse.redirect(new URL('/', req.url))
}
Expand All @@ -14,7 +17,7 @@ export default function middleware(req: NextRequest) {
}
// It's important to return a response for all paths, you might want to return `undefined` or `NextResponse.next()`
// for other cases to let the request continue.
return NextResponse.next()
return response
}

export const config = {
Expand Down
5 changes: 3 additions & 2 deletions apps/dsnp-v2/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export default function middleware(req: NextRequest) {
// TypeScript will correctly understand the type of `loggedin`
const loggedin = req.cookies.get('authToken')
const { pathname } = req.nextUrl
console.log('Dank', pathname, loggedin)
const response = NextResponse.next()
response.headers.set('Cache-Control', 'no-store')

if (loggedin && (pathname === '/signin' || pathname === '/signUp')) {
// Correctly redirect to the home page if the user is already logged in
Expand All @@ -20,7 +21,7 @@ export default function middleware(req: NextRequest) {

// It's important to return a response for all paths, you might want to return `undefined` or `NextResponse.next()`
// for other cases to let the request continue.
return NextResponse.next()
return response
}

export const config = {
Expand Down
9 changes: 5 additions & 4 deletions apps/earth-support-initiative/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ export default function middleware(req: NextRequest) {
const loggedin = req.cookies.get('authToken')
const { pathname } = req.nextUrl

const url = req.nextUrl
const urlObj = new URL(url)
const searchParams = urlObj.searchParams
const response = NextResponse.next()
response.headers.set('Cache-Control', 'no-store')

const searchParams = req.nextUrl.searchParams

const externalUrlParam = searchParams.get('external_url')

Expand All @@ -24,7 +25,7 @@ export default function middleware(req: NextRequest) {

// It's important to return a response for all paths, you might want to return `undefined` or `NextResponse.next()`
// for other cases to let the request continue.
return NextResponse.next()
return response
}

export const config = {
Expand Down
9 changes: 5 additions & 4 deletions apps/envirogrowth/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ export default function middleware(req: NextRequest) {
const loggedin = req.cookies.get('authToken')
const { pathname } = req.nextUrl

const url = req.nextUrl
const urlObj = new URL(url)
const searchParams = urlObj.searchParams
const response = NextResponse.next()
response.headers.set('Cache-Control', 'no-store')

const searchParams = req.nextUrl.searchParams

const externalUrlParam = searchParams.get('external_url')

Expand All @@ -24,7 +25,7 @@ export default function middleware(req: NextRequest) {

// It's important to return a response for all paths, you might want to return `undefined` or `NextResponse.next()`
// for other cases to let the request continue.
return NextResponse.next()
return response
}

export const config = {
Expand Down
9 changes: 5 additions & 4 deletions apps/harmoni-aids/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ export default function middleware(req: NextRequest) {
const loggedin = req.cookies.get('authToken')
const { pathname } = req.nextUrl

const url = req.nextUrl
const urlObj = new URL(url)
const searchParams = urlObj.searchParams
const response = NextResponse.next()
response.headers.set('Cache-Control', 'no-store')

const searchParams = req.nextUrl.searchParams

const externalUrlParam = searchParams.get('external_url')

Expand All @@ -24,7 +25,7 @@ export default function middleware(req: NextRequest) {

// It's important to return a response for all paths, you might want to return `undefined` or `NextResponse.next()`
// for other cases to let the request continue.
return NextResponse.next()
return response
}

export const config = {
Expand Down
10 changes: 6 additions & 4 deletions apps/industry_4.0/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { NextRequest, NextResponse } from 'next/server'
export default function middleware(req: NextRequest) {
const loggedin = req.cookies.get('authToken')
const { pathname } = req.nextUrl
const url = req.nextUrl
const urlObj = new URL(url)
const searchParams = urlObj.searchParams

const response = NextResponse.next()
response.headers.set('Cache-Control', 'no-store')

const searchParams = req.nextUrl.searchParams

const externalUrlParam = searchParams.get('external_url')

Expand All @@ -23,7 +25,7 @@ export default function middleware(req: NextRequest) {

// It's important to return a response for all paths, you might want to return `undefined` or `NextResponse.next()`
// for other cases to let the request continue.
return NextResponse.next()
return response
}

export const config = {
Expand Down
9 changes: 5 additions & 4 deletions apps/odr-v2/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ export default function middleware(req: NextRequest) {
const loggedin = req.cookies.get('authToken')
const { pathname } = req.nextUrl

const url = req.nextUrl
const urlObj = new URL(url)
const searchParams = urlObj.searchParams
const response = NextResponse.next()
response.headers.set('Cache-Control', 'no-store')

const searchParams = req.nextUrl.searchParams

const externalUrlParam = searchParams.get('external_url')

Expand All @@ -24,7 +25,7 @@ export default function middleware(req: NextRequest) {

// It's important to return a response for all paths, you might want to return `undefined` or `NextResponse.next()`
// for other cases to let the request continue.
return NextResponse.next()
return response
}

export const config = {
Expand Down
4 changes: 3 additions & 1 deletion apps/open-spark/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export default function middleware(req: NextRequest) {
const loggedin = req.cookies.get('authToken')
const role = req.cookies.get('roleSelected')
const { pathname } = req.nextUrl
const response = NextResponse.next()
response.headers.set('Cache-Control', 'no-store')

if (role && loggedin && (pathname === '/signIn' || pathname === '/signUp' || pathname === '/welcome')) {
return NextResponse.redirect(new URL('/', req.url))
Expand All @@ -26,7 +28,7 @@ export default function middleware(req: NextRequest) {

// It's important to return a response for all paths, you might want to return `undefined` or `NextResponse.next()`
// for other cases to let the request continue.
return NextResponse.next()
return response
}

export const config = {
Expand Down
5 changes: 4 additions & 1 deletion apps/policy-admin/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export default function middleware(req: NextRequest) {
const loggedin = req.cookies.get('authToken')
const { pathname } = req.nextUrl

const response = NextResponse.next()
response.headers.set('Cache-Control', 'no-store')

// once API is integrated then we can add middleware
if (loggedin && (pathname === '/signIn' || pathname === '/signUp')) {
return NextResponse.redirect(new URL('/', req.url))
Expand All @@ -23,7 +26,7 @@ export default function middleware(req: NextRequest) {

// It's important to return a response for all paths, you might want to return `undefined` or `NextResponse.next()`
// for other cases to let the request continue.
return NextResponse.next()
return response
}

export const config = {
Expand Down
9 changes: 5 additions & 4 deletions apps/retail/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ export default function middleware(req: NextRequest) {
const loggedin = req.cookies.get('authToken')
const { pathname } = req.nextUrl

const url = req.nextUrl
const urlObj = new URL(url)
const searchParams = urlObj.searchParams
const response = NextResponse.next()
response.headers.set('Cache-Control', 'no-store')

const searchParams = req.nextUrl.searchParams

const externalUrlParam = searchParams.get('external_url')

Expand All @@ -24,7 +25,7 @@ export default function middleware(req: NextRequest) {

// It's important to return a response for all paths, you might want to return `undefined` or `NextResponse.next()`
// for other cases to let the request continue.
return NextResponse.next()
return response
}

export const config = {
Expand Down
9 changes: 5 additions & 4 deletions apps/sky-analytics/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ export default function middleware(req: NextRequest) {
const loggedin = req.cookies.get('authToken')
const { pathname } = req.nextUrl

const url = req.nextUrl
const urlObj = new URL(url)
const searchParams = urlObj.searchParams
const response = NextResponse.next()
response.headers.set('Cache-Control', 'no-store')

const searchParams = req.nextUrl.searchParams

const externalUrlParam = searchParams.get('external_url')

Expand All @@ -24,7 +25,7 @@ export default function middleware(req: NextRequest) {

// It's important to return a response for all paths, you might want to return `undefined` or `NextResponse.next()`
// for other cases to let the request continue.
return NextResponse.next()
return response
}

export const config = {
Expand Down
9 changes: 5 additions & 4 deletions apps/state-forest-department/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ export default function middleware(req: NextRequest) {
const loggedin = req.cookies.get('authToken')
const { pathname } = req.nextUrl

const url = req.nextUrl
const urlObj = new URL(url)
const searchParams = urlObj.searchParams
const response = NextResponse.next()
response.headers.set('Cache-Control', 'no-store')

const searchParams = req.nextUrl.searchParams

const externalUrlParam = searchParams.get('external_url')

Expand All @@ -24,7 +25,7 @@ export default function middleware(req: NextRequest) {

// It's important to return a response for all paths, you might want to return `undefined` or `NextResponse.next()`
// for other cases to let the request continue.
return NextResponse.next()
return response
}

export const config = {
Expand Down
5 changes: 4 additions & 1 deletion apps/taxi-bpp-v2/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export default function middleware(req: NextRequest) {
const loggedin = req.cookies.get('authToken')
const { pathname } = req.nextUrl

const response = NextResponse.next()
response.headers.set('Cache-Control', 'no-store')

if (loggedin && (pathname === '/signIn' || pathname === '/signUp')) {
return NextResponse.redirect(new URL('/', req.url))
}
Expand All @@ -18,7 +21,7 @@ export default function middleware(req: NextRequest) {

// It's important to return a response for all paths, you might want to return `undefined` or `NextResponse.next()`
// for other cases to let the request continue.
return NextResponse.next()
return response
}

export const config = {
Expand Down
2 changes: 2 additions & 0 deletions apps/tourismV1.1/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export default function middleware(req: NextRequest) {
const loggedin = req.cookies.get('authToken')
const { pathname, href, host, searchParams } = req.nextUrl
const response = NextResponse.next()
response.headers.set('Cache-Control', 'no-store')

// const tourismType = req.cookies.get('tourismType')?.value || ''
const queryTourismType = searchParams.get('tourismType')
const tourismType = queryTourismType
Expand Down