fix(auth): restore BetterAuth OIDC flow across api/web/compose
This commit is contained in:
@@ -133,6 +133,11 @@ export class AuthController {
|
||||
);
|
||||
|
||||
if (!res.headersSent) {
|
||||
const mappedError = this.mapToHttpException(error);
|
||||
if (mappedError) {
|
||||
throw mappedError;
|
||||
}
|
||||
|
||||
throw new HttpException(
|
||||
"Unable to complete authentication. Please try again in a moment.",
|
||||
HttpStatus.INTERNAL_SERVER_ERROR
|
||||
@@ -159,4 +164,45 @@ export class AuthController {
|
||||
// Fall back to direct IP
|
||||
return req.ip ?? req.socket.remoteAddress ?? "unknown";
|
||||
}
|
||||
|
||||
/**
|
||||
* Preserve known HTTP errors from BetterAuth/better-call instead of converting
|
||||
* every failure into a generic 500.
|
||||
*/
|
||||
private mapToHttpException(error: unknown): HttpException | null {
|
||||
if (error instanceof HttpException) {
|
||||
return error;
|
||||
}
|
||||
|
||||
if (!error || typeof error !== "object") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const statusCode = "statusCode" in error ? error.statusCode : undefined;
|
||||
if (!this.isHttpStatus(statusCode)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const responseBody = "body" in error && error.body !== undefined ? error.body : undefined;
|
||||
if (
|
||||
responseBody !== undefined &&
|
||||
responseBody !== null &&
|
||||
(typeof responseBody === "string" || typeof responseBody === "object")
|
||||
) {
|
||||
return new HttpException(responseBody, statusCode);
|
||||
}
|
||||
|
||||
const message =
|
||||
"message" in error && typeof error.message === "string" && error.message.length > 0
|
||||
? error.message
|
||||
: "Authentication request failed";
|
||||
return new HttpException(message, statusCode);
|
||||
}
|
||||
|
||||
private isHttpStatus(value: unknown): value is number {
|
||||
if (typeof value !== "number" || !Number.isInteger(value)) {
|
||||
return false;
|
||||
}
|
||||
return value >= 400 && value <= 599;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user