16

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?

georgedyer
  • 2,737
  • 1
  • 21
  • 25

3 Answers3

23

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);
Jimmy Knoot
  • 2,378
  • 20
  • 29
3

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

John
  • 3,866
  • 6
  • 33
  • 37
  • How can you call this from client side? – Gobliins Jun 28 '18 at 14:38
  • Idk, this answer is 5 years old so I assume it's extremely out of date. – John Jul 11 '18 at 00:45
  • Answer is still correct. Code hasn't changed that much. ATM i use, `Meteor.methods` to call this function. Since i didn't find any Client sided API except the login function. – Gobliins Jul 12 '18 at 07:35
2

it's 2015 - use one of these packages:

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

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

krivar
  • 342
  • 1
  • 3
  • 16