diff --git a/src/components/Tilgangskontrollside/Tilgangskontrollside.tsx b/src/components/Tilgangskontrollside/Tilgangskontrollside.tsx index 2e5c528a..dfd06092 100644 --- a/src/components/Tilgangskontrollside/Tilgangskontrollside.tsx +++ b/src/components/Tilgangskontrollside/Tilgangskontrollside.tsx @@ -47,18 +47,9 @@ const Tilgangskontrollside: React.FC = ({children, qu // const {data, isLoading, error} = useHarTilgang(); const router = useRouter(); const {t} = useTranslation(); - const sessionQuery = useDekoratorLogin(); - const { - error, - isPending, - data: harTilgangData, - } = useHarTilgang({query: {enabled: sessionQuery.status === "success"}}); + const {error, isPending, data: harTilgangData} = useHarTilgang(); - console.log("sessionQuery.isLoading: ", sessionQuery.isPending); - console.log("sessionQuery.error: ", sessionQuery.error); - console.log("harTilgang.error: ", error); - console.log("harTilgang.isLoading", isPending); - console.log("harTilgang.data", harTilgangData); + const sessionQuery = useDekoratorLogin(); useEffect(() => { if ( !sessionQuery.isPending && @@ -83,7 +74,8 @@ const Tilgangskontrollside: React.FC = ({children, qu ); } - if (!harTilgangData?.data.harTilgang || queryHas403) { + const isAuthError = harTilgangData?.status === 401 || harTilgangData?.status === 403; + if (isAuthError || queryHas403 || (harTilgangData && harTilgangData.data.harTilgang)) { const fornavn = harTilgangData?.data.fornavn ?? ""; fornavn === "" ? logger.warn(`Viser tilgangskontrollside uten fornavn`) diff --git a/src/middleware.ts b/src/middleware.ts index 0d1238d7..3228818e 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -13,32 +13,26 @@ interface AzureAdAuthenticationError { export async function middleware(request: NextRequest) { const pathname = request.nextUrl.pathname; - if (!pathname.includes("api/internal")) { - console.log("Detta er middleware"); - console.log("hva skjer hera ", pathname); - console.log("her ere no cookies: ", request.cookies); - console.log("Ere no auth header a?", request.headers.get("Authorization")); - } // Ikke gjør noe med requests til /api eller statiske filer if (pathname.startsWith("/_next") || pathname.includes("/api") || PUBLIC_FILE.test(pathname)) { return; } // Sett språk basert på decorator-language cookien - // const decoratorLocale = request.cookies.get("decorator-language")?.value ?? "nb"; - // if (decoratorLocale !== request.nextUrl.locale) { - // if (request.nextUrl.locale !== "nb") { - // const next = NextResponse.next(); - // next.cookies.set("decorator-language", request.nextUrl.locale); - // return next; - // } - // const url = new URL( - // `${process.env.NEXT_PUBLIC_INNSYN_ORIGIN}${process.env.NEXT_PUBLIC_BASE_PATH}/${ - // decoratorLocale === "nb" ? "" : decoratorLocale - // }${pathname.replace("/sosialhjelp/innsyn", "")}` - // ); - // return NextResponse.redirect(url); - // } + const decoratorLocale = request.cookies.get("decorator-language")?.value ?? "nb"; + if (decoratorLocale !== request.nextUrl.locale) { + if (request.nextUrl.locale !== "nb") { + const next = NextResponse.next(); + next.cookies.set("decorator-language", request.nextUrl.locale); + return next; + } + const url = new URL( + `${process.env.NEXT_PUBLIC_INNSYN_ORIGIN}${process.env.NEXT_PUBLIC_BASE_PATH}/${ + decoratorLocale === "nb" ? "" : decoratorLocale + }${pathname.replace("/sosialhjelp/innsyn", "")}` + ); + return NextResponse.redirect(url); + } // Router bruker til login hvis vi får 401. Dette gjelder bare for auth gjennom mock-alt. I prod/preprod gjelder dette for "vanlig" innlogging på login.nav.no if (["mock", "local"].includes(process.env.NEXT_PUBLIC_RUNTIME_ENVIRONMENT!)) {