aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/auth
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-08-09 21:45:21 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-08-09 21:45:21 +0200
commit7da18e4420c4b71a8ecfda07f39324fbfec081c3 (patch)
tree2755feedd730a78cdc407e7a79bcbfce3ebe71c8 /client/src/app/shared/auth
parent68a3b9f2aacb0225ae8b883b561b144bac339cbd (diff)
downloadPeerTube-7da18e4420c4b71a8ecfda07f39324fbfec081c3.tar.gz
PeerTube-7da18e4420c4b71a8ecfda07f39324fbfec081c3.tar.zst
PeerTube-7da18e4420c4b71a8ecfda07f39324fbfec081c3.zip
Client: add user management
Diffstat (limited to 'client/src/app/shared/auth')
-rw-r--r--client/src/app/shared/auth/auth-user.model.ts (renamed from client/src/app/shared/auth/user.model.ts)28
-rw-r--r--client/src/app/shared/auth/auth.service.ts20
-rw-r--r--client/src/app/shared/auth/index.ts2
3 files changed, 29 insertions, 21 deletions
diff --git a/client/src/app/shared/auth/user.model.ts b/client/src/app/shared/auth/auth-user.model.ts
index e486873ab..bdd5ea5a9 100644
--- a/client/src/app/shared/auth/user.model.ts
+++ b/client/src/app/shared/auth/auth-user.model.ts
@@ -1,4 +1,6 @@
1export class User { 1import { User } from '../users';
2
3export class AuthUser extends User {
2 private static KEYS = { 4 private static KEYS = {
3 ID: 'id', 5 ID: 'id',
4 ROLE: 'role', 6 ROLE: 'role',
@@ -13,10 +15,12 @@ export class User {
13 static load() { 15 static load() {
14 const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME); 16 const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME);
15 if (usernameLocalStorage) { 17 if (usernameLocalStorage) {
16 return new User( 18 return new AuthUser(
17 localStorage.getItem(this.KEYS.ID), 19 {
18 localStorage.getItem(this.KEYS.USERNAME), 20 id: localStorage.getItem(this.KEYS.ID),
19 localStorage.getItem(this.KEYS.ROLE), 21 username: localStorage.getItem(this.KEYS.USERNAME),
22 role: localStorage.getItem(this.KEYS.ROLE)
23 },
20 Tokens.load() 24 Tokens.load()
21 ); 25 );
22 } 26 }
@@ -31,11 +35,9 @@ export class User {
31 Tokens.flush(); 35 Tokens.flush();
32 } 36 }
33 37
34 constructor(id: string, username: string, role: string, hash_tokens: any) { 38 constructor(userHash: { id: string, username: string, role: string }, hashTokens: any) {
35 this.id = id; 39 super(userHash);
36 this.username = username; 40 this.tokens = new Tokens(hashTokens);
37 this.role = role;
38 this.tokens = new Tokens(hash_tokens);
39 } 41 }
40 42
41 getAccessToken() { 43 getAccessToken() {
@@ -56,9 +58,9 @@ export class User {
56 } 58 }
57 59
58 save() { 60 save() {
59 localStorage.setItem(User.KEYS.ID, this.id); 61 localStorage.setItem(AuthUser.KEYS.ID, this.id);
60 localStorage.setItem(User.KEYS.USERNAME, this.username); 62 localStorage.setItem(AuthUser.KEYS.USERNAME, this.username);
61 localStorage.setItem(User.KEYS.ROLE, this.role); 63 localStorage.setItem(AuthUser.KEYS.ROLE, this.role);
62 this.tokens.save(); 64 this.tokens.save();
63 } 65 }
64} 66}
diff --git a/client/src/app/shared/auth/auth.service.ts b/client/src/app/shared/auth/auth.service.ts
index 24d1a4fa2..8eea0c4bf 100644
--- a/client/src/app/shared/auth/auth.service.ts
+++ b/client/src/app/shared/auth/auth.service.ts
@@ -4,7 +4,7 @@ import { Observable } from 'rxjs/Observable';
4import { Subject } from 'rxjs/Subject'; 4import { Subject } from 'rxjs/Subject';
5 5
6import { AuthStatus } from './auth-status.model'; 6import { AuthStatus } from './auth-status.model';
7import { User } from './user.model'; 7import { AuthUser } from './auth-user.model';
8 8
9@Injectable() 9@Injectable()
10export class AuthService { 10export class AuthService {
@@ -17,7 +17,7 @@ export class AuthService {
17 private clientId: string; 17 private clientId: string;
18 private clientSecret: string; 18 private clientSecret: string;
19 private loginChanged: Subject<AuthStatus>; 19 private loginChanged: Subject<AuthStatus>;
20 private user: User = null; 20 private user: AuthUser = null;
21 21
22 constructor(private http: Http) { 22 constructor(private http: Http) {
23 this.loginChanged = new Subject<AuthStatus>(); 23 this.loginChanged = new Subject<AuthStatus>();
@@ -40,7 +40,7 @@ export class AuthService {
40 ); 40 );
41 41
42 // Return null if there is nothing to load 42 // Return null if there is nothing to load
43 this.user = User.load(); 43 this.user = AuthUser.load();
44 } 44 }
45 45
46 getRefreshToken() { 46 getRefreshToken() {
@@ -65,10 +65,16 @@ export class AuthService {
65 return this.user.getTokenType(); 65 return this.user.getTokenType();
66 } 66 }
67 67
68 getUser(): User { 68 getUser(): AuthUser {
69 return this.user; 69 return this.user;
70 } 70 }
71 71
72 isAdmin() {
73 if (this.user === null) return false;
74
75 return this.user.isAdmin();
76 }
77
72 isLoggedIn() { 78 isLoggedIn() {
73 if (this.getAccessToken()) { 79 if (this.getAccessToken()) {
74 return true; 80 return true;
@@ -108,7 +114,7 @@ export class AuthService {
108 logout() { 114 logout() {
109 // TODO: make an HTTP request to revoke the tokens 115 // TODO: make an HTTP request to revoke the tokens
110 this.user = null; 116 this.user = null;
111 User.flush(); 117 AuthUser.flush();
112 118
113 this.setStatus(AuthStatus.LoggedOut); 119 this.setStatus(AuthStatus.LoggedOut);
114 } 120 }
@@ -163,13 +169,13 @@ export class AuthService {
163 const id = obj.id; 169 const id = obj.id;
164 const username = obj.username; 170 const username = obj.username;
165 const role = obj.role; 171 const role = obj.role;
166 const hash_tokens = { 172 const hashTokens = {
167 access_token: obj.access_token, 173 access_token: obj.access_token,
168 token_type: obj.token_type, 174 token_type: obj.token_type,
169 refresh_token: obj.refresh_token 175 refresh_token: obj.refresh_token
170 }; 176 };
171 177
172 this.user = new User(id, username, role, hash_tokens); 178 this.user = new AuthUser({ id, username, role }, hashTokens);
173 this.user.save(); 179 this.user.save();
174 180
175 this.setStatus(AuthStatus.LoggedIn); 181 this.setStatus(AuthStatus.LoggedIn);
diff --git a/client/src/app/shared/auth/index.ts b/client/src/app/shared/auth/index.ts
index aafaacbf1..ebd9e14cd 100644
--- a/client/src/app/shared/auth/index.ts
+++ b/client/src/app/shared/auth/index.ts
@@ -1,4 +1,4 @@
1export * from './auth-http.service'; 1export * from './auth-http.service';
2export * from './auth-status.model'; 2export * from './auth-status.model';
3export * from './auth.service'; 3export * from './auth.service';
4export * from './user.model'; 4export * from './auth-user.model';