Search code examples
reactjsauthenticationsocket.iofeathersjs

Feathersjs: Switching to sockets instead of REST, using Express Middleware with Sockets


So I'm following Feathersjs docs regarding authentication,
and I have a middleware /signup,
from User Management docs:

module.exports = function(app) {
  return function(req, res, next) {
    const body = req.body;   
    app.service('users').create({
      email: body.email,
      password: body.password
    })
    // Then redirect to the login page
    .then(user => res.redirect('/login.html'))//this will be a redirect in my client not in the server      
    .catch(next);
  };
};

Now in src/middleware/index.js: I have :

module.exports = function() {
  const app = this;

  app.post('/signup', signup(app));// how can I reimplement this with sockets
  app.use(notFound());
  app.use(logger(app));
  app.use(handler());
};

Using REST was easy :

request.post(`${SERVER}/signup`)
      .send({ email: username, password: password })
      .then(data=>{console.log(`data comming from response`,data)})
      .catch(error=>{console.log(`ERROR comming from response`,error)})  

so the problem is that now that I'm using sockets (feathers-client) I don't know how to tell feathers client to "post" the email/pass to that /signup middleware. Is there any way to achieve this?
this is my client conf:

import feathers from 'feathers-client';    
const io = require('socket.io-client');
var socket = io(SERVER);


let feathersClient = 
  feathers()
    .configure(feathers.socketio(socket))
    .configure(feathers.hooks())
    .configure(feathers.authentication({
      storage: window.localStorage
    }));

Solution

  • You don't need the signup middleware. Just create a new user through the /users service on your client like this:

    import feathers from 'feathers-client';    
    const io = require('socket.io-client');
    var socket = io(SERVER);
    
    
    let feathersClient = 
      feathers()
        .configure(feathers.socketio(socket))
        .configure(feathers.hooks())
        .configure(feathers.authentication({
          storage: window.localStorage
        }));
    
    feathersClient.service('users').create({
      email: 'test@example.com',
      password: 'testing'
    });
    

    Then you will be able to authenticate the user like this:

    feathersClient.authenticate({
      type: 'local',
      email: 'test@example.com',
      password: 'testing'
    });