Skip to content

Adds ability to prevent login with unverified emails #2165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ var server = ParseServer({
...otherOptions,
// Enable email verification
verifyUserEmails: true,
// allows user to login only after email verification
allowLoginForUnverifiedEmail: false, // defaults to true
// The public URL of your app.
// This will appear in the link that is used to verify email addresses and reset passwords.
// Set the mount path as it is in serverURL
Expand Down
10 changes: 8 additions & 2 deletions src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class Config {
this.serverURL = cacheInfo.serverURL;
this.publicServerURL = removeTrailingSlash(cacheInfo.publicServerURL);
this.verifyUserEmails = cacheInfo.verifyUserEmails;
this.allowLoginForUnverifiedEmail = cacheInfo.allowLoginForUnverifiedEmail;
this.appName = cacheInfo.appName;

this.cacheController = cacheInfo.cacheController;
Expand All @@ -61,11 +62,13 @@ export class Config {
revokeSessionOnPasswordReset,
expireInactiveSessions,
sessionLength,
allowLoginForUnverifiedEmail,
}) {
this.validateEmailConfiguration({
verifyUserEmails: verifyUserEmails,
appName: appName,
publicServerURL: publicServerURL
publicServerURL: publicServerURL,
allowLoginForUnverifiedEmail: allowLoginForUnverifiedEmail,
})

if (typeof revokeSessionOnPasswordReset !== 'boolean') {
Expand All @@ -81,7 +84,7 @@ export class Config {
this.validateSessionConfiguration(sessionLength, expireInactiveSessions);
}

static validateEmailConfiguration({verifyUserEmails, appName, publicServerURL}) {
static validateEmailConfiguration({verifyUserEmails, appName, publicServerURL, allowLoginForUnverifiedEmail}) {
if (verifyUserEmails) {
if (typeof appName !== 'string') {
throw 'An app name is required when using email verification.';
Expand All @@ -90,6 +93,9 @@ export class Config {
throw 'A public server url is required when using email verification.';
}
}
if (!allowLoginForUnverifiedEmail && !verifyUserEmails) {
throw 'You can disallow login for unverified email only if the verifyUserEmails flag is set to true';
}
}

get mount() {
Expand Down
2 changes: 2 additions & 0 deletions src/ParseServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class ParseServer {
serverURL = requiredParameter('You must provide a serverURL!'),
maxUploadSize = '20mb',
verifyUserEmails = false,
allowLoginForUnverifiedEmail = true,
cacheAdapter,
emailAdapter,
publicServerURL,
Expand Down Expand Up @@ -231,6 +232,7 @@ class ParseServer {
hooksController: hooksController,
userController: userController,
verifyUserEmails: verifyUserEmails,
allowLoginForUnverifiedEmail: allowLoginForUnverifiedEmail,
allowClientClassCreation: allowClientClassCreation,
authDataManager: authDataManager(oauth, enableAnonymousUsers),
appName: appName,
Expand Down
5 changes: 5 additions & 0 deletions src/Routers/UsersRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ export class UsersRouter extends ClassesRouter {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Invalid username/password.');
}
user = results[0];

if (req.config.verifyUserEmails && !req.config.allowLoginForUnverifiedEmail && !user.emailVerified) {
throw new Parse.Error(Parse.Error.EMAIL_NOT_FOUND, 'User email is not verified.');
}

return passwordCrypto.compare(req.body.password, user.password);
}).then((correct) => {

Expand Down
5 changes: 5 additions & 0 deletions src/cli/cli-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ export default {
help: "Enable (or disable) user email validation, defaults to false",
action: booleanParser
},
"allowLoginForUnverifiedEmail": {
env: "PARSE_SERVER_ALLOW_LOGIN_FOR_UNVERIFIED_EMAIL",
help: "Allow login even if the users email is not verified, defaults to true",
action: booleanParser
},
"appName": {
env: "PARSE_SERVER_APP_NAME",
help: "Sets the app name"
Expand Down