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

@@ -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()
}