用户注册为非活动状态,并通过OTP激活

以下是authenticationstream程的步骤:

  1. 用户通过input他的详细信息进行注册,他将被发送一个OTP到他的邮件。
  2. 此时用户的详细信息存储到mongoDB。
  3. 通常在validationOTP之后,用户可以login到应用程序。 但在我的情况下,validationOTP之前,用户可以login到应用程序。

如何解决这个请帮助我。 我的一些代码如下所示。

model.js

var UserSchema = new Schema({ name: String, email: {type: String, required: true, select: true}, mobile: {type: String, required: true, select: true}, password: {type: String, required: true, select: true}, }); 

controller.js

 vm.submitPost = function(userData){ $http({ url: 'http://192.168.2.8:7200/api/pages/auth/register', method: 'POST', data: userData }).then(function(res) { if(res.data.success){ $location.path('/pages/auth/otp'); } else { alert('Please fill all credentials'); } }, function(error) { alert(error.data); }); }; 

的node.js

 router.post('/pages/auth/register',function(req, res, next){ var user = new User({ name: req.body.username, email: req.body.email, password: req.body.password, mobile: req.body.mobile, }); var secret = "mysecretkey"; var code = otp.generate(secret); var insertOtp = function(db, callback) { db.collection('otp').createIndex( { "createdAt": 1 }, { expireAfterSeconds: 10 } ); db.collection('otp').insertOne( { "createdAt": new Date(), "generatedOtp": code, "logEvent": 2, "logMessage": "Success!" }, function(err, result) { assert.equal(err, null); callback(result); }); }; MongoClient.connect(config.database, function(err, db) { assert.equal(null, err); insertOtp(db, function(err,docs) { db.close(); }); }); var mailOptions={ to : req.body.email, subject : 'OTP', text : "Your One-Time Password is "+code } transport.sendMail(mailOptions, function(error, response){ if(error){ console.log(error); res.end("error"); }else{ res.end("sent"); } }); user.save(function(err){ if(err){ res.send(err); return; } res.json({ success:true, message: 'User has been created!' }); }); }); 

当用户通过OTPvalidation时,将缺省值为false的活动属性添加到您的模式,然后将此属性设置为true,并且如果此属性为true,则允许用户login。

 var UserSchema = new Schema({ name: String, email: {type: String, required: true, select: true}, mobile: {type: String, required: true, select: true}, password: {type: String, required: true, select: true}, active:{ type: 'Boolean', default: false} });