How to generate new Meteor login tokens (server side) in order to make a quick login link

半腔热情 提交于 2019-12-31 10:40:35

问题


Meteor has a loginWithToken method, and there are resume tokens in the user object. So one can login using one of these tokens with loginWithToken. That works.

Is there a way to generate new login tokens, or should I just use the resume tokens to create a quick login link?


回答1:


As Johnny said, you can use the Accounts._generateStampedLoginToken() function, which is actually nothing special, just the following function:

_generateStampedLoginToken = function () {
  return {
    token: Random.secret(),
    when: new Date
  };
}

anyway, to use it, here is an example:

// Server //

// Creates a stamped login token
var stampedLoginToken = Accounts._generateStampedLoginToken();

/**
 * Hashes the stamped login token and inserts the stamped login token 
 * to the user with the id specified, adds it to the field 
 * services.resume.loginTokens.$.hashedToken. 
 * (you can use Accounts._hashLoginToken(stampedLoginToken.token) 
 * to get the same token that gets inserted)
 */
Accounts._insertLoginToken(user._id, stampedLoginToken);


// Client //

// Login with the stamped loginToken's token
Meteor.loginWithToken(stampedLoginToken.token);



回答2:


Yes, you can generate new tokens by calling Accounts._generateStampedLoginToken(). You can call it from within a login handler.

https://github.com/meteor/meteor/blob/master/packages/accounts-base/accounts_server.js#L114




回答3:


it's 2015 - use one of these packages:

  • poetic:accounts-passwordless
  • acemtp:accounts-passwordless

http://fastosphere.meteor.com/?q=passwordless



来源:https://stackoverflow.com/questions/15684634/how-to-generate-new-meteor-login-tokens-server-side-in-order-to-make-a-quick-l

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!