Skip to content

Instantly share code, notes, and snippets.

@ihfazhillah
Last active November 30, 2017 15:26
Show Gist options
  • Save ihfazhillah/4823397688bcef3ffe873fa9dab73896 to your computer and use it in GitHub Desktop.
Save ihfazhillah/4823397688bcef3ffe873fa9dab73896 to your computer and use it in GitHub Desktop.
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);
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);
};
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