Login:OAuth: add handle errors

This commit is contained in:
Timofey Boyko 2024-06-28 13:01:13 +03:00
parent 11b5de43c8
commit dea872e497
6 changed files with 65 additions and 7 deletions

View File

@ -16,5 +16,8 @@
"SsoSettingsDisabled": "Single sign-on is disabled",
"SsoSettingsEmptyToken": "Authentication token could not be found",
"SsoSettingsNotValidToken": "Invalid authentication token",
"SsoSettingsUserTerminated": "This user is disabled"
"SsoSettingsUserTerminated": "This user is disabled",
"OAuthApplicationEmpty": "Application could not be found",
"OAuthApplicationDisabled": "This application is disabled",
"OAuthClientEmpty": "Client id could not be found"
}

View File

@ -35,8 +35,13 @@ import { Text } from "@docspace/shared/components/text";
import { combineUrl } from "@docspace/shared/utils/combineUrl";
import ErrorContainer from "@docspace/shared/components/error-container/ErrorContainer";
import { getMessageFromKey, getMessageKeyTranslate } from "@/utils";
import {
getMessageFromKey,
getMessageKeyTranslate,
getOAuthMessageKeyTranslation,
} from "@/utils";
import { PRODUCT_NAME } from "@docspace/shared/constants";
import { OAuth2ErrorKey } from "@/utils/enums";
const homepage = "/";
@ -62,9 +67,16 @@ const InvalidError = ({ match }: InvalidErrorProps) => {
}, [router]);
const message = getMessageFromKey(match?.messageKey ? +match.messageKey : 1);
const errorTitle = match?.messageKey
? getMessageKeyTranslate(t, message)
: t("Common:ExpiredLink");
const oauthError = getOAuthMessageKeyTranslation(
t,
match?.oauthMessageKey as OAuth2ErrorKey | undefined,
);
const errorTitle = oauthError
? oauthError
: match?.messageKey
? getMessageKeyTranslate(t, message)
: t("Common:ExpiredLink");
return (
<ErrorContainer headerText={errorTitle}>

View File

@ -26,6 +26,7 @@
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { OAuth2ErrorKey } from "./utils/enums";
// This function can be marked `async` if using `await` inside
export function middleware(request: NextRequest) {
@ -49,6 +50,14 @@ export function middleware(request: NextRequest) {
if (oauthClientId === "error")
return NextResponse.redirect(`${redirectUrl}/login/error`);
const error = request.nextUrl.searchParams.get("error");
if (error && error !== OAuth2ErrorKey.missing_asc_cookie_error) {
return NextResponse.redirect(
`${redirectUrl}/login/error?oauthMessageKey=${error}`,
);
}
if (isAuth && !request.nextUrl.pathname.includes("consent")) {
return NextResponse.redirect(
`${redirectUrl}/login/consent${request.nextUrl.search}`,

View File

@ -47,3 +47,13 @@ export enum MessageKey {
"SsoAttributesNotFound",
"QuotaPaidUserLimitError",
}
export enum OAuth2ErrorKey {
asc_retrieval_error = "asc_retrieval_error",
client_disabled_error = "client_disabled_error",
client_not_found_error = "client_not_found_error",
client_permission_denied_error = "client_permission_denied_error",
missing_asc_cookie_error = "missing_asc_cookie_error",
missing_client_id_error = "missing_client_id_error",
something_went_wrong_error = "something_went_wrong_error",
}

View File

@ -27,7 +27,7 @@
import { thirdPartyLogin } from "@docspace/shared/utils/loginUtils";
import { Nullable, TTranslation } from "@docspace/shared/types";
import { MessageKey } from "./enums";
import { MessageKey, OAuth2ErrorKey } from "./enums";
export async function oAuthLogin(profile: string) {
let isSuccess = false;
@ -104,6 +104,30 @@ export const getMessageKeyTranslate = (t: TTranslation, message: string) => {
}
};
export const getOAuthMessageKeyTranslation = (
t: TTranslation,
messageKey?: OAuth2ErrorKey,
) => {
if (!messageKey) return;
switch (messageKey) {
case OAuth2ErrorKey.asc_retrieval_error:
return t("Common:SomethingWentWrong");
case OAuth2ErrorKey.client_disabled_error:
return t("Errors:OAuthApplicationDisabled");
case OAuth2ErrorKey.client_not_found_error:
return t("Errors:OAuthApplicationEmpty");
case OAuth2ErrorKey.client_permission_denied_error:
return t("Common:AccessDenied");
case OAuth2ErrorKey.missing_client_id_error:
return t("Errors:OAuthClientEmpty");
case OAuth2ErrorKey.something_went_wrong_error:
return t("Common:UnknownError");
case OAuth2ErrorKey.missing_asc_cookie_error:
default:
return t("Common:Error");
}
};
export const getInvitationLinkData = (encodeString: string) => {
const fromBinaryStr = (encodeString: string) => {
const decodeStr = atob(encodeString);

View File

@ -1,5 +1,5 @@
{
"date": "2024627_121634",
"date": "2024628_101539",
"checksums": {
"api.js": "0efbae3383bf6c6b6f26d573eee164d2",
"api.poly.js": "2a2ac2c0e4a7007b61d2d1ff7b00a22e",