strongloop / loopback-component-oauth2

oAuth 2.0 server for LoopBack
http://www.strongloop.com
Other
62 stars 63 forks source link

dataSource.idName null #19

Closed mvaullerin closed 7 years ago

mvaullerin commented 9 years ago

Hi guys,

I've tried to install oauth2 component on a fresh install of loopback and I got this error :

/home/rlocal/pm3-auth/node_modules/loopback-datasource-juggler/lib/relation-definition.js:1137
    idName = modelTo.dataSource.idName(modelTo.modelName) || 'id';
                                ^
TypeError: Cannot call method 'idName' of null
    at Function.RelationDefinition.belongsTo (/home/rlocal/pm3-auth/node_modules/loopback-datasource-juggler/lib/relation-definition.js:1137:33)
    at Function.RelationMixin.belongsTo (/home/rlocal/pm3-auth/node_modules/loopback-datasource-juggler/lib/relations.js:123:29)
    at module.exports (/home/rlocal/pm3-auth/node_modules/loopback-component-oauth2/lib/models/index.js:26:19)
    at Function.module.exports (/home/rlocal/pm3-auth/node_modules/loopback-component-oauth2/lib/oauth2-loopback.js:201:16)
    at /home/rlocal/pm3-auth/server/server.js:30:8
    at /home/rlocal/pm3-auth/node_modules/loopback-boot/lib/executor.js:53:7
    at /home/rlocal/pm3-auth/node_modules/loopback-boot/node_modules/async/lib/async.js:254:17
    at /home/rlocal/pm3-auth/node_modules/loopback-boot/node_modules/async/lib/async.js:157:25
    at /home/rlocal/pm3-auth/node_modules/loopback-boot/node_modules/async/lib/async.js:251:21
    at /home/rlocal/pm3-auth/node_modules/loopback-boot/node_modules/async/lib/async.js:615:34

I just add the following code in the callback of boot in server.js (+require at the begining):

  var options = { 
  dataSource: app.dataSources.db, // Data source for oAuth2 metadata persistence
  loginPage: '/login', // The login page url
  loginPath: '/login' // The login form processing url
};

oauth2.oAuth2Provider(
  app, // The app instance
  options // The options
);

Any idea ?

Thanks in advance

raymondfeng commented 9 years ago

Can you show me the code in server/server.js? It seems that the User model hasn't been attached to a data source yet.

mvaullerin commented 9 years ago
var loopback = require('loopback');
var boot = require('loopback-boot');
var oauth2 = require('loopback-component-oauth2');

var app = module.exports = loopback();

app.start = function() {
  // start the web server
  return app.listen(function() {
    app.emit('started');
    console.log('Web server listening at: %s', app.get('url'));
  });
};

// Bootstrap the application, configure models, datasources and middleware.
// Sub-apps like REST API are mounted via boot scripts.
boot(app, __dirname, function(err) {
  if (err) throw err;

    var options = { 
        dataSource: app.dataSources.db, // Data source for oAuth2 metadata persistence
        loginPage: '/login', // The login page url
        loginPath: '/login' // The login form processing url
    };

    oauth2.oAuth2Provider(
      app, // The app instance
      options // The options
    );

  // start the server if `$ node server.js`
  if (require.main === module)
    app.start();
});
richardpringle commented 8 years ago

@KCypher13, I see this is an old issue, but I'm just wondering if you got it resolved or not?

richardpringle commented 8 years ago

@KCypher13, I'm going to close this issue. Please re-open and mention me in a comment if the issue persists.

Samox commented 8 years ago

Hello @richardpringle ! I'm having the same problem right now, any ideas ? Thanks

richardpringle commented 8 years ago

Hey @Samox, best thing to do is to fork the loopback-sandbox and reproduce the issue in there so that I can clone and take a look myself.

I'll re-open the issue for now since you are still running into it.

deviant32 commented 7 years ago

I am having the same issue...

deviant32 commented 7 years ago

This OAuth2 implementation shouldn't be in the LB 3.0 docs if no one is going to support it.

https://loopback.io/doc/en/lb3/OAuth-2.0.html

deviant32 commented 7 years ago

This problem is caused when you either didn't use the User model or at least extend the User model. As soon as I had my Client model extend User it started to work.

richardpringle commented 7 years ago

@deviant32, you can see the dependency here: https://github.com/strongloop/loopback-component-oauth2/blob/master/lib/models/index.js#L26

deviant32 commented 7 years ago

@richardpringle Why wouldn't this be in the documentation?

richardpringle commented 7 years ago

@deviant32, not sure, I don't actually work on this project anymore. @crandmck, looks like this is an issue for you (and if not, I'm guessing you'll know where to assign it).

crandmck commented 7 years ago

AFAIK, OAuth component is still supported, but I need to confirm.

crandmck commented 7 years ago

Confirmed this component is still supported.

... either didn't use the User model or at least extend the User model. As soon as I had my Client model extend User it started to work.

So, docs should just say that to use this component, the app must have a User model or model that extends User?

richardpringle commented 7 years ago

Yeah @crandmck, I would just add that somewhere as a disclaimer.

deviant32 commented 7 years ago

@crandmck It will work if one of your models is already extending the User model. If you do not have any models extending User, then you have to specify in the options what model to set as the User model.

It worked once I did this, however I am not stuck on another problem. I cannot generate any access tokens when I make a request to /oauth/authorize.

crandmck commented 7 years ago

OK, thanks. I created a separate doc issue. As I delved into it, I also found additional options that may need to be documented.

stale[bot] commented 7 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 7 years ago

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.