feat(WEB-132): add middlewares, support for seamless login and improve lang based routes

This commit is contained in:
Michael Zetterberg
2024-04-08 16:08:35 +02:00
parent 8ab5325fc3
commit 7093a0b2dd
31 changed files with 493 additions and 188 deletions

View File

@@ -1,6 +1,4 @@
import { redirect } from "next/navigation"
import { auth } from "@/auth"
import { auth, signIn } from "@/auth"
import type { LangParams, LayoutArgs } from "@/types/params"
@@ -14,7 +12,10 @@ export default async function ProtectedLayout({
* protected route group is actually protected.
*/
if (!session) {
return redirect(`/${params.lang}/login`)
await signIn("curity", undefined, {
ui_locales: params.lang,
})
return null
}
return <>{children}</>

View File

@@ -1,21 +0,0 @@
import { signIn } from "@/auth"
import { pageNames } from "@/constants/myPages"
import type { LangParams, Params } from "@/types/params"
export default async function Page({ params }: Params<LangParams>) {
async function login() {
"use server"
await signIn("curity", {
redirectTo: `/${params.lang}/${pageNames[params.lang]}`,
})
}
return (
<main>
<form action={login}>
<button type="submit">Sign In</button>
</form>
</main>
)
}

View File

@@ -0,0 +1,47 @@
import { NextRequest, NextResponse } from "next/server"
import { AuthError } from "next-auth"
import { signIn } from "@/auth"
import { badRequest } from "@/server/errors/next"
import type { Lang } from "@/constants/languages"
export async function GET(
request: NextRequest,
context: { params: { lang: Lang } }
) {
const redirectTo =
request.headers.get("x-redirect-to") ||
request.nextUrl.searchParams.get("redirectTo") ||
undefined
try {
/**
* Passing `redirect: false` to `signIn` will return the URL instead of
* automatically redirecting to it inside of `signIn`.
* https://github.com/nextauthjs/next-auth/blob/3c035ec/packages/next-auth/src/lib/actions.ts#L76
*/
const url = await signIn(
"curity",
{
redirectTo,
redirect: false,
},
{
ui_locales: context.params.lang,
}
)
if (url) {
return NextResponse.redirect(url)
}
} catch (error) {
if (error instanceof AuthError) {
console.log({ signInAuthError: error })
} else {
console.log({ signInError: error })
}
}
return badRequest()
}

View File

@@ -8,7 +8,6 @@ import SkipToMainContent from "@/components/SkipToMainContent"
import type { Metadata } from "next"
import type { LangParams, LayoutArgs } from "@/types/params"
import VwoScript from "@/components/Current/NewVWOScript"
export const fetchCache = "default-no-store"
@@ -40,10 +39,6 @@ export default function RootLayout({
id="Cookiebot"
src="https://consent.cookiebot.com/uc.js"
/>
{/* <Script
data-cookieconsent="ignore"
src="/_static/dist/js/head.js?85c84c9e24ae8da3e7af"
/> */}
<Script
data-cookieconsent="ignore"
src="/_static/dist/js/inline.js?00133e5a37de35c51a5d"
@@ -64,11 +59,6 @@ export default function RootLayout({
data-cookieconsent="ignore"
src="/_static/dist/js/ng/main.js?1705409330990"
/>
{/* <Script
data-cookieconsent="ignore"
src="/_static/dist/js/main-ng.js?336b801d6b38eff10884"
strategy="lazyOnload"
/> */}
<Script id="ensure-datalayer">{`
window.datalayer = window.datalayer || {}
`}</Script>

View File

@@ -0,0 +1,17 @@
import type { Metadata } from "next"
import type { LangParams, LayoutArgs } from "@/types/params"
export const metadata: Metadata = {
title: "Webview",
}
export default function RootLayout({
children,
params,
}: React.PropsWithChildren<LayoutArgs<LangParams>>) {
return (
<html lang={params.lang}>
<body>{children}</body>
</html>
)
}

View File

@@ -0,0 +1,15 @@
import type { Metadata } from "next"
export const metadata: Metadata = {
title: "Hello World from Webview",
}
export default function WebViewTestPage() {
return (
<main>
<header>
<h1>Hello From WebView Test Page!</h1>
</header>
</main>
)
}