aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/auth/auth-user.model.ts
blob: f560351f4878416186c8af25b9b201d81672427c (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { User } from '../users';

export class AuthUser extends User {
  private static KEYS = {
    ID: 'id',
    ROLE: 'role',
    USERNAME: 'username'
  };

  tokens: Tokens;

  static load() {
    const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME);
    if (usernameLocalStorage) {
      return new AuthUser(
        {
          id: parseInt(localStorage.getItem(this.KEYS.ID)),
          username: localStorage.getItem(this.KEYS.USERNAME),
          role: localStorage.getItem(this.KEYS.ROLE)
        },
        Tokens.load()
      );
    }

    return null;
  }

  static flush() {
    localStorage.removeItem(this.KEYS.USERNAME);
    localStorage.removeItem(this.KEYS.ID);
    localStorage.removeItem(this.KEYS.ROLE);
    Tokens.flush();
  }

  constructor(userHash: { id: number, username: string, role: string }, hashTokens: any) {
    super(userHash);
    this.tokens = new Tokens(hashTokens);
  }

  getAccessToken() {
    return this.tokens.access_token;
  }

  getRefreshToken() {
    return this.tokens.refresh_token;
  }

  getTokenType() {
    return this.tokens.token_type;
  }

  refreshTokens(access_token: string, refresh_token: string) {
    this.tokens.access_token = access_token;
    this.tokens.refresh_token = refresh_token;
  }

  save() {
    localStorage.setItem(AuthUser.KEYS.ID, this.id.toString());
    localStorage.setItem(AuthUser.KEYS.USERNAME, this.username);
    localStorage.setItem(AuthUser.KEYS.ROLE, this.role);
    this.tokens.save();
  }
}

// Private class only used by User
class Tokens {
  private static KEYS = {
    ACCESS_TOKEN: 'access_token',
    REFRESH_TOKEN: 'refresh_token',
    TOKEN_TYPE: 'token_type',
  };

  access_token: string;
  refresh_token: string;
  token_type: string;

  static load() {
    const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN);
    const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN);
    const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE);

    if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
      return new Tokens({
        access_token: accessTokenLocalStorage,
        refresh_token: refreshTokenLocalStorage,
        token_type: tokenTypeLocalStorage
      });
    }

    return null;
  }

  static flush() {
    localStorage.removeItem(this.KEYS.ACCESS_TOKEN);
    localStorage.removeItem(this.KEYS.REFRESH_TOKEN);
    localStorage.removeItem(this.KEYS.TOKEN_TYPE);
  }

  constructor(hash?: any) {
    if (hash) {
      this.access_token = hash.access_token;
      this.refresh_token = hash.refresh_token;

      if (hash.token_type === 'bearer') {
        this.token_type = 'Bearer';
      } else {
        this.token_type = hash.token_type;
      }
    }
  }

  save() {
    localStorage.setItem('access_token', this.access_token);
    localStorage.setItem('refresh_token', this.refresh_token);
    localStorage.setItem('token_type', this.token_type);
  }
}