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

时间:2016-07-22 10:34:49

标签: angularjs node.js mongodb express

以下是身份验证流程的步骤:

  1. 用户通过输入他的详细信息进行注册,他将会 向他的邮件发送了一份OTP。
  2. 此时用户详细信息存储到 MongoDB的。
  3. 通常在验证OTP后,用户可以登录 应用。但在我的情况下,在验证OTP之前,用户可以登录 应用
  4. 如何解决这个请帮帮我。我的一些代码如下所示。

    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!'
            });
        }); 
    });
    

1 个答案:

答案 0 :(得分:2)

当用户通过OTP验证时,为您的架构添加活动属性,默认值为false,然后将此属性设置为true,并允许用户在此属性为true时登录。

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}
});