feat(SW-158): Updated to use custom cookie and optimization

This commit is contained in:
Hrishikesh Vaipurkar
2024-08-19 11:54:49 +02:00
parent fd4c238428
commit 01bd7d2299
3 changed files with 34 additions and 40 deletions

View File

@@ -4,7 +4,7 @@ import { AuthError } from "next-auth"
import { Lang } from "@/constants/languages"
import { login } from "@/constants/routes/handleAuth"
import { env } from "@/env/server"
import { internalServerError } from "@/server/errors/next"
import { badRequest, internalServerError } from "@/server/errors/next"
import { signIn } from "@/auth"
@@ -14,16 +14,17 @@ export async function GET(
) {
let redirectTo: string
// Set callback from Cookie set by NextAuth when intiating login
// Set redirect url from the magicLinkRedirect Cookie which is set when intiating login
redirectTo =
request.cookies.get("Scandic-auth.callback-url")?.value ||
request.cookies.get("magicLinkRedirectTo")?.value ||
"/" + context.params.lang
if (!env.PUBLIC_URL) {
throw internalServerError("No value for env.PUBLIC_URL")
}
// Make relative URL to absolute URL
if (redirectTo.startsWith("/")) {
if (!env.PUBLIC_URL) {
throw internalServerError("No value for env.PUBLIC_URL")
}
redirectTo = new URL(redirectTo, env.PUBLIC_URL).href
}
@@ -33,12 +34,7 @@ export async function GET(
const loginKey = request.nextUrl.searchParams.get("loginKey")
if (!loginKey) {
if (!env.PUBLIC_URL) {
throw internalServerError("No value for env.PUBLIC_URL")
}
const publicUrl = new URL(env.PUBLIC_URL)
const loginUrl = login[context.params.lang]
return NextResponse.redirect(new URL(loginUrl, publicUrl))
return badRequest()
}
try {
@@ -58,7 +54,7 @@ export async function GET(
ui_locales: context.params.lang,
scope: ["openid", "profile"].join(" "),
loginKey: loginKey,
for_origin: env.PUBLIC_URL ? env.PUBLIC_URL : "",
for_origin: env.PUBLIC_URL,
acr_values: "abc",
version: "2",
}