首页 > 解决方案 > FeathersJS socketio jwt 身份验证

问题描述

我正在尝试使用feathersjs. jwt即使我jwt从服务器接收到令牌,我也无法理解为什么我的令牌没有被发送。

这是我的angular代码:

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
});

此代码有效,我jwt accessToken返回浏览器,但该cookieStorage部分未保存 cookie。socket.emit('authenticate')如果我使用以下代码切换部分,它可以工作:

client.authenticate(userData)

但是,我想socketio用于身份验证并将其保存jwt在 cookie 中,而不是使用以下方法:

如何获取socketio身份验证事件以jwt在 cookie 中设置?

标签: cookiessocket.iojwtfeathersjs

解决方案


client.authenticate(userData) 使用 SocketIO 进行身份验证(因为您已经在使用client.configure(socketio(socket));)并将其存储在您传递的存储中(cookieStorage在您的情况下)。


推荐阅读