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

How to make a request to an Endpoint that requires authentication from nodejs? #523

Closed
webnugget opened this Issue Jun 2, 2017 · 10 comments

Comments

Projects
None yet
5 participants
@webnugget
Copy link

webnugget commented Jun 2, 2017

Hi Guys i'm trying to use a service that requires authentication from a nodejs client that updates some data. How do I authenticate my node client?

const feathers = require('feathers/client')
const socketio = require('feathers-socketio/client');
const hooks = require('feathers-hooks');
const io = require('socket.io-client');

const socket = io('http://localhost:3030');
const app = feathers()
    .configure(hooks())
    .configure(socketio(socket));


const testService = app.service('test');

testService.on('created', message => console.log('Created a test', message));

// Use the messages service from the server
testService.create({
    some:'message'
});

If the testservice does not require authentication this works as expected.

Thanks for your help.

@daffl

This comment has been minimized.

Copy link
Member

daffl commented Jun 7, 2017

The authentication client API is documented here, and the exact same as going through the browser except that you will have to provide your own token storage (e.g. something like node-localstorage).

@webnugget

This comment has been minimized.

Copy link
Author

webnugget commented Jun 9, 2017

thanks for your help, didn't know about node-localstorage. looks promising, i'll give it a try.

@daffl

This comment has been minimized.

Copy link
Member

daffl commented Jun 9, 2017

Cool. I will close this issue then but feel free to reopen if you run into any problems.

@daffl daffl closed this Jun 9, 2017

@amaurymartiny

This comment has been minimized.

Copy link

amaurymartiny commented Jun 16, 2017

What if I don't use feathers-authentication-client? Is there a way to put the accessToken somewhere in the socket connection? Related to #517.

@marshallswain

This comment has been minimized.

Copy link
Member

marshallswain commented Jun 22, 2017

@amaurymartiny I don't know of any way of doing this in the Socket.io-client API. I think you would have to store the token somewhere like localStorage and override the methods as described here: https://stackoverflow.com/questions/8832414/overriding-socket-ios-emit-and-on

@amaurymartiny

This comment has been minimized.

Copy link

amaurymartiny commented Jun 22, 2017

@marshallswain I actually figured it out, by looking in the frames of the socket connection inside the Chrome network console.

Basically, when the socket connection is on, the client sends:

socket.emit('authenticate', { strategy: 'jwt', accessToken: '{{token}}'});

The server sends a response with the accessToken if the token was correct.

And next step, you can use your socket to emit any CRUD operation on any service:

socket.emit('messages::create', { text: 'Hello' });
@daffl

This comment has been minimized.

Copy link
Member

daffl commented Jun 22, 2017

We should probably add this to the documentation. There is some for how to do it with REST already.

@amaurymartiny

This comment has been minimized.

Copy link

amaurymartiny commented Jun 22, 2017

@daffl I can do that. To be sure, it's here https://docs.feathersjs.com/api/socketio.html#direct-connection right?

@daffl

This comment has been minimized.

Copy link
Member

daffl commented Jun 22, 2017

I'm not sure actually. Maybe in https://docs.feathersjs.com/api/authentication/local.html#direct-usage (or the other according strategy).

@TimNZ

This comment has been minimized.

Copy link

TimNZ commented Jan 25, 2018

For anyone who comes across this, or looks at the direct-usage section in the docs, I recommend using authentication-client instead of direct authentication, as the authentication-client will reconnect automatically if the connection is lost.

Use node-localstorage for access token saving, or provide your own wrapper.

This is my shared client code for a node service that consumes services from a feathers service.

const path = require('path');
const config = require(path.join(process.cwd(), 'config', 'config.json'));
const io = require('socket.io-client');
const feathers = require('@feathersjs/client');
const socketio = require('@feathersjs/socketio-client');
const authentication = require('@feathersjs/authentication-client');
const socket = io(config.apiServer.socketUrl);
const client = feathers();
const debug = require('debug')('account-guardian:mailsender');
const LocalStorage = require('node-localstorage').LocalStorage;
const localStorage = new LocalStorage('./storage');
client.configure(socketio(socket));
client.configure(authentication({storage: localStorage}));

client
    .authenticate({strategy: 'local', 'email': config.apiServer.credentials.email, 'password': config.apiServer.credentials.password})
    .catch(error => {
        debug('Unable to authenticate to server');
        process.exit(1);
    });
client.on('authenticated',(login) => {
    debug('Authenticated to server')
})

module.exports = client;
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.