strongloop / loopback-example-passport

LoopBack example for facebook login
Other
185 stars 134 forks source link

Loopback 2.0 compatibility #5

Closed collinforrester closed 10 years ago

collinforrester commented 10 years ago

I'm working to add this to a 2.0 project and when I'm following the example code in this project, I'm getting the error:

Error: Model not found: UserCredential

Running with debug I get:

5 Aug 20:28:17 - [nodemon] v1.2.1
5 Aug 20:28:17 - [nodemon] to restart at any time, enter `rs`
5 Aug 20:28:17 - [nodemon] watching: *.*
5 Aug 20:28:17 - [nodemon] starting `node server/server.js`
  loopback:boot:executor Configuring existing model User +0ms
  loopback:boot:executor Configuring existing model AccessToken +5ms
  loopback:boot:executor Configuring existing model ACL +1ms
  loopback:boot:executor Configuring existing model RoleMapping +0ms
  loopback:boot:executor Configuring existing model Role +0ms
  loopback:boot:executor Creating new model user {"name":"user","plural":"users","base":"User","properties":{"accessTokens":{"type":"hasMany","model":"accessToken","foreignKey":"userId"},"identities":{"type":"hasMany","model":"userIdentity","foreignKey":"userId"},"credentials":{"type":"hasMany","model":"userCredential","foreignKey":"userId"}},"validations":[],"relations":{},"acls":[],"methods":[]} +1ms
  loopback:boot:executor Creating new model userCredential {"name":"userCredential","plural":"userCredentials","base":"UserCredential","properties":{},"validations":[],"relations":{"user":{"type":"belongsTo","model":"user","foreignKey":"userId"}},"acls":[],"methods":[]} +5ms

/Users/collin/code/backend/node_modules/loopback/lib/registry.js:202
  throw new Error('Model not found: ' + modelName);
        ^
Error: Model not found: UserCredential
    at Function.registry.getModel (/Users/collin/code/backend/node_modules/loopback/lib/registry.js:202:9)
    at Function.registry.createModel (/Users/collin/code/backend/node_modules/loopback/lib/registry.js:102:22)
    at /Users/collin/code/backend/node_modules/loopback-boot/lib/executor.js:153:28
    at Array.forEach (native)
    at defineModels (/Users/collin/code/backend/node_modules/loopback-boot/lib/executor.js:141:23)
    at setupModels (/Users/collin/code/backend/node_modules/loopback-boot/lib/executor.js:130:3)
    at execute (/Users/collin/code/backend/node_modules/loopback-boot/lib/executor.js:25:3)
    at bootLoopBackApp (/Users/collin/code/backend/node_modules/loopback-boot/index.js:79:3)
    at Object.<anonymous> (/Users/collin/code/backend/server/server.js:17:1)
    at Module._compile (module.js:456:26)
5 Aug 20:28:20 - [nodemon] app crashed - waiting for file changes before starting...

I was able to change the model definitions from the 1.x format to 2.0 but I'm guessing there is still something I'm missing as far as getting loopback to inherit the models that this project includes?

collinforrester commented 10 years ago

I'll also mention that I've updated my model-config.json to try pulling in the node_modules models directory:

"_meta": {
    "sources": [
      "../common/models",
      "./models",
      "./node_modules/loopback-component-passport/lib/models"
    ]
  }

But I'm guessing that doesn't matter since those models aren't defined in .json files like the 2.0 models are..

collinforrester commented 10 years ago

Well I've been able to get it to at least include the models without issue. The trick was to run

var loopbackPassport = require('loopback-component-passport');
var PassportConfigurator = loopbackPassport.PassportConfigurator;
var passportConfigurator = new PassportConfigurator(app);

before running the code in boot/ which makes sense now that I know what the problem was.

I'm still going to leave this open so I can find out what is causing a few other issues that I'm guessing is related to 1.x vs 2.0.

console.log(app.get('cookieSecret')); prints out undefined even those I have cookieSecret defined in server/config.json

I have my routes /auth/facebook set up in the same fashion that this example has it set up. I am successfully redirected to facebook where I am asked to link the app, when I get redirected back the calls to ensureLoggedIn() always fails. Any idea why?

Edit: sorry for the bombard of comments on this one issue. I'm more or less just updating it as I tinker around with it some more.

nowherenearithaca commented 10 years ago

I recently went through this as well in getting a 2.0 project to work with passport-3rd-party login/ensureloggedin - what error(s) are you seeing on the redirect back to your app?

raymondfeng commented 10 years ago

Can you share your project somewhere so that I can do some troubleshooting?

collinforrester commented 10 years ago

I'm not seeing any errors, ensuredLoggedIn() will constantly return false. I'm guessing its something wrong with my model set up.

@raymondfeng Here is a copy of my project - https://github.com/collinforrester/loopback-passport-issue Took me a bit, just created a demo/sample facebook app to go along with it.

collinforrester commented 10 years ago

I just noticed I don't have any of the push related models added to my project. Would that make a difference?

raymondfeng commented 10 years ago

The cookieSecret is set after boot(app, __dirname);

raymondfeng commented 10 years ago

I debugged https://github.com/collinforrester/loopback-passport-issue and it seemed that Express 4.x somehow doesn't work with passport session. The other steps are working well. I still have to figure out what's going on.

collinforrester commented 10 years ago

No luck after following this answer either: http://stackoverflow.com/questions/23171249/express-4-0-and-passport-redirect-to-facebook-not-working

@raymondfeng We're going to be building a mobile app. We'd like to authenticate users into our mobile app with facebook but its not really RESTful at that point is it? Or are sessions typically the only way to go about 3rd party auth with REST APIs regardless.

nowherenearithaca commented 10 years ago

I'm a little confused - when I run it and try to go to /auth/account it correctly recognizes that you're not logged in, and you have it immediately go to '/fail'. How is it supposed to be directed to try to talk to facebook? The original loopback/passport example has it redirect to a simple "login.html" page if you're not logged in, and that page includes the line (I stripped out html brackets)

a href="/auth/facebook" Login with Facebook

What happens when you type in the path /auth/facebook when you have it running? I don't have my facebook credentials handy atm.

Let me know if I'm missing something here.

raymondfeng commented 10 years ago

This repo https://github.com/collinforrester/loopback-passport-issue is missing some configuration for the static HTML files and EJS views. I had to port them from 1.x locally. I can share what I have so far if @collinforrester can grant me the permission.

gyss commented 10 years ago

I'm having similar problems trying to integrate loopback-passport with Loopback 2.0. I've tried this example successfully (previous version), but obviously it's not running with loopback 2.0. It'd be great an update of the examples and documentation's examples.

collinforrester commented 10 years ago

My example I just didn't copy over the ejs files. To reproduce the error I go to /auth/facebook which the first time there it redirects to facebook and auths the account. The second time there it will always fail at ensureLoggedIn and when it fails it redirects to /fail. If it was ever successful it would output the user object in json.

collinforrester commented 10 years ago

@raymondfeng You should have permission by now. Not sure if github notified you or not when I gave it to you yesterday.

collinforrester commented 10 years ago

I've found that if I use passport-facebook directly, it works fine. Still trying to narrow down the differences between my implementation and what loopback-component-passport adds and where the break down is.

clark0x commented 10 years ago

I don't see remoteMethod() call for UserIdentity.Login(), how do you access it remotely?

collinforrester commented 10 years ago

Not in the code I have posted. In another project I've dropped loopback-example-passport and the redirect back from Facebook works with all the session details intact.

raymondfeng commented 10 years ago

I pushed the working code into https://github.com/collinforrester/loopback-passport-issue. The main issue is the code you added to https://github.com/collinforrester/loopback-passport-issue/blob/f6e1ffc7304e48c82c3aecbf288af91fbbdb33fd/server/boot/root.js. It completely bypassed the middlewares required to restore the user object so that ensure-logged-in can pass.