Search code examples
cookiessocket.iojwtfeathersjs

FeathersJS socketio jwt authentication


I am attempting to add authentication to my app using feathersjs. I am having trouble understanding why my jwt token is not being sent even though I am receiving the jwt from the server.

Here is my angular code:

import {CookieStorage} from 'cookie-storage';
import {JwtHelperService} from '@auth0/angular-jwt';
import feathers from '@feathersjs/feathers';
import socketio from '@feathersjs/socketio-client';
import auth from '@feathersjs/authentication-client';
import {tap} from 'rxjs/operators';
import * as io from 'socket.io-client';

const socket = io('http://localhost:3030');
const cookieStorage = new CookieStorage();
const client = feathers();

client.configure(socketio(socket));
client.configure(auth({ storage: cookieStorage }));

socket.emit('authenticate', userData, function(message, data) {
  console.log(message); // message will be null
  console.log(data); // data will be {"accessToken": "your token"}

  // You can now send authenticated messages to the server
});

This code works and I get a jwt accessToken back to the browser but the cookieStorage part is not saving the cookie. If I switch socket.emit('authenticate') part out with the following code, it works:

client.authenticate(userData)

But, I want to use socketio for authentication and saving the jwt in a cookie instead of using the following method:

How do I get the socketio authenticate event to set the jwt in a cookie?


Solution

  • client.authenticate(userData) will use SocketIO for authentication (since you are already using client.configure(socketio(socket));) and store it in the storage you passed (which is cookieStorage in your case).