Skip to content

Commit

Permalink
🚚 Flyttet auth til getserversideprops
Browse files Browse the repository at this point in the history
  • Loading branch information
KenAJoh committed Feb 22, 2024
1 parent 5e37b97 commit 89660fb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 109 deletions.
56 changes: 0 additions & 56 deletions aksel.nav.no/website/components/auth/AuthProvider.tsx

This file was deleted.

19 changes: 19 additions & 0 deletions aksel.nav.no/website/components/auth/useAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useRouter } from "next/router";

export const useAuth = () => {
const router = useRouter();

const login = async () => {
router.push(`/oauth2/login?redirect=${router.asPath}`, undefined, {
shallow: true,
});
};

const logout = async () => {
router.push(`/oauth2/logout?redirect=${router.asPath}`, undefined, {
shallow: true,
});
};

return { login, logout };
};
7 changes: 2 additions & 5 deletions aksel.nav.no/website/components/auth/validate-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ import { logger } from "../../config/logger";
* Validates the wonderwall token according to nais.io. Should only actually redirect if the token has expired.
*/
export async function validateWonderwallToken(
req: NextApiRequest,
headers: NextApiRequest["headers"],
): Promise<boolean> {
const requestHeaders = req.headers;

/* if (isLocal) {
logger.warn('Is running locally, skipping RSC auth')
return
} */

const bearerToken: string | null | undefined =
requestHeaders["authorization"];
const bearerToken: string | null | undefined = headers["authorization"];

console.log("token: ", bearerToken);
if (!bearerToken) {
Expand Down
19 changes: 0 additions & 19 deletions aksel.nav.no/website/pages/api/auth/v1/validate-user.ts

This file was deleted.

43 changes: 14 additions & 29 deletions aksel.nav.no/website/pages/god-praksis/artikler/[slug].tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import NextLink from "next/link";
import { GetStaticPaths, GetStaticProps } from "next/types";
import { GetServerSideProps } from "next/types";
import { Suspense, lazy } from "react";
import { ChevronRightIcon } from "@navikt/aksel-icons";
import {
Expand All @@ -15,7 +15,6 @@ import Footer from "@/layout/footer/Footer";
import Header from "@/layout/header/Header";
import { SanityBlockContent } from "@/sanity-block";
import { getClient } from "@/sanity/client.server";
import { getDocuments } from "@/sanity/interface";
import {
contributorsAll,
contributorsSingle,
Expand All @@ -35,7 +34,8 @@ import { BreadCrumbs } from "@/web/BreadCrumbs";
import { SEO } from "@/web/seo/SEO";
import TableOfContents from "@/web/toc/TableOfContents";
import NotFotfund from "../../404";
import { useAuth } from "../../../components/auth/AuthProvider";
import { useAuth } from "../../../components/auth/useAuth";
import { validateWonderwallToken } from "../../../components/auth/validate-auth";

type PageProps = NextPageT<{
page: ResolveContributorsT<
Expand All @@ -44,6 +44,7 @@ type PageProps = NextPageT<{
publishDate: string;
verifiedDate: string;
toc: TableOfContentsT;
signedIn: boolean;
}>;

export const query = `{
Expand Down Expand Up @@ -72,26 +73,12 @@ export const query = `{
}
}`;

export const getStaticPaths: GetStaticPaths = async () => {
return {
paths: await getDocuments("aksel_artikkel").then((paths) =>
paths.map(({ slug }) => ({
params: {
slug: slug.replace("god-praksis/artikler/", ""),
},
})),
),
fallback: "blocking",
};
};
export const getServerSideProps: GetServerSideProps = async (
context,
): Promise<PageProps> => {
const signedIn = await validateWonderwallToken(context.req.headers);

export const getStaticProps: GetStaticProps = async ({
params: { slug },
preview = false,
}: {
params: { slug: string };
preview?: boolean;
}): Promise<PageProps> => {
const slug = context.params.slug as string;
const { page } = await getClient().fetch(query, {
slug: `god-praksis/artikler/${slug}`,
});
Expand All @@ -100,7 +87,7 @@ export const getStaticProps: GetStaticProps = async ({
props: {
page,
slug,
preview,
preview: context.preview ?? false,
id: page?._id ?? "",
title: page?.heading ?? "",
verifiedDate: await dateStr(
Expand All @@ -111,8 +98,9 @@ export const getStaticProps: GetStaticProps = async ({
content: page?.content,
type: "aksel_artikkel",
}),
signedIn,
},
notFound: !page && !preview,
notFound: !page && !(context.preview ?? false),
revalidate: 60,
};
};
Expand All @@ -122,9 +110,9 @@ const Page = ({
publishDate,
verifiedDate,
toc,
signedIn,
}: PageProps["props"]) => {
const auth = useAuth();

if (!data) {
return <NotFotfund />;
}
Expand Down Expand Up @@ -190,10 +178,7 @@ const Page = ({
id="hovedinnhold"
className="aksel-artikkel group/aksel bg-surface-subtle pt-4 focus:outline-none"
>
<div>{`Loading: ${auth.loading}`}</div>
{!auth.loading && (
<div>{`Is authenticated: ${auth.isAuthenticated}`}</div>
)}
<div>{`Is authenticated: ${signedIn}`}</div>
<div>
<Button variant="secondary" onClick={auth.login}>
Login
Expand Down

0 comments on commit 89660fb

Please sign in to comment.