Skip to content

Commit

Permalink
Gjeninnfører språk-redirect og bare vis tilgangskontrollside når 40x
Browse files Browse the repository at this point in the history
  • Loading branch information
almyy committed Dec 10, 2024
1 parent 78db14e commit d7bb6a6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 33 deletions.
18 changes: 5 additions & 13 deletions src/components/Tilgangskontrollside/Tilgangskontrollside.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,9 @@ const Tilgangskontrollside: React.FC<TilgangskontrollsideProps> = ({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 &&
Expand All @@ -83,8 +74,9 @@ const Tilgangskontrollside: React.FC<TilgangskontrollsideProps> = ({children, qu
);
}

if (!harTilgangData?.data.harTilgang || queryHas403) {
const fornavn = harTilgangData?.data.fornavn ?? "";
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`)
: logger.warn(`Viser tilgangskontrollside med fornavn`);
Expand Down
34 changes: 14 additions & 20 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!)) {
Expand Down

0 comments on commit d7bb6a6

Please sign in to comment.