feat(SW-158): Updated to use custom cookie and optimization
This commit is contained in:
@@ -16,7 +16,13 @@ export async function GET(
|
|||||||
|
|
||||||
const returnUrl = request.headers.get("x-returnurl")
|
const returnUrl = request.headers.get("x-returnurl")
|
||||||
const isMFA = request.headers.get("x-mfa-login")
|
const isMFA = request.headers.get("x-mfa-login")
|
||||||
const isMagicLinkLogin = !!request.headers.get("x-magic-link")
|
|
||||||
|
// This is to support seamless login when using magic link login
|
||||||
|
const isMagicLinkUpdateLogin = !!request.headers.get("x-magic-link")
|
||||||
|
|
||||||
|
if (!env.PUBLIC_URL) {
|
||||||
|
throw internalServerError("No value for env.PUBLIC_URL")
|
||||||
|
}
|
||||||
|
|
||||||
if (returnUrl) {
|
if (returnUrl) {
|
||||||
// Seamless login request from Current web
|
// Seamless login request from Current web
|
||||||
@@ -30,9 +36,6 @@ export async function GET(
|
|||||||
|
|
||||||
// Make relative URL to absolute URL
|
// Make relative URL to absolute URL
|
||||||
if (redirectTo.startsWith("/")) {
|
if (redirectTo.startsWith("/")) {
|
||||||
if (!env.PUBLIC_URL) {
|
|
||||||
throw internalServerError("No value for env.PUBLIC_URL")
|
|
||||||
}
|
|
||||||
redirectTo = new URL(redirectTo, env.PUBLIC_URL).href
|
redirectTo = new URL(redirectTo, env.PUBLIC_URL).href
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,6 +72,14 @@ export async function GET(
|
|||||||
const redirectUrl = new URL(redirectUrlValue)
|
const redirectUrl = new URL(redirectUrlValue)
|
||||||
redirectUrl.searchParams.set("returnurl", redirectTo)
|
redirectUrl.searchParams.set("returnurl", redirectTo)
|
||||||
redirectTo = redirectUrl.toString()
|
redirectTo = redirectUrl.toString()
|
||||||
|
|
||||||
|
/** Set cookie with redirect Url to appropriately redirect user when using magic link login */
|
||||||
|
redirectHeaders.append(
|
||||||
|
"set-cookie",
|
||||||
|
"magicLinkRedirectTo=" +
|
||||||
|
redirectTo +
|
||||||
|
"; Max-Age=300; Path=/; HttpOnly; SameSite=Lax"
|
||||||
|
)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(
|
console.error(
|
||||||
"Unable to create URL for seamless login, proceeding without it."
|
"Unable to create URL for seamless login, proceeding without it."
|
||||||
@@ -96,26 +107,26 @@ export async function GET(
|
|||||||
* to the user which we do not want.
|
* to the user which we do not want.
|
||||||
*/
|
*/
|
||||||
acr_values: "acr",
|
acr_values: "acr",
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The `for_origin` param is used to make Curity email login functionality working.
|
* Both of the below two params are required to send for initiating login as well
|
||||||
* Without the parameter Curity gives Internal Error issue for login with Email link.
|
* because user might choose to do Email link login.
|
||||||
*/
|
* */
|
||||||
for_origin: env.PUBLIC_URL ? env.PUBLIC_URL : "",
|
// The `for_origin` param is used to make Curity email login functionality working.
|
||||||
/**
|
for_origin: env.PUBLIC_URL,
|
||||||
* This is new param set for differentiate between
|
// This is new param set for differentiate between the Magic link login of New web and current web
|
||||||
* the Magic link login of New web and current web
|
|
||||||
*/
|
|
||||||
version: "2",
|
version: "2",
|
||||||
}
|
}
|
||||||
if (isMFA) {
|
if (isMFA) {
|
||||||
params.scope = ["profile_update", "openid", "profile"].join(" ")
|
// Append profile_update scope for MFA
|
||||||
|
params.scope = params.scope + " profile_udpate"
|
||||||
/**
|
/**
|
||||||
* The below acr value is required as for New Web same Curity Client is used for MFA
|
* The below acr value is required as for New Web same Curity Client is used for MFA
|
||||||
* while in current web it is being setup using different Curity Client
|
* while in current web it is being setup using different Curity Client
|
||||||
*/
|
*/
|
||||||
params.acr_values =
|
params.acr_values =
|
||||||
"urn:se:curity:authentication:otp-authenticator:OTP-Authenticator_web"
|
"urn:se:curity:authentication:otp-authenticator:OTP-Authenticator_web"
|
||||||
} else if (isMagicLinkLogin) {
|
} else if (isMagicLinkUpdateLogin) {
|
||||||
params.acr_values = "abc"
|
params.acr_values = "abc"
|
||||||
}
|
}
|
||||||
const redirectUrl = await signIn(
|
const redirectUrl = await signIn(
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { AuthError } from "next-auth"
|
|||||||
import { Lang } from "@/constants/languages"
|
import { Lang } from "@/constants/languages"
|
||||||
import { login } from "@/constants/routes/handleAuth"
|
import { login } from "@/constants/routes/handleAuth"
|
||||||
import { env } from "@/env/server"
|
import { env } from "@/env/server"
|
||||||
import { internalServerError } from "@/server/errors/next"
|
import { badRequest, internalServerError } from "@/server/errors/next"
|
||||||
|
|
||||||
import { signIn } from "@/auth"
|
import { signIn } from "@/auth"
|
||||||
|
|
||||||
@@ -14,16 +14,17 @@ export async function GET(
|
|||||||
) {
|
) {
|
||||||
let redirectTo: string
|
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 =
|
redirectTo =
|
||||||
request.cookies.get("Scandic-auth.callback-url")?.value ||
|
request.cookies.get("magicLinkRedirectTo")?.value ||
|
||||||
"/" + context.params.lang
|
"/" + context.params.lang
|
||||||
|
|
||||||
|
if (!env.PUBLIC_URL) {
|
||||||
|
throw internalServerError("No value for env.PUBLIC_URL")
|
||||||
|
}
|
||||||
|
|
||||||
// Make relative URL to absolute URL
|
// Make relative URL to absolute URL
|
||||||
if (redirectTo.startsWith("/")) {
|
if (redirectTo.startsWith("/")) {
|
||||||
if (!env.PUBLIC_URL) {
|
|
||||||
throw internalServerError("No value for env.PUBLIC_URL")
|
|
||||||
}
|
|
||||||
redirectTo = new URL(redirectTo, env.PUBLIC_URL).href
|
redirectTo = new URL(redirectTo, env.PUBLIC_URL).href
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,12 +34,7 @@ export async function GET(
|
|||||||
const loginKey = request.nextUrl.searchParams.get("loginKey")
|
const loginKey = request.nextUrl.searchParams.get("loginKey")
|
||||||
|
|
||||||
if (!loginKey) {
|
if (!loginKey) {
|
||||||
if (!env.PUBLIC_URL) {
|
return badRequest()
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -58,7 +54,7 @@ export async function GET(
|
|||||||
ui_locales: context.params.lang,
|
ui_locales: context.params.lang,
|
||||||
scope: ["openid", "profile"].join(" "),
|
scope: ["openid", "profile"].join(" "),
|
||||||
loginKey: loginKey,
|
loginKey: loginKey,
|
||||||
for_origin: env.PUBLIC_URL ? env.PUBLIC_URL : "",
|
for_origin: env.PUBLIC_URL,
|
||||||
acr_values: "abc",
|
acr_values: "abc",
|
||||||
version: "2",
|
version: "2",
|
||||||
}
|
}
|
||||||
|
|||||||
13
auth.ts
13
auth.ts
@@ -191,19 +191,6 @@ export const config = {
|
|||||||
return token
|
return token
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
cookies: {
|
|
||||||
// Specific cookie name required to reset callback url when login using
|
|
||||||
// Email verification link (Magic link) scenario
|
|
||||||
callbackUrl: {
|
|
||||||
name: `Scandic-auth.callback-url`,
|
|
||||||
options: {
|
|
||||||
sameSite: "lax",
|
|
||||||
path: "/",
|
|
||||||
secure: true,
|
|
||||||
maxAge: 900,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
// events: {
|
// events: {
|
||||||
// async signIn() {
|
// async signIn() {
|
||||||
// console.log("#### SIGNIN EVENT ARGS ######")
|
// console.log("#### SIGNIN EVENT ARGS ######")
|
||||||
|
|||||||
Reference in New Issue
Block a user