User.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. get account(){
  12. return this._accounts[this._currentAccount];
  13. }
  14. static async create(name, email, password){
  15. const passwordSalt = EncryptionHandler.generateSalt();
  16. return {
  17. name: name,
  18. email: email,
  19. password_hash: await EncryptionHandler.hashPassword(password, passwordSalt),
  20. password_salt: passwordSalt,
  21. encryption_salt: EncryptionHandler.generateSalt()
  22. };
  23. }
  24. addAccount(account){
  25. this._accounts.push(account);
  26. }
  27. changeAccount(account){
  28. if(typeof account === "number"){
  29. this._currentAccount = 0;
  30. }else if(typeof account === "string"){
  31. for(let i = 0; i < this._accounts.length; i++){
  32. if(this._accounts[i].id === account){
  33. this._currentAccount = i;
  34. break;
  35. }
  36. }
  37. }else{
  38. for(let i = 0; i < this._accounts.length; i++){
  39. if(this._accounts[i] === account){
  40. this._currentAccount = i;
  41. break;
  42. }
  43. }
  44. }
  45. }
  46. async decryptAndAddAccount(account){
  47. const data = await encryptionHandler.decrypt(account.data, account.iv);
  48. const newAccount = new Account(account.id, account.iv, data);
  49. this._accounts.push(newAccount);
  50. }
  51. }