Skip to content

Commit

Permalink
Refakturerer sessjonsmodalen
Browse files Browse the repository at this point in the history
  • Loading branch information
nattaphongklinjan committed Nov 1, 2023
1 parent 8f4aeb2 commit f98dd03
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
21 changes: 3 additions & 18 deletions app/components/session-modal/SessjonModal.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import type { SessionWithOboProvider } from "@navikt/dp-auth";
import { Button, Heading, Modal } from "@navikt/ds-react";
import classNames from "classnames";
import { useEffect, useState } from "react";
import { useTypedRouteLoaderData } from "~/hooks/useTypedRouteLoaderData";
import styles from "./SessjonModal.module.css";
import { DevelopmentContainer } from "../development-container/DevelopmentContainer";
import { getEnv } from "~/utils/env.utils";
import styles from "./SessjonModal.module.css";

interface IProps {
sesjon?: SessionWithOboProvider;
}

export function SessjonModal(props: IProps) {
export function SessjonModal() {
const { session } = useTypedRouteLoaderData("routes/rapportering");
const [utlopesOm, setUtlopesOm] = useState<number | undefined>(
props.sesjon?.expiresIn || session?.expiresIn || 1
session?.expiresIn || session?.expiresIn || 1
);
const [utlopt, setUtlopt] = useState(false);
const [laster, setLaster] = useState(false);
Expand Down Expand Up @@ -64,15 +58,6 @@ export function SessjonModal(props: IProps) {
Gå til forsiden
</Button>
</div>
<DevelopmentContainer>
<a
target="_blank"
rel="noreferrer"
href="https://wonderwalled-idporten.intern.dev.nav.no/api/obo?aud=dev-gcp:teamdagpenger:dp-rapportering"
>
Klikk på lenken for å hente ny token
</a>
</DevelopmentContainer>
</Modal.Body>
</Modal>
);
Expand Down
17 changes: 15 additions & 2 deletions app/routes/rapportering.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { Heading } from "@navikt/ds-react";
import type { LoaderFunctionArgs } from "@remix-run/node";
import { json } from "@remix-run/node";
import { isRouteErrorResponse, Outlet, useRouteError } from "@remix-run/react";
import { DevelopmentContainer } from "~/components/development-container/DevelopmentContainer";
import { SessjonModal } from "~/components/session-modal/SessjonModal";
import { getSession } from "~/utils/auth.utils.server";
import { getEnv } from "~/utils/env.utils";

export async function loader({ request }: LoaderFunctionArgs) {
const session = await getSession(request);
Expand All @@ -24,7 +26,7 @@ export function ErrorBoundary() {
const error = useRouteError();

if (isRouteErrorResponse(error)) {
if (error.status === 401) {
if (getEnv("IS_LOCALHOST") === "true" && error.status === 440) {
return (
<main>
<div className="rapportering-header">
Expand All @@ -35,7 +37,18 @@ export function ErrorBoundary() {
</div>
</div>
<div className="rapportering-container">
<SessjonModal sesjon={error.data.session} />
<DevelopmentContainer>
<>
Sessjon utløpt! &nbsp;
<a
target="_blank"
rel="noreferrer"
href="https://tokenx-token-generator.intern.dev.nav.no/api/obo?aud=dev-gcp:teamdagpenger:dp-rapportering"
>
Klikk på lenken for å hente ny token
</a>
</>
</DevelopmentContainer>
</div>
</main>
);
Expand Down
15 changes: 12 additions & 3 deletions app/utils/auth.utils.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@ export async function getRapporteringOboToken(request: Request) {
throw new Response(null, { status: 500, statusText: "Feil ved henting av sesjon" });
}

const utloptLocalhostSessjon = process.env.IS_LOCALHOST === "true" && session.expiresIn === 0;

if (utloptLocalhostSessjon && process.env.USE_MSW !== "true") {
throw new Response(null, {
status: 440,
statusText: "Localhost sessjon er utløpt!",
});
}

if (process.env.IS_LOCALHOST === "true") {
return process.env.DP_RAPPORTERING_TOKEN || fallbackToken;
} else {
const audienceDPRapportering = `${process.env.NAIS_CLUSTER_NAME}:teamdagpenger:dp-rapportering`;
return session.apiToken(audienceDPRapportering);
}

const audienceDPRapportering = `${process.env.NAIS_CLUSTER_NAME}:teamdagpenger:dp-rapportering`;
return session.apiToken(audienceDPRapportering);
}

0 comments on commit f98dd03

Please sign in to comment.