Created
April 14, 2020 10:27
-
-
Save mjzone/a38e7e8df2cbbdcf131da814afbdfe0f to your computer and use it in GitHub Desktop.
User Migration Example
This file contains 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
let _ = require('underscore'); | |
let USERS_TABLE = process.env.USERS_TABLE; | |
module.exports.migrateUser = async (event, context) => { | |
if (event.triggerSource == 'UserMigration_Authentication') { | |
// check if the user exists on mlab | |
if (event.userName && event.userName.length) { | |
let db = await dbHelper.connectToDatabase(MONGODB_URI, DB_NAME); | |
// lookup on the mongodb | |
lookUp(db, event.userName).then( | |
user => { | |
if (user) { | |
event.response.userAttributes = { | |
email: user.email, | |
name: user.name, | |
email_verified: 'true' | |
}; | |
event.response.finalUserStatus = 'CONFIRMED'; | |
event.response.messageAction = 'SUPPRESS'; | |
context.succeed(event); | |
} | |
context.fail('Incorrect email or password'); | |
}, | |
err => { | |
context.fail('Incorrect email or password'); | |
} | |
); | |
} else { | |
context.fail('Incorrect email or password'); | |
} | |
} else { | |
context.succeed(event); | |
} | |
}; | |
let lookUp = (db, username) => { | |
username = username.toLowerCase(); | |
if (username.length) { | |
return db.collection(USERS_TABLE).findOne({ | |
email: username | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment