deepstreamIO / deepstream.io

deepstream.io server
https://deepstreamio.github.io
MIT License
7.14k stars 381 forks source link

Check client already Logged In #1060

Closed caracal7 closed 3 years ago

caracal7 commented 4 years ago

I do it this way

const users = {};

const isValidUser = async ( handshakeData, authData ) => {
    //  Allow only 1 connection per login
    if( users[authData.username] ) return restrictUser(authData.username, 'Already logged in');
        //...
        users[ username ] = true;
}

const onClientDisconnect = async username => {
    if(users[username]) {
        console.log(`Disconnected [${username}]`);
        delete users[username];
    }
}

In most cases its work good but sometimes maybe then client (Mac on local network) goes sleep, onClientDisconnect is not fired and user can't connect because server thinks user is connected.

Is there a more reliable way to check is client already Logged In?

yasserf commented 4 years ago

In most cases its work good but sometimes maybe then client (Mac on local network) goes sleep,

This is where heartbeats are meant to kick in, and the server triggers the disconnect call. What timeout parameters do you have?

caracal7 commented 4 years ago

all is default

const server = new Deepstream({
    logLevel: 'INFO',//'DEBUG',
    httpServer: {
        type: 'default',
        options: {
            port: 6020,
            host: config.host, 
            allowAllOrigins: true,
            ssl: {
                key: keyFile("privkey.pem"),
                cert: keyFile("fullchain.pem"),
            }
        }
    },
});

Maybe I didn't understand the problem right. Once I failed to connect with login without any other connected clients. And seems like clientData not passed to the client. Only reason passed.

this code

    client.login( { username, password }, (success, clientData) => {
        if( success )  resolve(client);
        else {
            console.info(clientData);
            reject(Error(clientData.reason));
        }
    })

produce

{reason: "INVALID_AUTHENTICATION_DETAILS"}
yasserf commented 4 years ago

What is the server logs? My feeling is your most likely doing something wrong rather than this being a bug since it looks like the server is refusing you access

caracal7 commented 4 years ago

What is the server logs? My feeling is your most likely doing something wrong rather than this being a bug since it looks like the server is refusing you access

I've added more console info to server but I can't reproduce problem because its happens very rare

I think its not server itself. I think my code is refusing

if( users[authData.username] ) return restrictUser(authData.username, 'Already logged in');

But I need clientData returned to client to confirm that. My code is


function restrictUser(username, error = 'Incorrect login/password') {
    console.log(`[${username}] restricted: ${error}`);
    return {
        isValid: false,
        id: username,
        clientData: { error }
    }
}```
Any ideas why I didn't receive `clientData` on client?
yasserf commented 4 years ago

Will need to look into this 👍

yasserf commented 4 years ago

Be great if you can contribute a tiny project / gist so we can easily run this to reproduce.

jaime-ez commented 3 years ago

Hi, I'm closing this for now due to inactivity. Feel free to reopen if problem persist and share some code to replicate.