nikhil.g777
nikhil.g777

Reputation: 904

unable to implement ouath2 on loopback

I want to implement an oauth2 server using loopback. I have previously done it using express and it works perfectly, but using loopback i am having some issues . Here is the code for oauth2orize : var app = require('../../server/server'); //require server.js as in any node.js app

var User = app.models.user;
var Client = app.models.client;
var Token = app.models.token;
var Code = app.models.code;

var server = oauth2orize.createServer();    

// Register serialialization function
server.serializeClient(function(client, callback) {
  return callback(null, client._id);
});

// Register deserialization function
server.deserializeClient(function(id, callback) {
  Client.findOne({ _id: id }, function (err, client) {
    if (err) { return callback(err); }
    return callback(null, client);
  });
});
// User authorization endpoint
exports.authorization = [  
  server.authorization(function(clientId, redirectUri, callback) {
    console.log('At Authorization endpoint');
    var Client = app.models.client;
    Client.findOne({where :{ appId: clientId }}, function (err, client) {
      if (err) { return callback(err); }
      console.log("Found client : "+client);
      return callback(null, client, redirectUri);
    });
  })]

When i call the authorisation endpoint i am getting an error : Error: Failed to serialize client. Register serialization function using serializeClient() However i have implemented the server.serializeClient and server.deserializeClient methods

Upvotes: 1

Views: 167

Answers (1)

KyleFarris
KyleFarris

Reputation: 17538

I came across the same problem recently and figured out the issue (in my case anyway). The issue was that my client model didn't have an id field. I had a clientId field but it seems the oauth2orize module specifically requires an id field as well. Kinda lame but not hard to implement.

Upvotes: 2

Related Questions