简体   繁体   English

为用户注册添加密码

[英]Adding a secret code to user sign up

Please forgive any ignorance due to unfamiliarity with this framework and its components, I am learning by doing. 请原谅我由于不熟悉此框架及其组件而导致的任何无知。

I have set up a basic app with angular-fullstack and am exploring some tasks I would like to know how to do. 我已经用angular-fullstack建立了一个基本的应用程序,并且正在探索一些我想知道如何做的任务。 Specifically, I would like to add an additional form element to the user sign up process so that not just anyone can sign up, but only those who provide a pre-determined security code that is shared verbally. 具体来说,我想在用户注册过程中添加一个额外的表单元素,以便不仅任何人都可以注册,而且仅提供提供口头共享的预定安全代码的用户可以注册。 If the code entered is invalid, the new user should not be created and (optionally) some message is returned to the user. 如果输入的代码无效,则不应创建新用户,并且(可选)向该用户返回一些消息。

In my server/config/environment/index.js file, I have added an additional item to the secrets key that I will use to check a valid code was entered 在我的server/config/environment/index.js文件中,我向secrets密钥添加了一个附加项目,用于检查输入的有效代码

...

// Secret for session, you will want to change this and make it an environment variable
secrets: {
  session: 'myapp-secret',
  secretCode: 'my-secret' // pre-determined secret code
},

...

In my form, I add the additional field and assign ng-model="secret" . 在我的表单中,添加其他字段并分配ng-model="secret" The form points to the controller's register function, so I also add in the new input's value to the argument being passed to Auth.createUser : 该表单指向控制器的register函数,因此我还将新输入的值添加到传递给Auth.createUser的参数中:

$scope.register = function(form){
    ...

    if (form.$valid) {
        Auth.createUser({
            name: $scope.user.name,
            email: $scope.user.email,
            password: $scope.user.password,
            secret: $scope.secret // My input field to pass to the user controller
        })
    }

    ...
}

Now I can go into the create function of server/api/user/user.controller.js and include my logic for checking the secret code. 现在,我可以进入server/api/user/user.controller.jscreate功能,并包括检查密码的逻辑。

/**
 * Creates a new user
 */
exports.create = function(req, res, next) {
  ...

  if (req.body.secret !== config.secrets.secretCode) {
     // cancel creating a new user
  };

  ...
};

My question now is how am I supposed to handle this inside my if statement? 现在我的问题是我应该如何在if语句中处理此问题? Snooping around the framework, it seems I can maybe just do a redirect or something back to the /signup page and include an error message, but I'm not sure what I should be doing here to handle that. 窥探框架,似乎我可以只做重定向或返回到/signup页面的某些操作,并包含一条错误消息,但是我不确定在此应该做什么。

I have been looking at this from a number of different angles and I (so far as I can tell) haven't yet had the "Aha!" 我从多个角度研究了这个问题,但据我所知,我还没有“啊哈!” moment where I feel confident I am approaching this the right way. 当我感到自信的那一刻,我正朝着正确的方向前进。 Am I going about this in an unconventional way? 我会以一种非常规的方式来解决这个问题吗?

I'm using the SRP principle to guide me here. 我正在使用SRP原理来指导我。

There are 2 red flags. 有2个红旗。 In order to actually create a user, you dont need a secret key. 为了实际创建用户,您不需要密钥。 The secret is required for security purposes for an actual person to create a user. 出于安全目的,该密码是实际人员创建用户所必需的。 So that key logic should be with the other actual-person-focused logic in the controller, not inside the Auth.create method. 因此,关键逻辑应该与控制器中其他以实际人为中心的逻辑保持一致,而不是在Auth.create方法内部。

Second red flag is that you want to keep the redirecting happening in the controller. 第二个危险信号是您想使重定向一直发生在控制器中。 It looks like you saw that red flag, so good work. 看起来您看到了那个危险信号,所以很好。

Have the controller perform the security and the redirects, so that your code will be more or less this: 让控制器执行安全性和重定向,以便您的代码或多或少是这样的:

// controller.js
if (key_matches)
   createUser();
else
   redirectUser()

// auth.js
exports.create = function(req) {
    create_user(req);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM