48 lines
1.1 KiB
TypeScript
48 lines
1.1 KiB
TypeScript
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()
|
|
}
|