diff options
Diffstat (limited to 'client/src/app/core/auth')
-rw-r--r-- | client/src/app/core/auth/auth.service.ts | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/client/src/app/core/auth/auth.service.ts b/client/src/app/core/auth/auth.service.ts index c3879d570..614d38d08 100644 --- a/client/src/app/core/auth/auth.service.ts +++ b/client/src/app/core/auth/auth.service.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | import { Observable, ReplaySubject, Subject, throwError as observableThrowError } from 'rxjs' | 1 | import { Observable, ReplaySubject, Subject, throwError as observableThrowError } from 'rxjs' |
2 | import { catchError, map, mergeMap, tap } from 'rxjs/operators' | 2 | import { catchError, map, mergeMap, tap, share } from 'rxjs/operators' |
3 | import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http' | 3 | import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http' |
4 | import { Injectable } from '@angular/core' | 4 | import { Injectable } from '@angular/core' |
5 | import { Router } from '@angular/router' | 5 | import { Router } from '@angular/router' |
@@ -12,6 +12,7 @@ import { RestExtractor } from '../../shared/rest' | |||
12 | import { AuthStatus } from './auth-status.model' | 12 | import { AuthStatus } from './auth-status.model' |
13 | import { AuthUser } from './auth-user.model' | 13 | import { AuthUser } from './auth-user.model' |
14 | import { objectToUrlEncoded } from '@app/shared/misc/utils' | 14 | import { objectToUrlEncoded } from '@app/shared/misc/utils' |
15 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage' | ||
15 | 16 | ||
16 | interface UserLoginWithUsername extends UserLogin { | 17 | interface UserLoginWithUsername extends UserLogin { |
17 | access_token: string | 18 | access_token: string |
@@ -27,12 +28,16 @@ export class AuthService { | |||
27 | private static BASE_CLIENT_URL = environment.apiUrl + '/api/v1/oauth-clients/local' | 28 | private static BASE_CLIENT_URL = environment.apiUrl + '/api/v1/oauth-clients/local' |
28 | private static BASE_TOKEN_URL = environment.apiUrl + '/api/v1/users/token' | 29 | private static BASE_TOKEN_URL = environment.apiUrl + '/api/v1/users/token' |
29 | private static BASE_USER_INFORMATION_URL = environment.apiUrl + '/api/v1/users/me' | 30 | private static BASE_USER_INFORMATION_URL = environment.apiUrl + '/api/v1/users/me' |
31 | private static LOCAL_STORAGE_OAUTH_CLIENT_KEYS = { | ||
32 | CLIENT_ID: 'client_id', | ||
33 | CLIENT_SECRET: 'client_secret' | ||
34 | } | ||
30 | 35 | ||
31 | loginChangedSource: Observable<AuthStatus> | 36 | loginChangedSource: Observable<AuthStatus> |
32 | userInformationLoaded = new ReplaySubject<boolean>(1) | 37 | userInformationLoaded = new ReplaySubject<boolean>(1) |
33 | 38 | ||
34 | private clientId: string | 39 | private clientId: string = peertubeLocalStorage.getItem(AuthService.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID) |
35 | private clientSecret: string | 40 | private clientSecret: string = peertubeLocalStorage.getItem(AuthService.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_SECRET) |
36 | private loginChanged: Subject<AuthStatus> | 41 | private loginChanged: Subject<AuthStatus> |
37 | private user: AuthUser = null | 42 | private user: AuthUser = null |
38 | private refreshingTokenObservable: Observable<any> | 43 | private refreshingTokenObservable: Observable<any> |
@@ -52,13 +57,16 @@ export class AuthService { | |||
52 | 57 | ||
53 | loadClientCredentials () { | 58 | loadClientCredentials () { |
54 | // Fetch the client_id/client_secret | 59 | // Fetch the client_id/client_secret |
55 | // FIXME: save in local storage? | ||
56 | this.http.get<OAuthClientLocal>(AuthService.BASE_CLIENT_URL) | 60 | this.http.get<OAuthClientLocal>(AuthService.BASE_CLIENT_URL) |
57 | .pipe(catchError(res => this.restExtractor.handleError(res))) | 61 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
58 | .subscribe( | 62 | .subscribe( |
59 | res => { | 63 | res => { |
60 | this.clientId = res.client_id | 64 | this.clientId = res.client_id |
61 | this.clientSecret = res.client_secret | 65 | this.clientSecret = res.client_secret |
66 | |||
67 | peertubeLocalStorage.setItem(AuthService.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID, this.clientId) | ||
68 | peertubeLocalStorage.setItem(AuthService.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_SECRET, this.clientSecret) | ||
69 | |||
62 | console.log('Client credentials loaded.') | 70 | console.log('Client credentials loaded.') |
63 | }, | 71 | }, |
64 | 72 | ||
@@ -174,7 +182,8 @@ export class AuthService { | |||
174 | return observableThrowError({ | 182 | return observableThrowError({ |
175 | error: 'You need to reconnect.' | 183 | error: 'You need to reconnect.' |
176 | }) | 184 | }) |
177 | }) | 185 | }), |
186 | share() | ||
178 | ) | 187 | ) |
179 | 188 | ||
180 | return this.refreshingTokenObservable | 189 | return this.refreshingTokenObservable |