Skip to content

Instantly share code, notes, and snippets.

@jarodreyes
Created November 12, 2020 19:26
Show Gist options
  • Save jarodreyes/fb6d12da1935ae375e907a0e67015560 to your computer and use it in GitHub Desktop.
Save jarodreyes/fb6d12da1935ae375e907a0e67015560 to your computer and use it in GitHub Desktop.
/** @type {PostLoginAction} */
module.exports = async (event, context) => {
const approveLogin = () => {
return {
user: {
appMetadata: {
actions: 'got-callback'
},
userMetadata: {
person: event.actor.query.person,
phone: '+12066505813'
}
}
};
}
const sendSMS = () => {
const client = require('twilio')(context.secrets.TWILIO_ACCOUNT_SID, context.secrets.TWILIO_ACCOUNT_SID)
client.messages.create({
body: `It appears that your child is attempting to login from ${event.actor.geoIp}. Do you approve? Response 'yes' or 'no'.`,
from: context.secrets.PHONE_NUMBER,
to: event.user.userMetadata.phone
})
}
if (event.protocol === 'redirect-callback') {
if (event.actor.query.person == 'parent') {
approveLogin()
} else {
// send SMS
sendSMS()
if (event.actor.query.approved) {
approveLogin()
} else {
throw new Error('Login not approved by parent.')
}
}
}
if (event.user.appMetadata.actions !== 'got-callback') {
return {
command: {
type: 'redirect',
url: 'https://parent-approval-ah11.herokuapp.com/parent-prompt'
}
};
}
return {};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment