aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/UserApi.ts
blob: b70d3b3454d4ebb6359864f0a5d3f007d3e8d1d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import type { BinaryLike } from 'node:crypto';
import { hash } from '../helpers/password-helpers';

export default class UserApi {
  server: any;

  local: any;

  constructor(server: any, local: any) {
    this.server = server;
    this.local = local;
  }

  login(email: string, password: BinaryLike) {
    return this.server.login(email, hash(password));
  }

  logout() {
    return this;
  }

  signup(data: { password: BinaryLike }) {
    Object.assign(data, {
      password: hash(data.password),
    });
    return this.server.signup(data);
  }

  password(email: string) {
    return this.server.retrievePassword(email);
  }

  invite(data: any) {
    return this.server.inviteUser(data);
  }

  getInfo() {
    return this.server.userInfo();
  }

  requestNewToken() {
    return this.server.requestNewToken();
  }

  updateInfo(data: { oldPassword: string; newPassword: string }) {
    const userData = data;
    if (userData.oldPassword && userData.newPassword) {
      userData.oldPassword = hash(userData.oldPassword);
      userData.newPassword = hash(userData.newPassword);
    }

    return this.server.updateUserInfo(userData);
  }

  delete() {
    return this.server.deleteAccount();
  }
}