Skip to content

Commit d3e73aa

Browse files
rishabhpoddarRishabh
andauthored
throws an error in case user tries to update email / password of a third party user (#249)
Co-authored-by: Rishabh <[email protected]>
1 parent 6680074 commit d3e73aa

File tree

4 files changed

+143
-5
lines changed

4 files changed

+143
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [unreleased]
1010

11+
- Fixes https://github.com/supertokens/supertokens-node/issues/244 - throws an error if a user tries to update email / password of a third party login user.
12+
1113
## [8.5.0] - 2022-01-14
1214

1315
### Added

lib/build/recipe/thirdpartyemailpassword/recipeImplementation/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ function getRecipeInterface(emailPasswordQuerier, thirdPartyQuerier) {
125125
},
126126
updateEmailOrPassword: function (input) {
127127
return __awaiter(this, void 0, void 0, function* () {
128+
let user = yield this.getUserById({ userId: input.userId });
129+
if (user === undefined) {
130+
return {
131+
status: "UNKNOWN_USER_ID_ERROR",
132+
};
133+
} else if (user.thirdParty !== undefined) {
134+
throw new Error("Cannot update email or password of a user who signed up using third party login.");
135+
}
128136
return originalEmailPasswordImplementation.updateEmailOrPassword.bind(
129137
emailPasswordRecipeImplementation_1.default(this)
130138
)(input);

lib/ts/recipe/thirdpartyemailpassword/recipeImplementation/index.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,22 @@ export default function getRecipeInterface(
103103
return originalEmailPasswordImplementation.resetPasswordUsingToken.bind(DerivedEP(this))(input);
104104
},
105105

106-
updateEmailOrPassword: async function (input: {
107-
userId: string;
108-
email?: string;
109-
password?: string;
110-
}): Promise<{ status: "OK" | "UNKNOWN_USER_ID_ERROR" | "EMAIL_ALREADY_EXISTS_ERROR" }> {
106+
updateEmailOrPassword: async function (
107+
this: RecipeInterface,
108+
input: {
109+
userId: string;
110+
email?: string;
111+
password?: string;
112+
}
113+
): Promise<{ status: "OK" | "UNKNOWN_USER_ID_ERROR" | "EMAIL_ALREADY_EXISTS_ERROR" }> {
114+
let user = await this.getUserById({ userId: input.userId });
115+
if (user === undefined) {
116+
return {
117+
status: "UNKNOWN_USER_ID_ERROR",
118+
};
119+
} else if (user.thirdParty !== undefined) {
120+
throw new Error("Cannot update email or password of a user who signed up using third party login.");
121+
}
111122
return originalEmailPasswordImplementation.updateEmailOrPassword.bind(DerivedEP(this))(input);
112123
},
113124
};

test/thirdpartyemailpassword/signupFeature.test.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,4 +819,121 @@ describe(`signupTest: ${printPath("[test/thirdpartyemailpassword/signupFeature.t
819819
assert(usersNewest2.users[0].recipeId === "emailpassword");
820820
assert(usersNewest2.users[0].user.email === "[email protected]");
821821
});
822+
823+
it("updateEmailOrPassword function test for third party login", async function () {
824+
await startST();
825+
826+
STExpress.init({
827+
supertokens: {
828+
connectionURI: "http://localhost:8080",
829+
},
830+
appInfo: {
831+
apiDomain: "api.supertokens.io",
832+
appName: "SuperTokens",
833+
websiteDomain: "supertokens.io",
834+
},
835+
recipeList: [
836+
ThirdPartyEmailPassword.init({
837+
providers: [this.customProvider1],
838+
}),
839+
Session.init(),
840+
],
841+
});
842+
843+
let thirdPartyRecipe = ThirdPartyEmailPasswordRecipe.getInstanceOrThrowError();
844+
845+
assert.strictEqual(await ThirdPartyEmailPassword.getUserByThirdPartyInfo("custom", "user"), undefined);
846+
847+
const app = express();
848+
849+
app.use(middleware());
850+
851+
app.use(errorHandler());
852+
853+
nock("https://test.com").post("/oauth/token").reply(200, {});
854+
855+
{
856+
let response = await new Promise((resolve) =>
857+
request(app)
858+
.post("/auth/signinup")
859+
.send({
860+
thirdPartyId: "custom",
861+
code: "32432432",
862+
redirectURI: "http://localhost.org",
863+
})
864+
.end((err, res) => {
865+
if (err) {
866+
resolve(undefined);
867+
} else {
868+
resolve(res);
869+
}
870+
})
871+
);
872+
assert.strictEqual(response.statusCode, 200);
873+
874+
let signUpUserInfo = response.body.user;
875+
let userInfo = await ThirdPartyEmailPassword.getUserByThirdPartyInfo("custom", "user");
876+
877+
assert.strictEqual(userInfo.email, signUpUserInfo.email);
878+
assert.strictEqual(userInfo.id, signUpUserInfo.id);
879+
880+
try {
881+
await ThirdPartyEmailPassword.updateEmailOrPassword({
882+
userId: userInfo.id,
883+
884+
});
885+
throw new Error("test failed");
886+
} catch (err) {
887+
if (
888+
err.message !== "Cannot update email or password of a user who signed up using third party login."
889+
) {
890+
throw err;
891+
}
892+
}
893+
}
894+
895+
{
896+
let response = await new Promise((resolve) =>
897+
request(app)
898+
.post("/auth/signup")
899+
.send({
900+
formFields: [
901+
{
902+
id: "email",
903+
904+
},
905+
{
906+
id: "password",
907+
value: "pass@123",
908+
},
909+
],
910+
})
911+
.end((err, res) => {
912+
if (err) {
913+
resolve(undefined);
914+
} else {
915+
resolve(res);
916+
}
917+
})
918+
);
919+
assert.strictEqual(response.statusCode, 200);
920+
921+
let signUpUserInfo = response.body.user;
922+
923+
let r = await ThirdPartyEmailPassword.updateEmailOrPassword({
924+
userId: signUpUserInfo.id,
925+
926+
password: "haha@1234",
927+
});
928+
929+
assert(r.status === "OK");
930+
931+
let r2 = await ThirdPartyEmailPassword.updateEmailOrPassword({
932+
userId: signUpUserInfo.id + "123",
933+
934+
});
935+
936+
assert(r2.status === "UNKNOWN_USER_ID_ERROR");
937+
}
938+
});
822939
});

0 commit comments

Comments
 (0)