Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST auth/local not working if socketio() not set #72

Closed
fastlorenzo opened this Issue Feb 22, 2016 · 6 comments

Comments

Projects
None yet
3 participants
@fastlorenzo
Copy link
Contributor

fastlorenzo commented Feb 22, 2016

I encountered the following issue while trying to work with multiple feathers instances :

The following configuration throws an error if I try to POST to /auth/local with username and password :

var app = feathers()
    .configure(rest())
    .configure(hooks())
    // .configure(socketio())
    .use(bodyParser.json())
    .use(bodyParser.urlencoded({ extended: true }))
    .configure(authentication({
      setUpSuccessRedirect: false,
      setUpFailureRedirect: false,
      idField: 'id',
      userEndpoint: serviceEndpoint + SERVICE_ENDPOINT_USER,
      token: {
        secret: JWT_SECRET,
        tokenEndpoint: serviceEndpoint + SERVICE_ENDPOINT_AUTH_TOKEN,
        userEndpoint: serviceEndpoint + SERVICE_ENDPOINT_USER,
        jwtOptions: {
          expiresIn: JWT_EXPIRES_IN
        },
      },
      local: {
        type: 'local',
        usernameField: 'username',
        userEndpoint: serviceEndpoint + SERVICE_ENDPOINT_USER,
        localEndpoint: serviceEndpoint + SERVICE_ENDPOINT_AUTH_LOCAL,
        successRedirect: serviceEndpoint + SERVICE_ENDPOINT_AUTH_SUCCESS,
        tokenEndpoint: serviceEndpoint + SERVICE_ENDPOINT_AUTH_TOKEN,
        jwtOptions: {
          expiresIn: JWT_EXPIRES_IN
        },
      },
      localEndpoint: serviceEndpoint + SERVICE_ENDPOINT_AUTH_LOCAL,
      tokenEndpoint: serviceEndpoint + SERVICE_ENDPOINT_AUTH_TOKEN
    }));

var feathersapp = feathers().use('/api/v1/rest', app);

Error :

TypeError: Cannot read property 'service' of undefined
<br> &nbsp; &nbsp;at Object.checkCredentials (/root/RAF/api/node_modules/feathers-authentication/lib/services/local/index.js:80:15)
<br> &nbsp; &nbsp;at Strategy.authenticate (/root/RAF/api/node_modules/passport-local/lib/strategy.js:90:12)
<br> &nbsp; &nbsp;at attempt (/root/RAF/api/node_modules/passport/lib/middleware/authenticate.js:348:16)
<br> &nbsp; &nbsp;at authenticate (/root/RAF/api/node_modules/passport/lib/middleware/authenticate.js:349:7)
<br> &nbsp; &nbsp;at /root/RAF/api/node_modules/feathers-authentication/lib/services/local/index.js:137:9
<br> &nbsp; &nbsp;at new Promise (/root/RAF/api/node_modules/core-js/modules/es6.promise.js:197:7)
<br> &nbsp; &nbsp;at Object.create (/root/RAF/api/node_modules/feathers-authentication/lib/services/local/index.js:116:14)
<br> &nbsp; &nbsp;at Object.wrapper (/root/RAF/api/node_modules/feathers/lib/mixins/promise.js:30:28)
<br> &nbsp; &nbsp;at Object.&lt;anonymous&gt; (/root/RAF/api/node_modules/uberproto/lib/proto.js:30:17)
<br> &nbsp; &nbsp;at Object.wrapped (/root/RAF/api/node_modules/rubberduck/lib/rubberduck.js:63:19)
<br> &nbsp; &nbsp;at /root/RAF/api/node_modules/feathers-hooks/lib/before.js:53:20
<br> &nbsp; &nbsp;at new Promise (/root/RAF/api/node_modules/core-js/modules/es6.promise.js:197:7)
<br> &nbsp; &nbsp;at /root/RAF/api/node_modules/feathers-hooks/lib/before.js:40:18
<br> &nbsp; &nbsp;at run (/root/RAF/api/node_modules/core-js/modules/es6.promise.js:104:47)
<br> &nbsp; &nbsp;at /root/RAF/api/node_modules/core-js/modules/es6.promise.js:115:28
<br> &nbsp; &nbsp;at flush (/root/RAF/api/node_modules/core-js/modules/$.microtask.js:19:5)

If I uncomment the following line to use socketio it works :
// .configure(socketio())

I think the app variable is not available in services/local/index.js if you don't setup socketio in feathers.

@daffl

This comment has been minimized.

Copy link
Member

daffl commented Feb 22, 2016

Good catch that it works only with the Socket.io provider! This actually looks like a bug either in Feathers core or the Socket.io provider messing with the original setup. I would've expected it to be the other way around but I'll investigate some more.

@ekryski

This comment has been minimized.

Copy link
Member

ekryski commented Feb 22, 2016

Yes thanks @fastlorenzo! As an aside, if/when you upgrade to the latest version of feathers-authentication you shouldn't have to duplicate all your endpoint configs. They cascade down now. So top level options automatically get passed to the individual auth services. See the authentication docs.

@daffl

This comment has been minimized.

Copy link
Member

daffl commented Feb 22, 2016

So the explanation is actually fairly simple. Usually when using it as a sub-app you have to call app.setup(server) yourself.

When mounted as a sub-app with websockets configured however, they will override their parent setup (see https://github.com/feathersjs/feathers-socket-commons/blob/master/src/index.js#L59) so that you don't have to do that.

This should probably be the expected behaviour of core in general but it might be tricky to change without breaking things (if we add it to core and you have an older websocket library it'll call setup twice).

Your case can be fixed like this:

var feathersapp = feathers().use('/api/v1/rest', app);

var server = feathersapp.listen();

app.setup(server);
@fastlorenzo

This comment has been minimized.

Copy link
Contributor Author

fastlorenzo commented Feb 22, 2016

@daffl thanks, that fixes my issue !
Maybe you should document it somewhere to avoid people having that issue again 👍

@ekryski thanks for the info, I'll update my configs :)

@ekryski

This comment has been minimized.

Copy link
Member

ekryski commented Feb 22, 2016

@daffl we should create an issue somewhere to track this. I'm not even sure where we'd put it in the docs.

@daffl

This comment has been minimized.

Copy link
Member

daffl commented Feb 23, 2016

Documentation has been added via feathersjs/docs@b25787e and can be found in the Middleware -> SSL, Virtual Hosts and sub-apps chapter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.