fix the booking and contract
Build & Push / Pipeline Tests (push) Failing after 1m31s
Build & Push / Build & Push Docker Image (push) Has been skipped
Test / Type Check (all packages) (push) Successful in 55s
Test / API Unit Tests (push) Successful in 1m8s
Test / Homepage Unit Tests (push) Successful in 47s
Test / Carplace Unit Tests (push) Successful in 44s
Test / Admin Unit Tests (push) Successful in 43s
Test / Dashboard Unit Tests (push) Failing after 46s
Test / API Integration Tests (push) Successful in 1m7s
Build & Push / Pipeline Tests (push) Failing after 1m31s
Build & Push / Build & Push Docker Image (push) Has been skipped
Test / Type Check (all packages) (push) Successful in 55s
Test / API Unit Tests (push) Successful in 1m8s
Test / Homepage Unit Tests (push) Successful in 47s
Test / Carplace Unit Tests (push) Successful in 44s
Test / Admin Unit Tests (push) Successful in 43s
Test / Dashboard Unit Tests (push) Failing after 46s
Test / API Integration Tests (push) Successful in 1m7s
This commit is contained in:
@@ -4,6 +4,8 @@ import type { NextRequest } from 'next/server'
|
||||
const WEBSITE_URL = process.env.NEXT_PUBLIC_WEBSITE_URL ?? 'http://localhost:3000'
|
||||
const DASHBOARD_PUBLIC_URL = process.env.NEXT_PUBLIC_DASHBOARD_URL ?? `${WEBSITE_URL.replace(/\/$/, '')}/dashboard`
|
||||
const DASHBOARD_BASE_PATH = '/dashboard'
|
||||
const DEFAULT_SIGN_IN_LOCALE = 'en'
|
||||
const DEFAULT_SIGN_IN_THEME = 'light'
|
||||
|
||||
function toDashboardAppPath(pathname: string): string {
|
||||
let normalized = pathname || '/'
|
||||
@@ -20,6 +22,33 @@ function toPublicDashboardPath(pathname: string): string {
|
||||
return appPath === '/' ? DASHBOARD_BASE_PATH : `${DASHBOARD_BASE_PATH}${appPath}`
|
||||
}
|
||||
|
||||
function resolveSignInPreference(value: string | undefined, allowed: string[], fallback: string) {
|
||||
return value && allowed.includes(value) ? value : fallback
|
||||
}
|
||||
|
||||
function resolveHomepageSignInUrl(req: NextRequest, redirectPath?: string): URL {
|
||||
const locale = resolveSignInPreference(
|
||||
req.nextUrl.searchParams.get('lang') ?? req.cookies.get('rentaldrivego-language')?.value,
|
||||
['en', 'fr', 'ar'],
|
||||
DEFAULT_SIGN_IN_LOCALE,
|
||||
)
|
||||
const theme = resolveSignInPreference(
|
||||
req.nextUrl.searchParams.get('theme') ?? req.cookies.get('rentaldrivego-theme')?.value ?? req.cookies.get('hpc-theme')?.value,
|
||||
['light', 'dark'],
|
||||
DEFAULT_SIGN_IN_THEME,
|
||||
)
|
||||
const signInUrl = new URL(`/${locale}/${theme}/sign-in`, new URL(WEBSITE_URL).origin)
|
||||
|
||||
if (redirectPath) signInUrl.searchParams.set('redirect', toPublicDashboardPath(redirectPath))
|
||||
|
||||
for (const key of ['portal', 'embedded']) {
|
||||
const value = req.nextUrl.searchParams.get(key)
|
||||
if (value) signInUrl.searchParams.set(key, value)
|
||||
}
|
||||
|
||||
return signInUrl
|
||||
}
|
||||
|
||||
function deduplicatePublicDashboardPath(pathname: string): string | null {
|
||||
if (!pathname.startsWith(`${DASHBOARD_BASE_PATH}${DASHBOARD_BASE_PATH}`)) return null
|
||||
|
||||
@@ -63,18 +92,19 @@ function localJwtMiddleware(req: NextRequest): NextResponse {
|
||||
const token = req.cookies.get('employee_session')?.value
|
||||
const pathname = toDashboardAppPath(req.nextUrl.pathname)
|
||||
|
||||
// Redirect signed-in users from sign-in to dashboard (through the website proxy)
|
||||
if (token && pathname === '/sign-in') {
|
||||
const dashboardUrl = resolveProxyUrl(req, DASHBOARD_BASE_PATH)
|
||||
return NextResponse.redirect(dashboardUrl)
|
||||
if (pathname === '/sign-in') {
|
||||
if (token) {
|
||||
const dashboardUrl = resolveProxyUrl(req, DASHBOARD_BASE_PATH)
|
||||
return NextResponse.redirect(dashboardUrl)
|
||||
}
|
||||
|
||||
return NextResponse.redirect(resolveHomepageSignInUrl(req, req.nextUrl.searchParams.get('redirect') ?? undefined))
|
||||
}
|
||||
|
||||
if (!isProtectedRoute(req)) return NextResponse.next()
|
||||
|
||||
if (!token) {
|
||||
const signInUrl = resolveProxyUrl(req, toPublicDashboardPath('/sign-in'))
|
||||
signInUrl.searchParams.set('redirect', toPublicDashboardPath(req.nextUrl.pathname))
|
||||
return NextResponse.redirect(signInUrl)
|
||||
return NextResponse.redirect(resolveHomepageSignInUrl(req, req.nextUrl.pathname))
|
||||
}
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user