Thank you! Your feedback has been delivered
Thank you! Your feedback has been sent

Creating a non-oauth Meteor Accounts package

The accounts-foobar packages all rely on oauth-based authentication systems, and involve quite a bit of confusing, undocumented boilerplate. I'm wondering if there's an easy way to create a package for a non-oauth authentication system that will integrate normally with the Meteor accounts system. The non-oauth system I have in mind is a simple external API that just accepts a username and password, and returns a simple json object with success data or an error. Is there a simple way to do this? I'm looking for a package like this on github, or a code example.

EDIT: This bit works fine, I'm just not entirely sure how to hook this up to the accounts system:

HTTP.post('https://my-site.com/api/login.json', { params: { username: 'foo', password: 'bar' } }, function (error, result) {
    if (result) console.log('User data:', result.data);
});

// User data: { userId: 217, username: "foobar" }

Conner: Thanks for the answer! Just to clarify, I know about the HTTP class. The auth API I'd like to use is actually an external API for a service unrelated to mine. My issue isn't with making a package (I've made a number of them), it's with adding another accounts option that uses the simple authentication API I've mentioned above, because so far I've only seen accounts services that are based on oauth.

User Gravatar

BenjaminRH

Posted Nov 18 2013 6:36 UTC

$10



2 Replies


Benjamin, it depend greatly on the API but that should be extremely easy to implement. I agree the the documentation needs improvement. If your API uses HTTP the please see the Meteor HTTP documentation. I would start by trying to set up your account system outside of a package in a basic app. After you have that working you can then move the code into a package.

User Gravatar

Conner

Posted Nov 18 2013 6:48 UTC

Add accounts-base package: meteor add accounts-base

On the server, add a Accounts.registerLoginHandler call

Accounts.registerLoginHandler(function(loginRequest) {
  // ... check loginRequest for proper credentials - up to you 
  var userId = something; // determined by the credentials check

  // add a resume token so that Meteor can resume your session on reconnect
  var stampedToken = Accounts._generateStampedLoginToken();
  Meteor.users.update(session.user, 
    {$push: {'services.resume.loginTokens': stampedToken}});

  return {
    id: userId,
    token: stampedToken.token
  });

Call login on the client:

var loginRequest = { ... your auth credentials ... };
Accounts.callLoginMethod({
  methodArguments: [loginRequest],
  userCallback: function (err) {
      if (!err) {  ... do stuff here on successful login ...  }
  }});

That's it!

User Gravatar

BenjaminRH

Posted Nov 19 2013 8:54 UTC

Solution

This didn't solve your task? Get your own custom solution.

Add a reply

By posting a reply on CodersClan you agree to our Terms & Conditions