Skip to content

Commit

Permalink
Merge pull request #48 from ORCID/moveEmail
Browse files Browse the repository at this point in the history
move send email before authentication
  • Loading branch information
lizkrznarich authored Apr 18, 2017
2 parents 03cd2fa + 8fdfa32 commit dfcb2ee
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 50 deletions.
24 changes: 21 additions & 3 deletions local_modules/smid-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ var SmidManger = function(connectionStr) {
* callback(err, [new smid object])
*/

SmidManger.prototype.createSmid = function(orcidRecord, callback) {
this.validateOrcidRecord(orcidRecord);
SmidManger.prototype.createSmid = function(callback) {
var smidManger = this;
var pubKey = randomstring.generate(8);
var privKey = randomstring.generate()
Expand All @@ -102,7 +101,7 @@ SmidManger.prototype.createSmid = function(orcidRecord, callback) {
description: undefined,
title: undefined,
},
owner: orcidRecord
owner: undefined
},
private_key: privKey, // used for allowing edits
public_key: pubKey, // used for shareing
Expand Down Expand Up @@ -197,6 +196,25 @@ SmidManger.prototype.validateOrcidRecord = function(orcidRecord) {
}
};

SmidManger.prototype.addOwnerOrcidRecord = function(orcidRecord, privateKey, callback) {
this.validateOrcidRecord(orcidRecord);
this._smidCol.findAndModify({
query: {
private_key: privateKey
},
update: {
$set: {
'details.owner': orcidRecord
}
},
new: true // this means return the updated object
},
function(err, doc, lastErrorObject) {
if (err) callback(err, null);
else callback(null, doc);
});
}

SmidManger.prototype.addOrcidRecord = function(orcidRecord, publicKey, callback) {
this.validateOrcidRecord(orcidRecord);
this._smidCol.findAndModify({
Expand Down
90 changes: 43 additions & 47 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var CREATE_SMID_URI = '/create-smid-redirect';
var COLLECTION_DETAILS = '/:publicKey/details';
var COLLECTION_DETAILS_DOWNLOAD = '/:publicKey/details/download';
var COLLECTION_DETAILS_FORM = '/:publicKey/details/:privateKey/details/form';
var COLLECTION_DETAILS_EMAIL = '/:publicKey/details/:privateKey/details/owner/email';
var EMAIL_SMID = '/email-smid';
var ADD_ID_AUTHORIZE = '/add-id-authorize/:publicKey';
var ADD_ID_REDIRECT = '/add-id-redirect';
var ADD_ID_SUCCESS = '/:publicKey/orcid/:orcid';
Expand Down Expand Up @@ -89,12 +89,6 @@ app.get(CONFIG, function(req, res) {
});
});

//Create smid oauth sign into ORCID
app.get(CREATE_SMID_AUTHORIZE, function(req, res) {
var create_smid_authorization_uri = ooau.getAuthUrl(config.HOST + CREATE_SMID_URI);
res.redirect(create_smid_authorization_uri);
});

app.get(CONFIG, function(req, res) {
return res.status(200).json({
'ORCID_URL': config.ORCID_URL
Expand Down Expand Up @@ -122,7 +116,7 @@ app.get(CREATE_SMID_URI, function(req, res) { // Redeem code URL
orcidLogger.log(date, token.name, token.orcid, req.query.state);
console.log("creating smid for " + token.orcid);
var orcidRecord = smidManger.createOrcidRecord(token.orcid, ooau.fullOrcid(token.orcid), token.name);
smidManger.createSmid(orcidRecord, function(err, doc) {
smidManger.addOwnerOrcidRecord(orcidRecord, req.query.state, function(err, doc) {
if (err) res.send(err)
else {
var collection = JSON.parse(JSON.stringify(doc, null, 2));
Expand Down Expand Up @@ -183,49 +177,51 @@ app.put(COLLECTION_DETAILS_FORM, function(req, res) {
});
});

//Update collection details form fields
app.put(COLLECTION_DETAILS_EMAIL, function(req, res) {
// create and email smid
app.post(EMAIL_SMID, function(req, res) {
var data = req.body;
console.log(data.email)
mailgunPub.validate(data.email, function (error, body) {
if(body && body.is_valid){
var mailData = {
from: 'No Reply <[email protected]>',
to: data.email,
subject: 'Share My iD links',
text: `Thanks for creating a ORCID iD collection.\n`
+ `\n`
+ `\n`
+ `Administration Link\n`
+ `Use this link to edit collection details: \n`
+ `${config.HOST}/${req.params.publicKey}/edit/${req.params.privateKey}\n`
+ `\n`
+ `\n`
+ `Share Link\n`
+ `Share this link with anyone whose iD you want to collect, or display this page on a laptop/tablet at your event:`
+ `${config.HOST}/${req.params.publicKey}`
+ `\n`
+ `\n`
+ `Thanks,\n`
+ `The Share My iD Team`
};
mailgunPriv.messages().send(mailData, function (error, body) {
if (error != null) {
console.log("mailgun error:");
console.log(error);
if (body != null && body.message != null && body.message.includes("Great job"))
res.status(200).json({'email': data.email}); // using test credentials
else
res.status(400).json({'error':error, 'body': body})
} else {
console.log("mailgun body:");
console.log(body);
res.status(200).json({'email': data.email});
}
if(body && body.is_valid) {
console.log("email is valid");
smidManger.createSmid(function(err, doc) {
var mailData = {
from: 'No Reply <[email protected]>',
to: data.email,
subject: 'Share My iD links',
text: `Thanks for creating a ORCID iD collection.\n`
+ `\n`
+ `\n`
+ `Administration Link\n`
+ `Use this link to edit collection details: \n`
+ `${config.HOST}/${doc.public_key}/edit/${doc.private_key}\n`
+ `\n`
+ `\n`
+ `Share Link\n`
+ `Share this link with anyone whose iD you want to collect, or display this page on a laptop/tablet at your event:`
+ `${config.HOST}/${doc.public_key}`
+ `\n`
+ `\n`
+ `Thanks,\n`
+ `The Share My iD Team`
};
var create_smid_authorization_uri = ooau.getAuthUrl(config.HOST + CREATE_SMID_URI, doc.private_key);
mailgunPriv.messages().send(mailData, function (error, body) {
if (error != null) {
console.log("mailgun error:");
console.log(error);
if (body != null && body.message != null && body.message.includes("Great job"))
res.status(200).json({'email': data.email, 'redirect': create_smid_authorization_uri }); // using test credentials
else
res.status(400).json({'error':error, 'body': body});
} else {
console.log("mailgun body:");
console.log(body);
res.status(200).json({'email': data.email});
}
});
});
// do something
} else {
res.status(400).json({'error':error, 'body': body});
res.status(400).json({'error':"Email failed to pass validation", 'body': body});
}
});
});
Expand Down

0 comments on commit dfcb2ee

Please sign in to comment.