Skip to content

Commit

Permalink
feat: remove/reorder tenantId params in account linking
Browse files Browse the repository at this point in the history
  • Loading branch information
porcellus committed Aug 23, 2023
1 parent b40c0b8 commit bfbc110
Show file tree
Hide file tree
Showing 31 changed files with 197 additions and 270 deletions.
6 changes: 3 additions & 3 deletions examples/with-account-linking/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ app.post("/addPassword", verifySession(), async (req: SessionRequest, res) => {
await EmailVerification.verifyEmailUsingToken(session.getTenantId(), tokenResp.token, false);
}

const linkResp = await AccountLinking.linkAccounts(session.getTenantId(), newRecipeUserId, session.getUserId());
const linkResp = await AccountLinking.linkAccounts(newRecipeUserId, session.getUserId());
if (linkResp.status !== "OK") {
return res.json({
status: "GENERAL_ERROR",
Expand Down Expand Up @@ -223,7 +223,7 @@ app.post("/addThirdPartyUser", verifySession(), async (req: SessionRequest, res)
await EmailVerification.verifyEmailUsingToken(session.getTenantId(), tokenResp.token, false);
}

const linkResp = await AccountLinking.linkAccounts(session.getTenantId(), newRecipeUserId, session.getUserId());
const linkResp = await AccountLinking.linkAccounts(newRecipeUserId, session.getUserId());
if (linkResp.status !== "OK") {
return res.json({
status: "GENERAL_ERROR",
Expand Down Expand Up @@ -285,7 +285,7 @@ app.post("/addPhoneNumber", verifySession(), async (req: SessionRequest, res) =>
}
const newRecipeUserId = signUpResp.user.loginMethods[0].recipeUserId;

const linkResp = await AccountLinking.linkAccounts(session.getTenantId(), newRecipeUserId, session.getUserId());
const linkResp = await AccountLinking.linkAccounts(newRecipeUserId, session.getUserId());
if (linkResp.status !== "OK") {
return res.json({
status: "GENERAL_ERROR",
Expand Down
9 changes: 4 additions & 5 deletions lib/build/recipe/accountlinking/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export default class Wrapper {
* no linking that happened.
*/
static createPrimaryUserIdOrLinkAccounts(
recipeUserId: RecipeUserId,
tenantId: string,
recipeUserId: RecipeUserId,
userContext?: any
): Promise<import("../../types").User>;
/**
Expand Down Expand Up @@ -91,7 +91,6 @@ export default class Wrapper {
}
>;
static linkAccounts(
tenantId: string,
recipeUserId: RecipeUserId,
primaryUserId: string,
userContext?: any
Expand Down Expand Up @@ -123,17 +122,17 @@ export default class Wrapper {
wasLinked: boolean;
}>;
static isSignUpAllowed(
tenantId: string,
newUser: AccountInfoWithRecipeId,
isVerified: boolean,
tenantId: string,
userContext?: any
): Promise<boolean>;
static isSignInAllowed(recipeUserId: RecipeUserId, tenantId: string, userContext?: any): Promise<boolean>;
static isSignInAllowed(tenantId: string, recipeUserId: RecipeUserId, userContext?: any): Promise<boolean>;
static isEmailChangeAllowed(
tenantId: string,
recipeUserId: RecipeUserId,
newEmail: string,
isVerified: boolean,
tenantId: string,
userContext?: any
): Promise<boolean>;
}
Expand Down
11 changes: 5 additions & 6 deletions lib/build/recipe/accountlinking/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Wrapper {
* same as the input recipeUserId if it was made into a primary user, or if there was
* no linking that happened.
*/
static async createPrimaryUserIdOrLinkAccounts(recipeUserId, tenantId, userContext = {}) {
static async createPrimaryUserIdOrLinkAccounts(tenantId, recipeUserId, userContext = {}) {
const user = await __1.getUser(recipeUserId.getAsString(), userContext);
if (user === undefined) {
// Should never really come here unless a programming error happened in the app
Expand Down Expand Up @@ -84,9 +84,8 @@ class Wrapper {
userContext,
});
}
static async linkAccounts(tenantId, recipeUserId, primaryUserId, userContext = {}) {
static async linkAccounts(recipeUserId, primaryUserId, userContext = {}) {
return await recipe_1.default.getInstance().recipeInterfaceImpl.linkAccounts({
tenantId,
recipeUserId,
primaryUserId,
userContext,
Expand All @@ -98,15 +97,15 @@ class Wrapper {
userContext,
});
}
static async isSignUpAllowed(newUser, isVerified, tenantId, userContext) {
static async isSignUpAllowed(tenantId, newUser, isVerified, userContext) {
return await recipe_1.default.getInstance().isSignUpAllowed({
newUser,
isVerified,
tenantId,
userContext,
});
}
static async isSignInAllowed(recipeUserId, tenantId, userContext = {}) {
static async isSignInAllowed(tenantId, recipeUserId, userContext = {}) {
const user = await __1.getUser(recipeUserId.getAsString(), userContext);
if (user === undefined) {
// Should never really come here unless a programming error happened in the app
Expand All @@ -118,7 +117,7 @@ class Wrapper {
userContext,
});
}
static async isEmailChangeAllowed(recipeUserId, newEmail, isVerified, tenantId, userContext) {
static async isEmailChangeAllowed(tenantId, recipeUserId, newEmail, isVerified, userContext) {
const user = await __1.getUser(recipeUserId.getAsString(), userContext);
return await recipe_1.default.getInstance().isEmailChangeAllowed({
user,
Expand Down
1 change: 0 additions & 1 deletion lib/build/recipe/accountlinking/recipe.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ export default class Recipe extends RecipeModule {
userContext: any;
}) => Promise<boolean>;
verifyEmailForRecipeUserIfLinkedAccountsAreVerified: (input: {
tenantId: string;
user: User;
recipeUserId: RecipeUserId;
userContext: any;
Expand Down
9 changes: 6 additions & 3 deletions lib/build/recipe/accountlinking/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ class Recipe extends recipeModule_1.default {
return user;
}
let linkAccountsResult = await this.recipeInterfaceImpl.linkAccounts({
tenantId,
recipeUserId: user.loginMethods[0].recipeUserId,
primaryUserId: primaryUser.id,
userContext,
Expand Down Expand Up @@ -560,7 +559,10 @@ class Recipe extends recipeModule_1.default {
let resp = await recipe_1.default
.getInstanceOrThrowError()
.recipeInterfaceImpl.createEmailVerificationToken({
tenantId: input.tenantId,
// While the token we create here is tenant specific, the verification status is not
// So we can use any tenantId the user is associated with here as long as we use the
// same in the verifyEmailUsingToken call
tenantId: input.user.tenantIds[0],
recipeUserId: input.recipeUserId,
email: recipeUserEmail,
userContext: input.userContext,
Expand All @@ -569,7 +571,8 @@ class Recipe extends recipeModule_1.default {
// we purposely pass in false below cause we don't want account
// linking to happen
await recipe_1.default.getInstanceOrThrowError().recipeInterfaceImpl.verifyEmailUsingToken({
tenantId: input.tenantId,
// See comment about tenantId in the createEmailVerificationToken params
tenantId: input.user.tenantIds[0],
token: resp.token,
attemptAccountLinking: false,
userContext: input.userContext,
Expand Down
5 changes: 2 additions & 3 deletions lib/build/recipe/accountlinking/recipeImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function getRecipeImplementation(querier, config, recipeInstance) {
);
return result;
},
linkAccounts: async function ({ tenantId, recipeUserId, primaryUserId, userContext }) {
linkAccounts: async function ({ recipeUserId, primaryUserId, userContext }) {
const accountsLinkingResult = await querier.sendPostRequest(
new normalisedURLPath_1.default("/recipe/accountlinking/user/link"),
{
Expand All @@ -94,7 +94,6 @@ function getRecipeImplementation(querier, config, recipeInstance) {
let user = accountsLinkingResult.user;
if (!accountsLinkingResult.accountsAlreadyLinked) {
await recipeInstance.verifyEmailForRecipeUserIfLinkedAccountsAreVerified({
tenantId,
user: user,
recipeUserId,
userContext,
Expand All @@ -113,7 +112,7 @@ function getRecipeImplementation(querier, config, recipeInstance) {
if (loginMethodInfo === undefined) {
throw Error("this error should never be thrown");
}
await config.onAccountLinked(user, loginMethodInfo, tenantId, userContext);
await config.onAccountLinked(user, loginMethodInfo, userContext);
}
accountsLinkingResult.user = user;
}
Expand Down
10 changes: 2 additions & 8 deletions lib/build/recipe/accountlinking/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ import OverrideableBuilder from "supertokens-js-override";
import type { User } from "../../types";
import RecipeUserId from "../../recipeUserId";
export declare type TypeInput = {
onAccountLinked?: (
user: User,
newAccountInfo: RecipeLevelUser,
tenantId: string,
userContext: any
) => Promise<void>;
onAccountLinked?: (user: User, newAccountInfo: RecipeLevelUser, userContext: any) => Promise<void>;
shouldDoAutomaticAccountLinking?: (
newAccountInfo: AccountInfoWithRecipeId,
user: User | undefined,
Expand All @@ -31,7 +26,7 @@ export declare type TypeInput = {
};
};
export declare type TypeNormalisedInput = {
onAccountLinked: (user: User, newAccountInfo: RecipeLevelUser, tenantId: string, userContext: any) => Promise<void>;
onAccountLinked: (user: User, newAccountInfo: RecipeLevelUser, userContext: any) => Promise<void>;
shouldDoAutomaticAccountLinking: (
newAccountInfo: AccountInfoWithRecipeId,
user: User | undefined,
Expand Down Expand Up @@ -126,7 +121,6 @@ export declare type RecipeInterface = {
}
>;
linkAccounts: (input: {
tenantId: string;
recipeUserId: RecipeUserId;
primaryUserId: string;
userContext: any;
Expand Down
2 changes: 0 additions & 2 deletions lib/build/recipe/emailpassword/recipeImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ function getRecipeInterface(querier, getEmailPasswordConfig) {
);
if (!loginMethod.verified) {
await recipe_1.default.getInstance().verifyEmailForRecipeUserIfLinkedAccountsAreVerified({
tenantId,
user: response.user,
recipeUserId: response.recipeUserId,
userContext,
Expand Down Expand Up @@ -156,7 +155,6 @@ function getRecipeInterface(querier, getEmailPasswordConfig) {
};
}
await recipe_1.default.getInstance().verifyEmailForRecipeUserIfLinkedAccountsAreVerified({
tenantId: input.tenantIdForPasswordPolicy,
user,
recipeUserId: input.recipeUserId,
userContext: input.userContext,
Expand Down
1 change: 0 additions & 1 deletion lib/build/recipe/passwordless/recipeImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ function getRecipeInterface(querier) {
};
}
await recipe_1.default.getInstance().verifyEmailForRecipeUserIfLinkedAccountsAreVerified({
tenantId: input.tenantId,
user,
recipeUserId: input.recipeUserId,
userContext: input.userContext,
Expand Down
1 change: 0 additions & 1 deletion lib/build/recipe/thirdparty/recipeImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ function getRecipeImplementation(querier, providers) {
response.user = new user_1.User(response.user);
response.recipeUserId = new recipeUserId_1.default(response.recipeUserId);
await recipe_1.default.getInstance().verifyEmailForRecipeUserIfLinkedAccountsAreVerified({
tenantId,
user: response.user,
recipeUserId: response.recipeUserId,
userContext,
Expand Down
16 changes: 5 additions & 11 deletions lib/ts/recipe/accountlinking/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export default class Wrapper {
* no linking that happened.
*/
static async createPrimaryUserIdOrLinkAccounts(
recipeUserId: RecipeUserId,
tenantId: string,
recipeUserId: RecipeUserId,
userContext: any = {}
) {
const user = await getUser(recipeUserId.getAsString(), userContext);
Expand Down Expand Up @@ -96,14 +96,8 @@ export default class Wrapper {
});
}

static async linkAccounts(
tenantId: string,
recipeUserId: RecipeUserId,
primaryUserId: string,
userContext: any = {}
) {
static async linkAccounts(recipeUserId: RecipeUserId, primaryUserId: string, userContext: any = {}) {
return await Recipe.getInstance().recipeInterfaceImpl.linkAccounts({
tenantId,
recipeUserId,
primaryUserId,
userContext,
Expand All @@ -118,9 +112,9 @@ export default class Wrapper {
}

static async isSignUpAllowed(
tenantId: string,
newUser: AccountInfoWithRecipeId,
isVerified: boolean,
tenantId: string,
userContext?: any
) {
return await Recipe.getInstance().isSignUpAllowed({
Expand All @@ -131,7 +125,7 @@ export default class Wrapper {
});
}

static async isSignInAllowed(recipeUserId: RecipeUserId, tenantId: string, userContext: any = {}) {
static async isSignInAllowed(tenantId: string, recipeUserId: RecipeUserId, userContext: any = {}) {
const user = await getUser(recipeUserId.getAsString(), userContext);
if (user === undefined) {
// Should never really come here unless a programming error happened in the app
Expand All @@ -146,10 +140,10 @@ export default class Wrapper {
}

static async isEmailChangeAllowed(
tenantId: string,
recipeUserId: RecipeUserId,
newEmail: string,
isVerified: boolean,
tenantId: string,
userContext?: any
) {
const user = await getUser(recipeUserId.getAsString(), userContext);
Expand Down
10 changes: 6 additions & 4 deletions lib/ts/recipe/accountlinking/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ export default class Recipe extends RecipeModule {
}

let linkAccountsResult = await this.recipeInterfaceImpl.linkAccounts({
tenantId,
recipeUserId: user.loginMethods[0].recipeUserId,
primaryUserId: primaryUser.id,
userContext,
Expand Down Expand Up @@ -710,7 +709,6 @@ export default class Recipe extends RecipeModule {
};

verifyEmailForRecipeUserIfLinkedAccountsAreVerified = async (input: {
tenantId: string;
user: User;
recipeUserId: RecipeUserId;
userContext: any;
Expand Down Expand Up @@ -754,7 +752,10 @@ export default class Recipe extends RecipeModule {
if (shouldVerifyEmail) {
let resp = await EmailVerificationRecipe.getInstanceOrThrowError().recipeInterfaceImpl.createEmailVerificationToken(
{
tenantId: input.tenantId,
// While the token we create here is tenant specific, the verification status is not
// So we can use any tenantId the user is associated with here as long as we use the
// same in the verifyEmailUsingToken call
tenantId: input.user.tenantIds[0],
recipeUserId: input.recipeUserId,
email: recipeUserEmail,
userContext: input.userContext,
Expand All @@ -765,7 +766,8 @@ export default class Recipe extends RecipeModule {
// linking to happen
await EmailVerificationRecipe.getInstanceOrThrowError().recipeInterfaceImpl.verifyEmailUsingToken(
{
tenantId: input.tenantId,
// See comment about tenantId in the createEmailVerificationToken params
tenantId: input.user.tenantIds[0],
token: resp.token,
attemptAccountLinking: false,
userContext: input.userContext,
Expand Down
5 changes: 1 addition & 4 deletions lib/ts/recipe/accountlinking/recipeImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,10 @@ export default function getRecipeImplementation(
linkAccounts: async function (
this: RecipeInterface,
{
tenantId,
recipeUserId,
primaryUserId,
userContext,
}: {
tenantId: string;
recipeUserId: RecipeUserId;
primaryUserId: string;
userContext: any;
Expand Down Expand Up @@ -208,7 +206,6 @@ export default function getRecipeImplementation(
let user: UserType = accountsLinkingResult.user;
if (!accountsLinkingResult.accountsAlreadyLinked) {
await recipeInstance.verifyEmailForRecipeUserIfLinkedAccountsAreVerified({
tenantId,
user: user,
recipeUserId,
userContext,
Expand All @@ -229,7 +226,7 @@ export default function getRecipeImplementation(
throw Error("this error should never be thrown");
}

await config.onAccountLinked(user, loginMethodInfo, tenantId, userContext);
await config.onAccountLinked(user, loginMethodInfo, userContext);
}
accountsLinkingResult.user = user;
}
Expand Down
10 changes: 2 additions & 8 deletions lib/ts/recipe/accountlinking/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ import type { User } from "../../types";
import RecipeUserId from "../../recipeUserId";

export type TypeInput = {
onAccountLinked?: (
user: User,
newAccountInfo: RecipeLevelUser,
tenantId: string,
userContext: any
) => Promise<void>;
onAccountLinked?: (user: User, newAccountInfo: RecipeLevelUser, userContext: any) => Promise<void>;
shouldDoAutomaticAccountLinking?: (
newAccountInfo: AccountInfoWithRecipeId,
user: User | undefined,
Expand All @@ -47,7 +42,7 @@ export type TypeInput = {
};

export type TypeNormalisedInput = {
onAccountLinked: (user: User, newAccountInfo: RecipeLevelUser, tenantId: string, userContext: any) => Promise<void>;
onAccountLinked: (user: User, newAccountInfo: RecipeLevelUser, userContext: any) => Promise<void>;
shouldDoAutomaticAccountLinking: (
newAccountInfo: AccountInfoWithRecipeId,
user: User | undefined,
Expand Down Expand Up @@ -141,7 +136,6 @@ export type RecipeInterface = {
}
>;
linkAccounts: (input: {
tenantId: string;
recipeUserId: RecipeUserId;
primaryUserId: string;
userContext: any;
Expand Down
Loading

0 comments on commit bfbc110

Please sign in to comment.