有人用过 Mongoose-auth 吗?如何覆盖 findOrCreateUsers?

发布于 2024-11-03 21:54:42 字数 1248 浏览 2 评论 0原文

现在我正在使用 mongoose-auth 的 Facebook Connect。一切正常,并且用户正在通过我的 mongodb 服务器创建/登录。

但是,我想覆盖 findOrCreateUsers,因为我想在注册时为每个新用户分配一个随机“代码”,并将其放入他们的 db.users 配置文件中。

因此,我查看了 mongoose-auth/lib/facebook,并将这 3 个 facebook 模块文件复制到我自己的目录中 [everyauth.js、index.js、schema.js]。为了运行一个简单的测试,我基本上从我的目录中包含了我自己的 everyauth.js,并使用它的 findOrCreateUsers 方法而不是默认的方法。

它可以工作,但有一个错误。登录/创建用户后,req.user 未定义。 (req.loggedIn 显示 True,耶!)

我知道这是一个新库,但如果您使用过它(或者对 Node.js 了解很多),我真的很感激你可以帮助我。我在 irc.freenode.net 上的 #node.js。昵称是“xeodox”。如果您能帮助我,我将非常感激!

我通过对 app.js 中的 facebook 模块执行以下操作来覆盖 findOrCreateUser:

 findOrCreateUser: function (sess, accessTok, accessTokExtra, fbUser) {
    var promise = new Promise()
      , self = this;
    // TODO Check user in session or request helper first
    //      e.g., req.user or sess.auth.userId
    this.User()().findOne({'fb.id': fbUser.id}, function (err, foundUser) {
      if (foundUser)
        return promise.fulfill(foundUser);
      self.User()().createWithFB(fbUser, accessTok, accessTokExtra.expires, function (err, createdUser) {
        return promise.fulfill(createdUser);
      });
    });
    return promise;
  }

Right now I'm using mongoose-auth's Facebook Connect. Everything works fine, and users are being created/logged in through my mongodb server.

However, I want to over-ride findOrCreateUsers, because I want to assign each new user a random "code" upon signup, and put it inside their db.users profile.

So I looked into the mongoose-auth/lib/facebook, and I copied those 3 facebook module files into my own directory [everyauth.js, index.js, schema.js]. To run a simple test, I basically included my own everyauth.js from my directory, and used its findOrCreateUsers method instead of the default one.

It works, but there is one error. After logging in/creating the user, req.user is undefined. (req.loggedIn shows True, yay!)

I know this is a new library, but if you've used it (or know a lot about Node.js), I'd really appreciate it if you could help me. I'm on #node.js on irc.freenode.net. Nickname is "xeodox". If you could help me I'd really appreciate it!!

I overrode findOrCreateUser by doing this to the facebook module in my app.js:

 findOrCreateUser: function (sess, accessTok, accessTokExtra, fbUser) {
    var promise = new Promise()
      , self = this;
    // TODO Check user in session or request helper first
    //      e.g., req.user or sess.auth.userId
    this.User()().findOne({'fb.id': fbUser.id}, function (err, foundUser) {
      if (foundUser)
        return promise.fulfill(foundUser);
      self.User()().createWithFB(fbUser, accessTok, accessTokExtra.expires, function (err, createdUser) {
        return promise.fulfill(createdUser);
      });
    });
    return promise;
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林很绿却致人迷途 2024-11-10 21:54:42

无需将这些文件复制到您自己的目录中。只需在 mongoose-auth 配置中执行以下操作:

facebook: {
  everyauth: {
      myHostname: 'http://localhost:3000'
    , appId: 'YOUR APP ID HERE'
    , appSecret: 'YOUR APP SECRET HERE'
    , redirectPath: '/home'
    , findOrCreateUser: function (session, accessToken, fbUserMetadata) {
      }
  }
}

有关未定义的 req.user 的帮助,请提交 issue 在 github 上,其中包含指向独立 gist 的链接,以重新创建问题,我可以帮助您解决。

No need to copy those files into your own directory. Just do the following in your mongoose-auth configuration:

facebook: {
  everyauth: {
      myHostname: 'http://localhost:3000'
    , appId: 'YOUR APP ID HERE'
    , appSecret: 'YOUR APP SECRET HERE'
    , redirectPath: '/home'
    , findOrCreateUser: function (session, accessToken, fbUserMetadata) {
      }
  }
}

For help on the undefined req.user, file an issue on github with a link to a self-contained gist to re-create the issue, and I can help you out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文