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

Using feathers-authentication-client for an existing API? #478

Closed
ralphstodomingo opened this Issue Apr 10, 2017 · 11 comments

Comments

Projects
None yet
3 participants
@ralphstodomingo
Copy link

ralphstodomingo commented Apr 10, 2017

Steps to reproduce

Was doing this in a fresh project:

import feathers from 'feathers/client';
import superagent from 'superagent';
import hooks from 'feathers-hooks';
import client from 'feathers-rest/client';
import localstorage from 'localstorage-memory';
import auth from 'feathers-authentication-client';

const apiRest = client(process.env.API_ENDPOINT);
const api = feathers()
  .configure(hooks())
  .configure(auth({
    storage: localstorage,
    header: 'Authorization',
    path: process.env.AUTH_SERVICE,
    type: 'jwt',
    storageKey: 'token',
  }))
  .configure(apiRest.superagent(superagent));

api.authenticate({
  strategy: 'local',
  user_name: '...',
  password: '...',
})
.then((response) => {
  console.log('Authenticated!', response, api.get('token'));
  return api.passport.verifyJWT(response.token);
}).then((payload) => {
  console.log('JWT Payload', payload);
  return '';
});

Actual behavior

Instead of the behavior outlined in the docs, this results in the following:
api.get('token') returns undefined, and the localstorage token has a key of 'token', but with a value of [object Object].

I suspect this is because I'm trying to authenticate with a different API server (a Laravel jwt-auth one instead of another Feathers server) which returns, upon authentication, { token: '...'}.

At the console.log('Authenticated!', response, api.get('token')); part, the response prints out while the api.get() call returns undefined. If I try another request in that promise chain, I can also see that the token isn't being included in subsequent call headers.

System configuration

Tell us about the applicable parts of your setup.

Module versions (especially the part that's not working):
feathers@2.1.1
feathers-authentication-client@0.3.1
feathers-hooks@1.8.1
feathers-rest@1.7.1

NodeJS version:
v6.9.1

Operating System:
Windows 10

Browser Version:
Latest Chrome.

Module Loader:
Basic react-boilerplate stuff.
webpack@2.2.0-rc.3
webpack-dev-middleware@1.9.0
webpack-hot-middleware@2.15.0

@marshallswain

This comment has been minimized.

Copy link
Member

marshallswain commented Apr 10, 2017

What version of feathers-authentication is on the existing API?

@ralphstodomingo

This comment has been minimized.

Copy link
Author

ralphstodomingo commented Apr 11, 2017

@marshallswain

This comment has been minimized.

Copy link
Member

marshallswain commented Apr 11, 2017

It's not configurable right now. Here's the line that needs to be modified: https://github.com/feathersjs/feathers-authentication-client/blob/master/src/passport.js#L235

@ralphstodomingo

This comment has been minimized.

Copy link
Author

ralphstodomingo commented Apr 11, 2017

So the whole { token: '...' } is stored because there's no accessToken field?
Thanks for the heads-up. Might consider adding a custom authentication route on our API for this.

@marshallswain

This comment has been minimized.

Copy link
Member

marshallswain commented Apr 11, 2017

We really ought to allow this to be customized. I created an issue for it here: feathersjs/authentication-client#38. I don't think it would be a difficult PR if you want to give it a shot. ;)

@ralphstodomingo

This comment has been minimized.

Copy link
Author

ralphstodomingo commented Apr 11, 2017

Well, since you've brought it up, might as well try. :)

@ekryski

This comment has been minimized.

Copy link
Member

ekryski commented Apr 11, 2017

Ok, going to close this in favour of tracking in the auth-client issue.

@ekryski ekryski closed this Apr 11, 2017

@ekryski

This comment has been minimized.

Copy link
Member

ekryski commented Apr 11, 2017

@prime-ralph I also added a comment here: feathersjs/authentication-client#38 (comment)

TL;DR could you not use a hook on the client to transform to and from the key name that you want?

@ralphstodomingo

This comment has been minimized.

Copy link
Author

ralphstodomingo commented Apr 11, 2017

A question: from what I gather, you can only use hooks on services, but since I'm using the authenticate function, is there a way to bind a hook on that?

@marshallswain

This comment has been minimized.

Copy link
Member

marshallswain commented Apr 11, 2017

Try this:

app.service('authentication').hooks(`...`)
@ralphstodomingo

This comment has been minimized.

Copy link
Author

ralphstodomingo commented Apr 11, 2017

After tuning, here's what I ended up using, for those who end up with the same situation:

api.service('authenticate').hooks({
  after: {
    all(hook) {
      Object.assign(hook.result, { accessToken: `Bearer ${hook.result.token}` });
      return Promise.resolve(hook);
    },
  },
});

The Bearer prefix was added because apparently our API requires the header format of Authorization: Bearer ....
Not sure if that's the cleanest I could do, just following the linter.

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.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.