cookies - 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 中设置?
解决方案
client.authenticate(userData)
将使用 SocketIO 进行身份验证(因为您已经在使用client.configure(socketio(socket));
)并将其存储在您传递的存储中(cookieStorage
在您的情况下)。