简体   繁体   English

使用Postgres进行PassportJS和用户创建

[英]PassportJS and user creation with postgres

I've been trying to use pg-promise with passportjs, but I can't get to work local-signup. 我一直在尝试将pg-promise与passwordjs一起使用,但无法进行本地注册。 My login works well. 我的登录正常。 Please see below: 请看下面:

    passport.use(
      "local-signup",
      new LocalStrategy(
        {
          usernameField: "email",
          passwordField: "pass",
          passReqToCallback: true
        },
        (req, email, pass, done) => {
          process.nextTick(function() {
            q.db
              .one("select email from users where email = $1", email)
              .then(data => {
                if (data) {
                  return done(
                    null,
                    false,
                    req.flash("signupMessage", "User already exists")
                  );
                } else {
                  console.log("here!?");
                  q.db
                    .none(
                      "insert into users (email, pass)" +
                        `values (${email}, ${pass})`
                    )
                    .then(data => {
                      console.log("created?");
                    });
                }
              })
              .catch(err => {
                console.log(err);
              });
          });
        }
      )
    );

The problem here is that, it actually detects if an user is already in the database, but if the user doesn't exists, it doesn't create the username, but instead, skips the whole process. 这里的问题是,它实际上检测到用户是否已经在数据库中,但是如果该用户不存在,则不会创建用户名,而是跳过整个过程。

Any ideas how to fix this issue? 任何想法如何解决此问题?

Thank you 谢谢

Your code inside nextTick should be something like this: 您在nextTick代码应如下所示:

q.db.task(async t => {
    const user = await t.oneOrNone('SELECT * FROM users WHERE email = $1', email);
    if (user) {
        return false;
    }
    await t.none('INSERT INTO users(email, pass) VALUES($1, $2)', [email, pass]);
    return true;
})
    .then(created => {
        done(
            null,
            false,
            req.flash('signupMessage', 'Created: ' + created)
        );
    })
    .catch(error => {
        console.log(error);
    });

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

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