User.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import EncryptionHandler from "../EncryptionHandler.js";
  2. import Account from "./Account.js";
  3. export default class User{
  4. constructor(id, name, email, accounts = []){
  5. this._id = id;
  6. this._name = name;
  7. this._email = email;
  8. this._accounts = accounts;
  9. this._currentAccount = 0;
  10. }
  11. static async create(name, email, password){
  12. const passwordSalt = EncryptionHandler.generateSalt();
  13. return {
  14. name: name,
  15. email: email,
  16. password_hash: await EncryptionHandler.hashPassword(password, passwordSalt),
  17. password_salt: passwordSalt,
  18. encryption_salt: EncryptionHandler.generateSalt()
  19. };
  20. }
  21. addAccount(account){
  22. this._accounts.push(account);
  23. }
  24. changeAccount(account){
  25. if(typeof account === "number"){
  26. this._currentAccount = 0;
  27. }else if(typeof account === "string"){
  28. for(let i = 0; i < this._accounts.length; i++){
  29. if(this._accounts[i].id === account){
  30. this._currentAccount = i;
  31. break;
  32. }
  33. }
  34. }else{
  35. for(let i = 0; i < this._accounts.length; i++){
  36. if(this.__accounts[i] === account){
  37. this._currentAccount = i;
  38. break;
  39. }
  40. }
  41. }
  42. }
  43. async decryptAndAddAccount(account){
  44. const data = await encryptionHandler.decrypt(account.data, account.iv);
  45. const newAccount = new Account(account.id, data);
  46. this._accounts.push(newAccount);
  47. }
  48. }