Skip to content

Commit 69c06df

Browse files
feat: add support for picking up not allowed to signup error in FE
1 parent cb8fcd5 commit 69c06df

File tree

5 files changed

+53
-19
lines changed

5 files changed

+53
-19
lines changed

packages/tenant-enrollment-nodejs/src/plugin.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ export const init = createPluginInitFunction<
9393
logDebugMessage("Reason: " + reason);
9494
if (!canJoin) {
9595
return {
96-
status: "LINKING_TO_SESSION_USER_FAILED",
97-
reason: "EMAIL_VERIFICATION_REQUIRED",
96+
// Use the `EMAIL_ALREADY_EXISTS_ERROR` since that is returned
97+
// directly without modification from the `signUpPOST` method.
98+
status: "EMAIL_ALREADY_EXISTS_ERROR",
99+
reason,
98100
};
99101
}
100102

@@ -107,17 +109,19 @@ export const init = createPluginInitFunction<
107109
...originalImplementation,
108110
signUpPOST: async (input) => {
109111
const response = await originalImplementation.signUpPOST!(input);
110-
if (response.status === "SIGN_UP_NOT_ALLOWED" && response.reason.includes("ERR_CODE_013")) {
111-
// There is a possibility that the user is not allowed
112-
// to signup to the tenant so we will have to update the message
113-
// accordingly.
112+
113+
logDebugMessage(`Got response status for signup: ${response.status}`);
114+
115+
// If the status is `EMAIL_ALREADY_EXISTS_ERROR`, we will have to pick that
116+
// up and return a GENERAL_ERROR instead to make the error passed along to
117+
// the FE
118+
if (response.status === "EMAIL_ALREADY_EXISTS_ERROR") {
114119
return {
115-
...response,
116-
reason: "Cannot sign in / sign up due to security reasons or tenant doesn't allow signup",
120+
status: "GENERAL_ERROR",
121+
message: (response as any).reason,
117122
};
118123
}
119124

120-
logDebugMessage(`Got response status for signup: ${response.status}`);
121125
if (response.status !== "OK") {
122126
return response;
123127
}
@@ -135,12 +139,10 @@ export const init = createPluginInitFunction<
135139
logDebugMessage(`wasAdded: ${wasAddedToTenant}`);
136140
logDebugMessage(`reason: ${tenantJoiningReason}`);
137141
return {
138-
status: "PENDING_APPROVAL",
142+
status: "PENDING_APPROVAL" as any,
139143
wasAddedToTenant,
140144
reason: tenantJoiningReason,
141145
};
142-
143-
// return response;
144146
},
145147
};
146148
},

packages/tenant-enrollment-nodejs/src/recipeImplementation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
AssociateAllLoginMethodsOfUserWithTenant,
66
SendPluginEmail,
77
} from "@supertokens-plugins/tenants-nodejs";
8-
import { ROLES } from "@shared/tenants";
8+
import { NOT_ALLOWED_TO_SIGNUP_REASONS, ROLES } from "@shared/tenants";
99
import SuperTokens from "supertokens-node";
1010
import { UserContext } from "supertokens-node/lib/build/types";
1111

@@ -35,7 +35,7 @@ export const getOverrideableTenantFunctionImplementation = (
3535
if (implementation.isTenantInviteOnly(tenantId)) {
3636
return {
3737
canJoin: false,
38-
reason: "INVITE_ONLY",
38+
reason: NOT_ALLOWED_TO_SIGNUP_REASONS.INVITE_ONLY,
3939
};
4040
}
4141

@@ -44,12 +44,12 @@ export const getOverrideableTenantFunctionImplementation = (
4444
if (emailOrThirdPartyId.type === "email") {
4545
canJoin = implementation.isMatchingEmailDomain(tenantId, emailOrThirdPartyId.email);
4646
if (!canJoin) {
47-
reason = "EMAIL_DOMAIN_NOT_ALLOWED";
47+
reason = NOT_ALLOWED_TO_SIGNUP_REASONS.EMAIL_DOMAIN_NOT_ALLOWED;
4848
}
4949
} else if (emailOrThirdPartyId.type === "thirdParty") {
5050
canJoin = implementation.isApprovedIdPProvider(tenantId, emailOrThirdPartyId.thirdPartyId);
5151
if (!canJoin) {
52-
reason = "IDP_NOT_ALLOWED";
52+
reason = NOT_ALLOWED_TO_SIGNUP_REASONS.IDP_NOT_ALLOWED;
5353
}
5454
}
5555

packages/tenant-enrollment-react/src/plugin.tsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
getTranslationFunction,
99
} from "supertokens-auth-react";
1010

11+
import { NOT_ALLOWED_TO_SIGNUP_REASONS } from "../../../shared/tenants/src";
12+
1113
import { getApi } from "./api";
1214
import { PLUGIN_ID, API_PATH } from "./constants";
1315
import { enableDebugLogs, logDebugMessage } from "./logger";
@@ -76,8 +78,32 @@ export const init = createPluginInitFunction<
7678
functions: (originalImplementation) => ({
7779
...originalImplementation,
7880
signUp: async (input) => {
79-
const signUpResponse = await originalImplementation.signUp(input);
81+
let signUpResponse;
82+
83+
try {
84+
signUpResponse = await originalImplementation.signUp(input);
85+
} catch (error: any) {
86+
// Check if the error is a STGeneralError
87+
logDebugMessage(`Caught error: ${error}`);
88+
if (error.isSuperTokensGeneralError === true) {
89+
logDebugMessage(`Got general error with reason: ${error.message}`);
90+
91+
// Check if the message is one of the not allowed defined errors.
92+
if (Object.values(NOT_ALLOWED_TO_SIGNUP_REASONS).includes(error.message)) {
93+
logDebugMessage("Found not-allowed to signup flow, redirecting");
94+
95+
// Update the message before re-throwing the error
96+
error.message = "Not allowed to signup to tenant";
97+
98+
// TODO: Redirect the user to not allowed to signup view
99+
}
100+
}
101+
102+
throw error;
103+
}
104+
80105
logDebugMessage(`response: ${signUpResponse}`);
106+
81107
if ((signUpResponse.status as any) !== "PENDING_APPROVAL") {
82108
return signUpResponse;
83109
}

shared/tenants/src/errors.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const NOT_ALLOWED_TO_SIGNUP_REASONS = {
2+
INVITE_ONLY: "INVITE_ONLY",
3+
EMAIL_DOMAIN_NOT_ALLOWED: "EMAIL_DOMAIN_NOT_ALLOWED",
4+
IDP_NOT_ALLOWED: "IDP_NOT_ALLOWED",
5+
};

shared/tenants/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
export * from './types';
2-
export * from './roles';
1+
export * from "./types";
2+
export * from "./roles";
3+
export * from "./errors";

0 commit comments

Comments
 (0)