-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Resend Verification Email Endpoint #3543
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
"use strict"; | ||
|
||
const request = require('request'); | ||
const requestp = require('request-promise'); | ||
const Config = require('../src/Config'); | ||
|
||
describe("Email Verification Token Expiration: ", () => { | ||
|
@@ -482,6 +483,257 @@ describe("Email Verification Token Expiration: ", () => { | |
}); | ||
}); | ||
|
||
it('should send a new verification email when a resend is requested and the user is UNVERIFIED', done => { | ||
var user = new Parse.User(); | ||
var sendEmailOptions; | ||
var sendVerificationEmailCallCount = 0; | ||
var emailAdapter = { | ||
sendVerificationEmail: options => { | ||
sendEmailOptions = options; | ||
sendVerificationEmailCallCount++; | ||
}, | ||
sendPasswordResetEmail: () => Promise.resolve(), | ||
sendMail: () => {} | ||
} | ||
reconfigureServer({ | ||
appName: 'emailVerifyToken', | ||
verifyUserEmails: true, | ||
emailAdapter: emailAdapter, | ||
emailVerifyTokenValidityDuration: 5, // 5 seconds | ||
publicServerURL: 'http://localhost:8378/1' | ||
}) | ||
.then(() => { | ||
user.setUsername('resends_verification_token'); | ||
user.setPassword('expiringToken'); | ||
user.set('email', '[email protected]'); | ||
return user.signUp(); | ||
}) | ||
.then(() => { | ||
expect(sendVerificationEmailCallCount).toBe(1); | ||
|
||
return requestp.post({ | ||
uri: 'http://localhost:8378/1/verificationEmailRequest', | ||
body: { | ||
email: '[email protected]' | ||
}, | ||
headers: { | ||
'X-Parse-Application-Id': Parse.applicationId, | ||
'X-Parse-REST-API-Key': 'rest', | ||
}, | ||
json: true, | ||
resolveWithFullResponse: true, | ||
simple: false // this promise is only rejected if the call itself failed | ||
}) | ||
.then((response) => { | ||
expect(response.statusCode).toBe(200); | ||
expect(sendVerificationEmailCallCount).toBe(2); | ||
expect(sendEmailOptions).toBeDefined(); | ||
done(); | ||
}); | ||
}) | ||
.catch(error => { | ||
jfail(error); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should not send a new verification email when a resend is requested and the user is VERIFIED', done => { | ||
var user = new Parse.User(); | ||
var sendEmailOptions; | ||
var sendVerificationEmailCallCount = 0; | ||
var emailAdapter = { | ||
sendVerificationEmail: options => { | ||
sendEmailOptions = options; | ||
sendVerificationEmailCallCount++; | ||
}, | ||
sendPasswordResetEmail: () => Promise.resolve(), | ||
sendMail: () => {} | ||
} | ||
reconfigureServer({ | ||
appName: 'emailVerifyToken', | ||
verifyUserEmails: true, | ||
emailAdapter: emailAdapter, | ||
emailVerifyTokenValidityDuration: 5, // 5 seconds | ||
publicServerURL: 'http://localhost:8378/1' | ||
}) | ||
.then(() => { | ||
user.setUsername('no_new_verification_token_once_verified'); | ||
user.setPassword('expiringToken'); | ||
user.set('email', '[email protected]'); | ||
return user.signUp(); | ||
}) | ||
.then(() => { | ||
return requestp.get({ | ||
url: sendEmailOptions.link, | ||
followRedirect: false, | ||
resolveWithFullResponse: true, | ||
simple: false | ||
}) | ||
.then((response) => { | ||
expect(response.statusCode).toEqual(302); | ||
}); | ||
}) | ||
.then(() => { | ||
expect(sendVerificationEmailCallCount).toBe(1); | ||
|
||
return requestp.post({ | ||
uri: 'http://localhost:8378/1/verificationEmailRequest', | ||
body: { | ||
email: '[email protected]' | ||
}, | ||
headers: { | ||
'X-Parse-Application-Id': Parse.applicationId, | ||
'X-Parse-REST-API-Key': 'rest', | ||
}, | ||
json: true, | ||
resolveWithFullResponse: true, | ||
simple: false // this promise is only rejected if the call itself failed | ||
}) | ||
.then((response) => { | ||
expect(response.statusCode).toBe(400); | ||
expect(sendVerificationEmailCallCount).toBe(1); | ||
done(); | ||
}); | ||
}) | ||
.catch(error => { | ||
jfail(error); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should not send a new verification email if this user does not exist', done => { | ||
var sendEmailOptions; | ||
var sendVerificationEmailCallCount = 0; | ||
var emailAdapter = { | ||
sendVerificationEmail: options => { | ||
sendEmailOptions = options; | ||
sendVerificationEmailCallCount++; | ||
}, | ||
sendPasswordResetEmail: () => Promise.resolve(), | ||
sendMail: () => {} | ||
} | ||
reconfigureServer({ | ||
appName: 'emailVerifyToken', | ||
verifyUserEmails: true, | ||
emailAdapter: emailAdapter, | ||
emailVerifyTokenValidityDuration: 5, // 5 seconds | ||
publicServerURL: 'http://localhost:8378/1' | ||
}) | ||
.then(() => { | ||
return requestp.post({ | ||
uri: 'http://localhost:8378/1/verificationEmailRequest', | ||
body: { | ||
email: '[email protected]' | ||
}, | ||
headers: { | ||
'X-Parse-Application-Id': Parse.applicationId, | ||
'X-Parse-REST-API-Key': 'rest', | ||
}, | ||
json: true, | ||
resolveWithFullResponse: true, | ||
simple: false | ||
}) | ||
.then(response => { | ||
expect(response.statusCode).toBe(400); | ||
expect(sendVerificationEmailCallCount).toBe(0); | ||
expect(sendEmailOptions).not.toBeDefined(); | ||
done(); | ||
}); | ||
}) | ||
.catch(error => { | ||
jfail(error); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should fail if no email is supplied', done => { | ||
var sendEmailOptions; | ||
var sendVerificationEmailCallCount = 0; | ||
var emailAdapter = { | ||
sendVerificationEmail: options => { | ||
sendEmailOptions = options; | ||
sendVerificationEmailCallCount++; | ||
}, | ||
sendPasswordResetEmail: () => Promise.resolve(), | ||
sendMail: () => {} | ||
} | ||
reconfigureServer({ | ||
appName: 'emailVerifyToken', | ||
verifyUserEmails: true, | ||
emailAdapter: emailAdapter, | ||
emailVerifyTokenValidityDuration: 5, // 5 seconds | ||
publicServerURL: 'http://localhost:8378/1' | ||
}) | ||
.then(() => { | ||
request.post({ | ||
uri: 'http://localhost:8378/1/verificationEmailRequest', | ||
body: {}, | ||
headers: { | ||
'X-Parse-Application-Id': Parse.applicationId, | ||
'X-Parse-REST-API-Key': 'rest', | ||
}, | ||
json: true, | ||
resolveWithFullResponse: true, | ||
simple: false | ||
}, (err, response) => { | ||
expect(response.statusCode).toBe(400); | ||
expect(response.body.code).toBe(Parse.Error.EMAIL_MISSING); | ||
expect(response.body.error).toBe('you must provide an email'); | ||
expect(sendVerificationEmailCallCount).toBe(0); | ||
expect(sendEmailOptions).not.toBeDefined(); | ||
done(); | ||
}); | ||
}) | ||
.catch(error => { | ||
jfail(error); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should fail if email is not a string', done => { | ||
var sendEmailOptions; | ||
var sendVerificationEmailCallCount = 0; | ||
var emailAdapter = { | ||
sendVerificationEmail: options => { | ||
sendEmailOptions = options; | ||
sendVerificationEmailCallCount++; | ||
}, | ||
sendPasswordResetEmail: () => Promise.resolve(), | ||
sendMail: () => {} | ||
} | ||
reconfigureServer({ | ||
appName: 'emailVerifyToken', | ||
verifyUserEmails: true, | ||
emailAdapter: emailAdapter, | ||
emailVerifyTokenValidityDuration: 5, // 5 seconds | ||
publicServerURL: 'http://localhost:8378/1' | ||
}) | ||
.then(() => { | ||
request.post({ | ||
uri: 'http://localhost:8378/1/verificationEmailRequest', | ||
body: {email: 3}, | ||
headers: { | ||
'X-Parse-Application-Id': Parse.applicationId, | ||
'X-Parse-REST-API-Key': 'rest', | ||
}, | ||
json: true, | ||
resolveWithFullResponse: true, | ||
simple: false | ||
}, (err, response) => { | ||
expect(response.statusCode).toBe(400); | ||
expect(response.body.code).toBe(Parse.Error.INVALID_EMAIL_ADDRESS); | ||
expect(response.body.error).toBe('you must provide a valid email string'); | ||
expect(sendVerificationEmailCallCount).toBe(0); | ||
expect(sendEmailOptions).not.toBeDefined(); | ||
done(); | ||
}); | ||
}) | ||
.catch(error => { | ||
jfail(error); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('client should not see the _email_verify_token_expires_at field', done => { | ||
var user = new Parse.User(); | ||
var sendEmailOptions; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,7 +191,7 @@ export class UsersRouter extends ClassesRouter { | |
return Promise.resolve(success); | ||
} | ||
|
||
handleResetRequest(req) { | ||
_throwOnBadEmailConfig(req) { | ||
try { | ||
Config.validateEmailConfiguration({ | ||
emailAdapter: req.config.userController.adapter, | ||
|
@@ -202,11 +202,16 @@ export class UsersRouter extends ClassesRouter { | |
} catch (e) { | ||
if (typeof e === 'string') { | ||
// Maybe we need a Bad Configuration error, but the SDKs won't understand it. For now, Internal Server Error. | ||
throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, 'An appName, publicServerURL, and emailAdapter are required for password reset functionality.'); | ||
throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, 'An appName, publicServerURL, and emailAdapter are required for password reset and email verification functionality.'); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
handleResetRequest(req) { | ||
this._throwOnBadEmailConfig(req); | ||
|
||
const { email } = req.body; | ||
if (!email) { | ||
throw new Parse.Error(Parse.Error.EMAIL_MISSING, "you must provide an email"); | ||
|
@@ -228,6 +233,33 @@ export class UsersRouter extends ClassesRouter { | |
}); | ||
} | ||
|
||
handleVerificationEmailRequest(req) { | ||
this._throwOnBadEmailConfig(req); | ||
|
||
const { email } = req.body; | ||
if (!email) { | ||
throw new Parse.Error(Parse.Error.EMAIL_MISSING, 'you must provide an email'); | ||
} | ||
if (typeof email !== 'string') { | ||
throw new Parse.Error(Parse.Error.INVALID_EMAIL_ADDRESS, 'you must provide a valid email string'); | ||
} | ||
|
||
return req.config.database.find('_User', { email: email }).then((results) => { | ||
if (!results.length || results.length < 1) { | ||
throw new Parse.Error(Parse.Error.EMAIL_NOT_FOUND, 'Invalid email.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rather than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated to |
||
} | ||
const user = results[0]; | ||
|
||
if (user.emailVerified) { | ||
throw new Parse.Error(Parse.Error.OTHER_CAUSE, 'User email is already verified.'); | ||
} | ||
|
||
const userController = req.config.userController; | ||
userController.sendVerificationEmail(user); | ||
return { response: {} }; | ||
}); | ||
} | ||
|
||
|
||
mountRoutes() { | ||
this.route('GET', '/users', req => { return this.handleFind(req); }); | ||
|
@@ -238,7 +270,8 @@ export class UsersRouter extends ClassesRouter { | |
this.route('DELETE', '/users/:objectId', req => { return this.handleDelete(req); }); | ||
this.route('GET', '/login', req => { return this.handleLogIn(req); }); | ||
this.route('POST', '/logout', req => { return this.handleLogOut(req); }); | ||
this.route('POST', '/requestPasswordReset', req => { return this.handleResetRequest(req); }) | ||
this.route('POST', '/requestPasswordReset', req => { return this.handleResetRequest(req); }); | ||
this.route('POST', '/verificationEmailRequest', req => { return this.handleVerificationEmailRequest(req); }); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe try and make this available and useful? https://github.com/ParsePlatform/parse-server/blob/0e900cbefda1ce1803e0bd78839afffdcfa01765/src/RestWrite.js#L415
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a natural place for this kind of utility code? I couldn't find one, and I'm not sure it makes sense to create one just to de-dup this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's a good question... @flovilmart, any thoughts/wisdom for us?