Last active
November 30, 2017 15:26
-
-
Save ihfazhillah/4823397688bcef3ffe873fa9dab73896 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const validator = require("email-validator"); | |
module.exports = function(context, callback) { | |
/* | |
* module to check email, context.data.input is a scaphold's CreateUserInput. | |
* email is must be valid email, using email-validator | |
*/ | |
const userInput = context.data.input; | |
if (!validator.validate(userInput.email)) { | |
callback("not valid email provided", null); | |
} | |
callback(null, context.data.input); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nodemailer = require("nodemailer"); | |
module.exports = function(context, callback) { | |
/* | |
* params context.data.payload is data payload from createUserPayload | |
* used to send verification email into registered user | |
* expected context.secrets.user and context.secrets.password | |
*/ | |
var transporter = nodemailer.createTransport({ | |
host: "smtp.gmail.com", | |
port: 587, | |
secure: false, | |
requireTLS: true, | |
auth: { | |
user: context.secrets.user, | |
pass: context.secrets.password | |
} | |
}); | |
var email = context.data.changedUser.email, | |
name = context.data.changedUser.username, | |
code = context.data.changedUser.verificationCode; | |
var mailOptions = { | |
from: "[email protected]", | |
to: email, | |
subject: "Verify your upSlack Account", | |
html: ` | |
<p>Please verify your <b>upSlack</b> account by clicking the link below</p> | |
<p>https://upslack.netlify.com/#/verify/${name}/${code}</p> | |
` | |
}; | |
transporter.sendMail(mailOptions, (err, info) => { | |
if (err) { | |
callback("something went wrong", null); | |
} else { | |
callback(null, "horray....!!!"); | |
} | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var rp = require("request-promise"); | |
module.exports = function(ctx, cb) { | |
/* | |
* expected input from scaphold: | |
* id === "1", username, and verificationCode | |
* if this all true, then this will create request to search that username + verificatioCode and get the id, and then perform verify | |
* for the particular user | |
* else: will use default implementation | |
* | |
* this need the token secrets <= the token to perform user query | |
*/ | |
var userInput = ctx.data.input; | |
if ( | |
userInput.id === "1" && | |
userInput.verificationCode && | |
userInput.username | |
) { | |
var getUserQuery = `query($username: String, $verificationCode: String){ | |
viewer{ | |
allUsers (where: {username: {eq: $username}, verificationCode: {eq: $verificationCode}}, first: 1){ | |
edges{ | |
node{ | |
id | |
}}}}}`; | |
var variables = { | |
username: userInput.username, | |
verificationCode: userInput.verificationCode | |
}; | |
var token = ctx.secrets.token; | |
var authorization = `Bearer ${token}`; | |
var url = "https://us-west-2.api.scaphold.io/graphql/slack-slack-me"; | |
return rp({ | |
url: url, | |
headers: { | |
Authorization: authorization | |
}, | |
json: true, | |
method: "POST", | |
body: { | |
query: getUserQuery, | |
variables: variables | |
} | |
}) | |
.then(data => { | |
var id = data.data.viewer.allUsers.edges[0].node.id; | |
var inputResult = { | |
input: { id: id, verified: true } | |
}; | |
return cb(null, inputResult); | |
}) | |
.catch(error => cb(error, null)); | |
} | |
cb(null, userInput); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const uuid = require("uuid"); | |
module.exports = function(context, cb) { | |
const userInput = context.data.input; | |
const verificationCode = uuid(); | |
/* | |
const result = Object.assign({}, userInput, { | |
verificationCode: verificationCode | |
}); | |
*/ | |
userInput.verificationCode = verificationCode; | |
cb(null, { input: userInput }); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment