register(user, callerInstance) {

    var root = this;

    /**
     * This line prevents method register
     * to be used by others classes.
     * Connector class is allowed.
     */
    if (callerInstance.constructor.name !== "Connector") {
      console.error("Potencial Critical Hack Attack");
      return;
    }

    const databaseName = this.config.databaseName;

    /**
    * Open connection with database.
    */
    MongoClient.connect(this.config.getDatabaseRoot,  {
                          useNewUrlParser: true,
                          useUnifiedTopology: true
                        }, function(error, db) {
      if (error) {
        console.warn("MyDatabase : err1:" + error);
        return;
      }

      const dbo = db.db(databaseName);
      if (!dbo.collection("users")) {
        dbo.createCollection("users").createIndex({ "email": 1 }, { unique: true });
        dbo.createCollection("users").createIndex({ "password": 1 }, { unique: true });
        dbo.createCollection("users").createIndex({ "socketid": 1 }, { unique: true });
        dbo.createCollection("users").createIndex({ "confirmed": 1 }, { unique: true });
        dbo.createCollection("users").createIndex({ "token": 1 }, { unique: true });
        dbo.createCollection("users").createIndex({ "online": 1 }, { unique: false });
        dbo.createCollection("users").createIndex({ "nickname": 1 }, { unique: false });
        dbo.createCollection("users").createIndex({ "points": 1 }, { unique: false });
      }

      dbo.collection("users").findOne({ "email": user.userRegData.email }, function(err, result) {

        if (err) { console.log("MyDatabase err2:" + err); return null; }

        if (result === null) {

          let uniqLocal = shared.generateToken();

          dbo.collection("users").insertOne({
            email: user.userRegData.email,
            password: callerInstance.crypto.encrypt(user.userRegData.password),
            nickname: "no-nick-name" + shared.getDefaultNickName(),
            confirmed: false,
            token: uniqLocal,
            socketid: user.socketId,
            online: false,
            points: 1000,
            rank: "junior"
          }, function(err, res) {
            if (err) {
              console.log("MyDatabase err3:" + err);
              db.close();
              return;
            }
            callerInstance.onRegisterResponse("USER_REGISTERED", res.ops[0].email, res.ops[0].token, res.ops[0].socketid, callerInstance);
            db.close();
          });
        } else {
          callerInstance.onRegisterResponse("USER_ALREADY_REGISTERED", user.userRegData.email, null, user.socketId, callerInstance);
          db.close();
        }

      });
    });

  }
/**
   * Method register is called on singup user action.
   * @param {object} user
   *  email: user.userRegData.email
   *  user.userRegData.password
   * @param {classInstance} callerInstance
   */
Method register is called on singup user action.
Method register is called on singup user action .
register(user, callerInstance) {
    var root = this;
    if (callerInstance.constructor.name !== "Connector") {
      console.error("Potencial Critical Hack Attack");
      return;
    }
    const databaseName = this.config.databaseName;
    MongoClient.connect(this.config.getDatabaseRoot,  {
                          useNewUrlParser: true,
                          useUnifiedTopology: true
                        }, function(error, db) {
      if (error) {
        console.warn("MyDatabase : err1:" + error);
        return;
      }
      const dbo = db.db(databaseName);
      if (!dbo.collection("users")) {
        dbo.createCollection("users").createIndex({ "email": 1 }, { unique: true });
        dbo.createCollection("users").createIndex({ "password": 1 }, { unique: true });
        dbo.createCollection("users").createIndex({ "socketid": 1 }, { unique: true });
        dbo.createCollection("users").createIndex({ "confirmed": 1 }, { unique: true });
        dbo.createCollection("users").createIndex({ "token": 1 }, { unique: true });
        dbo.createCollection("users").createIndex({ "online": 1 }, { unique: false });
        dbo.createCollection("users").createIndex({ "nickname": 1 }, { unique: false });
        dbo.createCollection("users").createIndex({ "points": 1 }, { unique: false });
      }
      dbo.collection("users").findOne({ "email": user.userRegData.email }, function(err, result) {
        if (err) { console.log("MyDatabase err2:" + err); return null; }
        if (result === null) {
          let uniqLocal = shared.generateToken();
          dbo.collection("users").insertOne({
            email: user.userRegData.email,
            password: callerInstance.crypto.encrypt(user.userRegData.password),
            nickname: "no-nick-name" + shared.getDefaultNickName(),
            confirmed: false,
            token: uniqLocal,
            socketid: user.socketId,
            online: false,
            points: 1000,
            rank: "junior"
          }, function(err, res) {
            if (err) {
              console.log("MyDatabase err3:" + err);
              db.close();
              return;
            }
            callerInstance.onRegisterResponse("USER_REGISTERED", res.ops[0].email, res.ops[0].token, res.ops[0].socketid, callerInstance);
            db.close();
          });
        } else {
          callerInstance.onRegisterResponse("USER_ALREADY_REGISTERED", user.userRegData.email, null, user.socketId, callerInstance);
          db.close();
        }
      });
    });
  }
register ( user , callerInstance ) { var root = this ; if ( callerInstance . constructor . name !== "Connector" ) { console . error ( "Potencial Critical Hack Attack" ) ; return ; } const databaseName = this . config . databaseName ; MongoClient . connect ( this . config . getDatabaseRoot , { useNewUrlParser : true , useUnifiedTopology : true } , function ( error , db ) { if ( error ) { console . warn ( "MyDatabase : err1:" + error ) ; return ; } const dbo = db . db ( databaseName ) ; if ( ! dbo . collection ( "users" ) ) { dbo . createCollection ( "users" ) . createIndex ( { "email" : 1 } , { unique : true } ) ; dbo . createCollection ( "users" ) . createIndex ( { "password" : 1 } , { unique : true } ) ; dbo . createCollection ( "users" ) . createIndex ( { "socketid" : 1 } , { unique : true } ) ; dbo . createCollection ( "users" ) . createIndex ( { "confirmed" : 1 } , { unique : true } ) ; dbo . createCollection ( "users" ) . createIndex ( { "token" : 1 } , { unique : true } ) ; dbo . createCollection ( "users" ) . createIndex ( { "online" : 1 } , { unique : false } ) ; dbo . createCollection ( "users" ) . createIndex ( { "nickname" : 1 } , { unique : false } ) ; dbo . createCollection ( "users" ) . createIndex ( { "points" : 1 } , { unique : false } ) ; } dbo . collection ( "users" ) . findOne ( { "email" : user . userRegData . email } , function ( err , result ) { if ( err ) { console . log ( "MyDatabase err2:" + err ) ; return null ; } if ( result === null ) { let uniqLocal = shared . generateToken ( ) ; dbo . collection ( "users" ) . insertOne ( { email : user . userRegData . email , password : callerInstance . crypto . encrypt ( user . userRegData . password ) , nickname : "no-nick-name" + shared . getDefaultNickName ( ) , confirmed : false , token : uniqLocal , socketid : user . socketId , online : false , points : 1000 , rank : "junior" } , function ( err , res ) { if ( err ) { console . log ( "MyDatabase err3:" + err ) ; db . close ( ) ; return ; } callerInstance . onRegisterResponse ( "USER_REGISTERED" , res . ops [ 0 ] . email , res . ops [ 0 ] . token , res . ops [ 0 ] . socketid , callerInstance ) ; db . close ( ) ; } ) ; } else { callerInstance . onRegisterResponse ( "USER_ALREADY_REGISTERED" , user . userRegData . email , null , user . socketId , callerInstance ) ; db . close ( ) ; } } ) ; } ) ; }
Method register is called on singup user action.
Method register is called on singup user action .
/**
     * This line prevents method register
     * to be used by others classes.
     * Connector class is allowed.
     */ /**
    * Open connection with database.
    */