Skip to content

Instantly share code, notes, and snippets.

@morloy
Last active August 9, 2018 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morloy/641f2677b56c4cb0ba117cced09a9dfd to your computer and use it in GitHub Desktop.
Save morloy/641f2677b56c4cb0ba117cced09a9dfd to your computer and use it in GitHub Desktop.
// /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