socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

authToken = null in springboot despite set by the client

Open deepto123 opened this issue 1 year ago • 2 comments

Describe the bug Im setting up the server in my springboot application and nodejs application (not simaltaneously) both and my client in nodejs. When I set the auth token on my client(in node application) and server is also in node it works fine and the authToken object is found in the handshake but if I run the server in my springboot application, authToken object is null.

To Reproduce

Please fill the following code example:

Server

socketIOServer.addConnectListener(new ConnectListener() {
            @Override
            public void onConnect(SocketIOClient client) {
                Object jwtToken = client.getHandshakeData().getAuthToken();  // null here
            }
}

Socket.IO client version: x.y.z

Client

const io = require('socket.io-client');

// Connect to the server with custom headers
const socket = io('http://localhost:8081', {
    auth: {token: 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0LGpvaG5kb2VAZXhhbXBsZS5jb20iLCJpYX' },
});


// Handle connection event
socket.on('connect', () => {
  console.log('Connected to server');
});

// Handle custom events from the server
socket.on('message', (data) => {
  console.log('Received message from server:', data);
});

// Emit custom events to the server
socket.emit('chatMessage', 'Hello, server!');

Expected behavior Expect springboot tocatch the authToken in handshake

Platform: -MacOs

Additional context Query params when set are being properly found in url params of the handshake, but authtoken isnt

deepto123 avatar Mar 12 '24 15:03 deepto123

Hi! The auth payload may not be supported by your server. Which version are you using?

darrachequesne avatar Mar 25 '24 06:03 darrachequesne

Hi! The auth payload may not be supported by your server. Which version are you using?

Hello, personally am using react native client, and node js server, but I can access both the query or even the auth I send.

socket.io version 4.8.1 socket.io-client version 4.8.1 React native version 0.76.7

client:

`
const {user, token} = useAuth(); const [socket, setSocket] = useState(null); const socketRef = useRef(null);

useEffect(() => {
    if (user) {
        console.log("user available", user);
        if (!socketRef.current) {
            socketRef.current = io(SOCKET_URL, {
                query: {
                    userId: user.id
                },
                auth: {
                    token: token
                }
            });

            setSocket(socketRef.current);
        }

        return () => {
            socketRef.current?.close();
            socketRef.current = null;
            setSocket(null);
        };
    } else {
        if (socketRef.current) {
            socketRef.current.close();
            socketRef.current = null;
            setSocket(null);
        }
    }
}, [user]);

`

Server:

` io.on("connection", (socket) => { console.log("a user connected", socket.id); console.log("Socket query params:", socket.handshake.query); console.log("Socket auth params:", socket.handshake.auth); const userId = socket.handshake.query.userId;

console.log({userId});

if (!userId) {
    console.log("No user id found in the socket query params");
    socket.disconnect(true);  // Force disconnect
    return;
}

userSocketMap[userId] = socket.id;

console.log("user socket data: ", userSocketMap);

socket.on("disconnect", () => {
    console.log("user disconnected", socket.id);
    delete userSocketMap[userId];
});

}); `

On my console am getting: Socket auth params: {} { userId: undefined }

for both the auth and query.

brayohmurithidev avatar Feb 21 '25 07:02 brayohmurithidev