Merged in feat/sw-3192-no-user (pull request #2680)
feat(SW-3192): Checks if user exists, otherwise logout and show error * feat(SW-3192): Checks if user exists, otherwise logout and show error
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { type NextRequest,NextResponse } from "next/server"
|
||||
import { AuthError } from "next-auth"
|
||||
|
||||
import { logger } from "@scandic-hotels/common/logger"
|
||||
|
||||
import { env } from "@/env/server"
|
||||
import { internalServerError } from "@/server/errors/next"
|
||||
import { getPublicURL } from "@/server/utils"
|
||||
|
||||
import { signOut } from "@/auth"
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const publicURL = getPublicURL(request)
|
||||
const redirectToSearchParamValue =
|
||||
request.nextUrl.searchParams.get("redirectTo")
|
||||
const redirectToFallback = "/"
|
||||
|
||||
let redirectTo: string = redirectToSearchParamValue || redirectToFallback
|
||||
|
||||
// Make relative URL to absolute URL
|
||||
if (redirectTo.startsWith("/")) {
|
||||
redirectTo = new URL(redirectTo, publicURL).href
|
||||
}
|
||||
|
||||
try {
|
||||
redirectTo = `${env.CURITY_ISSUER_USER}/authn/authenticate/logout?redirect_uri=${encodeURIComponent(redirectTo)}`
|
||||
logger.debug(`[logoutSafely] final redirectUrl: ${redirectTo}`)
|
||||
|
||||
const redirectUrlObj = await signOut({
|
||||
redirectTo,
|
||||
redirect: false,
|
||||
})
|
||||
|
||||
return NextResponse.redirect(redirectUrlObj.redirect)
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
logger.error("signOutSafelyAuthError", { signOutAuthError: error })
|
||||
} else {
|
||||
logger.error("signOutSafelyError", { signOutError: error })
|
||||
}
|
||||
}
|
||||
|
||||
return internalServerError()
|
||||
}
|
||||
Reference in New Issue
Block a user