How to get authenticated user details in socket.io with passport.js ? Why user is undefined in socket.request.user ? #4470
-
My goal is to access the authenticated user details, who are connected with the socket server. I am using passport.js for authentication. and using google strategy. and using express-session for managing sessions. I used socket middleware to merge passport.js and socket.io sessions. by doing this I can get the session id when any user establishes a connection with the socket server. but the problem is when I am trying to access user details I am getting user is undefined. But I am getting user details through the rest APIs in the HTTP request from the browser.
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
Hi! Did you set import { io } from "socket.io-client";
const socket = io("https://my-backend.com", {
withCredentials: true
}); Reference: https://socket.io/docs/v4/client-options/#withcredentials You can also check: |
Beta Was this translation helpful? Give feedback.
-
thank you @darrachequesne is working |
Beta Was this translation helpful? Give feedback.
-
please is there any documentation which can help me achieve that using jwt BEARER tokens instead of express-sessions? i need help. |
Beta Was this translation helpful? Give feedback.
-
Thanks so much for the article
…On Tue, 11 Jun 2024, 11:28 am Damien Arrachequesne, < ***@***.***> wrote:
@Kvngstar <https://github.com/Kvngstar> please check our guide:
https://socket.io/how-to/use-with-jwt
—
Reply to this email directly, view it on GitHub
<#4470 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AV6QD5LVEINTFNTNYRPZCRDZG3GNJAVCNFSM6AAAAAAQNSVGAGVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TOMZXGQ4DI>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Thanks alot, this is exactly what i needed
…On Tue, Jun 11, 2024 at 11:28 AM Damien Arrachequesne < ***@***.***> wrote:
@Kvngstar <https://github.com/Kvngstar> please check our guide:
https://socket.io/how-to/use-with-jwt
—
Reply to this email directly, view it on GitHub
<#4470 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AV6QD5LVEINTFNTNYRPZCRDZG3GNJAVCNFSM6AAAAAAQNSVGAGVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TOMZXGQ4DI>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
Hi! Did you set
withCredentials
totrue
on the client-side, in order to include the cookies in the cross-site requests?Reference: https://socket.io/docs/v4/client-options/#withcredentials
You can also check: