ACOUNT SHARING

Hello, I am creating a paid-membership web and I want to prevent users from sharing their accounts. As it is likely for my users to know each other I need to have it.

I was thinking on implement somehow a way to only let them login once at the same time. Do anyone know how to implement an idea like this?

Thanks

Hey
Of course. There is a great solution I think that I have used in a project.

First of all you will need to hook up the onLogin method of Wix users.
https://www.wix.com/code/reference/wix-users.html#onLogin

Put all of this verification code inside the Site Code Tab of your site because you want this code to be executed in all pages.

  1. Create a new Data Collection called ie AuditSystem
  2. In that Data Collection add fields for userId, ipAddress, LastLoginStamp.
  3. When someone logs in, store their data, check if there is data
  4. Handle it the way you want, redirect fake users or whatever you need to do

The below function will get a visitors ip address using getJSON in wix-fetch.

import {getJSON} from 'wix-fetch';
import wixUsers from 'wix-users';

// If you want to test to get an ip address
// $w.onReady(function () {  
//   getIpAddress().then((ip) => {
//     console.log(ip);
//   })
// });
export function getIpAddress() {
 return getJSON("https://api.ipify.org/?format=json&callback=")
  .then(json => {
 return json.ip;
  })
  .catch(err => {
    console.log(err);
  })
}

The below code is just a sample on my idea

wixUsers.onLogin( (user) => {
  getIpAddress().then((ip) => {
  let userId = user.id;           // "r5cme-6fem-485j-djre-4844c49"
  let isLoggedIn = user.loggedIn; // true
  let userIp = ip;
  
  // Check in your AuditSystem Data Collection if there is a record
  // in the data collection which matches the currently logging in
  // user. If there is a match it's probably the same user but if the
  // ip address is different it's
  // probably a new person from a different network trying to use some
  // other persons credentials.
  
  wixData.query("AuditSystem")
  .eq("userId", userId)
  .limit(1)
  .find()
  .then((results) => {
    if (results.totalCount > 0) {
      if (results.items[0].ipAddress === userIp) {
        // same as last login
      } else {
        // use is from different network and should be handled
      }
      
    } else {
      // No records, store a new record now
    }
   })
  
  });
} );

You get the idea I hope, I can’t make the whole thing working quicker.

The idea from me is that you log when they login with their login time, their ip address and user id. Then you can check those records and you can find out if there is a user lending out their credentials.

Maybe it would be nice to store the email and first name as well in the audit log so you can send them an email to inform them that their credentials is being used from another network and the account will be closed if this is not explained. Or you can do like Netflix and others and allow login from 2 different networks but not more.

Then at last make sure that you add code for the logout, add your own logout button and delete the current ip address and user from AuditSystem so that a user actually can login after they have logged out.

Or just mark it deleted by adding a field boolean called Deleted and set that to true so you can keep all audit trails if you would need them for something.

Thanks for the great help. I am in hw works on getting everything done but I am stuck when trying to store data in my collection. Here is the code I’ve done so far, but my collection does not collect anything. Any help?

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;

wixUsers.onLogin((user) => {
// getIpAddress().then((ip) => {
let userId = user.id; // “r5cme-6fem-485j-djre-4844c49”
let isLoggedIn = user.loggedIn; // true

let toInsert = {
“ID”: userId,
};

wixData.insert(“Users”, toInsert)
.then((results) => {
let item = results;
})
. catch ((err) => {
let errorMsg = err;
});
}
)

Hi,
Do you get an error on the console?
Did you set permissions?
Roi.