Last active
August 9, 2018 09:12
-
-
Save morloy/641f2677b56c4cb0ba117cced09a9dfd to your computer and use it in GitHub Desktop.
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
// /imports/api/methods/twoFactorAuth.js | |
import { Meteor } from 'meteor/meteor'; | |
import { ValidatedMethod } from 'meteor/mdg:validated-method'; | |
import otplib from 'otplib'; | |
const userIsLoggedIn = (userId: string) => { | |
if (!userId) { | |
throw new Meteor.Error('users.not-authorized'); | |
} | |
}; | |
export const generateSecret = new ValidatedMethod({ | |
name: 'twoFactor.generateSecret', | |
validate: null, | |
run() { | |
userIsLoggedIn(this.userId); | |
if (Meteor.user().twoFactorEnabled) { | |
throw new Meteor.Error('two-factor.generateSecret.already-enabled'); | |
} | |
if (this.isSimulation) return null; | |
const secret = otplib.authenticator.generateSecret(); | |
Meteor.users.update(this.userId, { $set: { 'services.twoFactorSecret': secret } }); | |
return secret; | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment