Feature/wrap logging * feat: change all logging to go through our own logger function so that we can control log levels * move packages/trpc to using our own logger * merge Approved-by: Linus Flood
48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import { NextResponse } from "next/server"
|
|
import { AuthError } from "next-auth"
|
|
|
|
import { logger } from "@scandic-hotels/common/logger"
|
|
|
|
import { dtmcApiCallback } from "@/constants/routes/dtmc"
|
|
import { env } from "@/env/server"
|
|
import { internalServerError, serviceUnavailable } from "@/server/errors/next"
|
|
|
|
import { signIn } from "@/auth.dtmc"
|
|
|
|
export async function GET() {
|
|
try {
|
|
const redirectUrl = await signIn(
|
|
"microsoft-entra-id",
|
|
{
|
|
redirectTo: `${env.PUBLIC_URL}${dtmcApiCallback}`,
|
|
redirect: false,
|
|
},
|
|
{
|
|
prompt: "login",
|
|
}
|
|
)
|
|
|
|
if (redirectUrl) {
|
|
logger.debug(`[dtmc] redirecting to: ${redirectUrl}`)
|
|
return NextResponse.redirect(redirectUrl)
|
|
} else {
|
|
logger.error(`[dtmc] missing redirectUrl response from signIn()`)
|
|
return internalServerError(
|
|
"[dtmc] Missing redirect URL from authentication service"
|
|
)
|
|
}
|
|
} catch (error) {
|
|
if (error instanceof AuthError) {
|
|
logger.error("signInAuthError", { signInAuthError: error })
|
|
return serviceUnavailable(
|
|
"[dtmc] Microsoft authentication service unavailable"
|
|
)
|
|
} else {
|
|
logger.error("signInError", { signInError: error })
|
|
return internalServerError(
|
|
"[dtmc] Unexpected error during authentication"
|
|
)
|
|
}
|
|
}
|
|
}
|