daily pastebin goal
1%
SHARE
TWEET

Untitled

a guest Dec 7th, 2017 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. // This class is used for logins
  4. class Login {
  5.   constructor(hash) {
  6.     this.sessions = [];
  7.     this.users = [];
  8.     this.passwords = [];
  9.     Object.keys(hash).map(k => ({k, v: hash[k]})).map(e => {
  10.       this.users = this.users.concat([e.k]);
  11.       this.passwords = this.passwords.concat([e.v]);
  12.     });
  13.   }
  14.  
  15.       login(user, password) {
  16.         if (this.userExists(user)) {
  17.             if (this.checkPassword(user, password)) {
  18.                 this.sessions.push(user);
  19.                 console.log("Login success!");
  20.             } else {
  21.                 console.error('Error password incorrect!');
  22.             }
  23.         } else {
  24.             console.error("Error, usuario: " + user + " no existe!");
  25.         }
  26.         //console.log(this.sessions);
  27.     }
  28.  
  29.     logout(user) {
  30.  
  31.         if (this.userExists(user)) {
  32.             let index = this.sessions.indexOf(user);
  33.             if (index != -1) {
  34.                 this.sessions.splice(index, 1);
  35.                 console.log("Logout success!");
  36.             } else {
  37.                 console.error("Error, user:" + user + " no está logeado!");
  38.             }
  39.         } else {
  40.             console.error("Error, usuario no existe!");
  41.         }
  42.     }
  43.  
  44.   // Usuario existe?
  45.    userExists(user) {
  46.         return (this.users.indexOf(user) != -1);
  47.     }
  48.  
  49.   // Register user
  50.   registerUser(user, password) {
  51.     let lastIndex = this.users.length;
  52.     this.users[lastIndex] = user;
  53.     this.passwords[lastIndex] = password;
  54.   }
  55.  
  56.   removeUser(user) {
  57.     let index = this.idx(user, this.users);
  58.     this.users[index] = null;
  59.     this.passwords[index] = null;
  60.     this.users = this.users.filter(user => user !== null);
  61.     this.passwords = this.passwords.filter(password => password !== null);
  62.   }
  63.  
  64.     checkPassword(user, password) {
  65.         let index = this.users.indexOf(user);
  66.         return this.passwords[index] === password;
  67.     }  
  68.  
  69.       updatePassword(user, oldPassword, newPassword) {
  70.         // Primero vemos si usuario existe
  71.         let index = this.users.indexOf(user);
  72.         if (index !== -1) {
  73.             if (this.passwords[index] === oldPassword) {
  74.                 this.passwords[index] = newPassword;
  75.                 console.log("Password updated!");
  76.             }else{
  77.                 console.error("Error, clave no coincide!")
  78.             }
  79.         } else {
  80.             console.error("Error, usuario: " + user + " no existe!");
  81.         }
  82.     }
  83. }
  84.  
  85.   // Gets index of an element in an array
  86.   idx(element, array) {
  87.     let cont=0;
  88.     for (let i of array) {
  89.       if (i === element) {
  90.         return cont;
  91.       }
  92.       cont += 1;
  93.     }
  94.     return cont;
  95.   }
  96. }
  97.  
  98. let registeredUsers = {
  99.   user1: 'pass1',
  100.   user2: 'pass2',
  101.   user3: 'pass3'
  102. };
  103.  
  104. let login = new Login(registeredUsers);
  105.  
  106. login.registerUser('user4', 'pass4');
  107. login.login('user4', 'pass4');
  108. login.updatePassword('user3', 'pass3', 'pass5');
  109. login.login('user3', 'pass5');
  110. login.logout('user4');
  111. login.logout('user3');
RAW Paste Data
Pastebin PRO CHRISTMAS Special!
Get 60% OFF on Pastebin PRO accounts!
Top