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?
cookies socket.io jwt feathersjs
add a comment |
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?
cookies socket.io jwt feathersjs
add a comment |
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?
cookies socket.io jwt feathersjs
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?
cookies socket.io jwt feathersjs
cookies socket.io jwt feathersjs
asked Nov 15 '18 at 18:35
MooseMoose
7111924
7111924
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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).
thanks for clearing that up. I am having difficulty understanding the proper way to use thesocket.emit('authenticate')version of the authentication scheme. Also, when i use theclient.authenticate(userData)method, it seems that the cookie name is not getting picked up fromdefault.config(I changed it to a custom name under thecookiesection of the config), instead I am getting a cookie namedfeathers-jwt, do you know why this is happening?
– Moose
Nov 15 '18 at 21:51
If you are using the Feathers client you won't have to use the socket directly. Sending the event only applies when using Socket.io directly. As documented here you will have to set thestorageKeyoption inclient.configure(auth())to what you need for the key.
– Daff
Nov 15 '18 at 23:17
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325890%2ffeathersjs-socketio-jwt-authentication%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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).
thanks for clearing that up. I am having difficulty understanding the proper way to use thesocket.emit('authenticate')version of the authentication scheme. Also, when i use theclient.authenticate(userData)method, it seems that the cookie name is not getting picked up fromdefault.config(I changed it to a custom name under thecookiesection of the config), instead I am getting a cookie namedfeathers-jwt, do you know why this is happening?
– Moose
Nov 15 '18 at 21:51
If you are using the Feathers client you won't have to use the socket directly. Sending the event only applies when using Socket.io directly. As documented here you will have to set thestorageKeyoption inclient.configure(auth())to what you need for the key.
– Daff
Nov 15 '18 at 23:17
add a comment |
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).
thanks for clearing that up. I am having difficulty understanding the proper way to use thesocket.emit('authenticate')version of the authentication scheme. Also, when i use theclient.authenticate(userData)method, it seems that the cookie name is not getting picked up fromdefault.config(I changed it to a custom name under thecookiesection of the config), instead I am getting a cookie namedfeathers-jwt, do you know why this is happening?
– Moose
Nov 15 '18 at 21:51
If you are using the Feathers client you won't have to use the socket directly. Sending the event only applies when using Socket.io directly. As documented here you will have to set thestorageKeyoption inclient.configure(auth())to what you need for the key.
– Daff
Nov 15 '18 at 23:17
add a comment |
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).
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).
answered Nov 15 '18 at 21:08
DaffDaff
36.5k783103
36.5k783103
thanks for clearing that up. I am having difficulty understanding the proper way to use thesocket.emit('authenticate')version of the authentication scheme. Also, when i use theclient.authenticate(userData)method, it seems that the cookie name is not getting picked up fromdefault.config(I changed it to a custom name under thecookiesection of the config), instead I am getting a cookie namedfeathers-jwt, do you know why this is happening?
– Moose
Nov 15 '18 at 21:51
If you are using the Feathers client you won't have to use the socket directly. Sending the event only applies when using Socket.io directly. As documented here you will have to set thestorageKeyoption inclient.configure(auth())to what you need for the key.
– Daff
Nov 15 '18 at 23:17
add a comment |
thanks for clearing that up. I am having difficulty understanding the proper way to use thesocket.emit('authenticate')version of the authentication scheme. Also, when i use theclient.authenticate(userData)method, it seems that the cookie name is not getting picked up fromdefault.config(I changed it to a custom name under thecookiesection of the config), instead I am getting a cookie namedfeathers-jwt, do you know why this is happening?
– Moose
Nov 15 '18 at 21:51
If you are using the Feathers client you won't have to use the socket directly. Sending the event only applies when using Socket.io directly. As documented here you will have to set thestorageKeyoption inclient.configure(auth())to what you need for the key.
– Daff
Nov 15 '18 at 23:17
thanks for clearing that up. I am having difficulty understanding the proper way to use the
socket.emit('authenticate') version of the authentication scheme. Also, when i use the client.authenticate(userData) method, it seems that the cookie name is not getting picked up from default.config (I changed it to a custom name under the cookie section of the config), instead I am getting a cookie named feathers-jwt, do you know why this is happening?– Moose
Nov 15 '18 at 21:51
thanks for clearing that up. I am having difficulty understanding the proper way to use the
socket.emit('authenticate') version of the authentication scheme. Also, when i use the client.authenticate(userData) method, it seems that the cookie name is not getting picked up from default.config (I changed it to a custom name under the cookie section of the config), instead I am getting a cookie named feathers-jwt, do you know why this is happening?– Moose
Nov 15 '18 at 21:51
If you are using the Feathers client you won't have to use the socket directly. Sending the event only applies when using Socket.io directly. As documented here you will have to set the
storageKey option in client.configure(auth()) to what you need for the key.– Daff
Nov 15 '18 at 23:17
If you are using the Feathers client you won't have to use the socket directly. Sending the event only applies when using Socket.io directly. As documented here you will have to set the
storageKey option in client.configure(auth()) to what you need for the key.– Daff
Nov 15 '18 at 23:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325890%2ffeathersjs-socketio-jwt-authentication%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown