Skip to content
This repository was archived by the owner on Mar 22, 2022. It is now read-only.

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

Closed
ghost opened this issue Apr 10, 2017 · 11 comments
Closed

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

ghost opened this issue Apr 10, 2017 · 11 comments

Comments

@ghost
Copy link

ghost 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
Copy link
Member

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

@ghost
Copy link
Author

ghost commented Apr 11, 2017 via email

@marshallswain
Copy link
Member

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

@ghost
Copy link
Author

ghost 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
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-ecosystem/authentication-client#38. I don't think it would be a difficult PR if you want to give it a shot. ;)

@ghost
Copy link
Author

ghost commented Apr 11, 2017

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

@ekryski
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 as completed Apr 11, 2017
@ekryski
Copy link
Member

ekryski commented Apr 11, 2017

@prime-ralph I also added a comment here: feathersjs-ecosystem/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?

@ghost
Copy link
Author

ghost 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
Copy link
Member

marshallswain commented Apr 11, 2017

Try this:

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

@ghost
Copy link
Author

ghost 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.

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants