]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/core/auth/auth.service.ts
Update server dependencies
[github/Chocobozzz/PeerTube.git] / client / src / app / core / auth / auth.service.ts
index 5da66c981da42d25e892a48933b6fe5a2a4d1708..2ac88c18529b8ad071e004ccdf2a778d3407dc20 100644 (file)
@@ -5,7 +5,7 @@ import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'
 import { Injectable } from '@angular/core'
 import { Router } from '@angular/router'
 import { Notifier } from '@app/core/notification/notifier.service'
-import { objectToUrlEncoded, peertubeLocalStorage } from '@root-helpers/index'
+import { objectToUrlEncoded, peertubeLocalStorage, UserTokens } from '@root-helpers/index'
 import { HttpStatusCode, MyUser as UserServerModel, OAuthClientLocal, User, UserLogin, UserRefreshToken } from '@shared/models'
 import { environment } from '../../../environments/environment'
 import { RestExtractor } from '../rest/rest-extractor.service'
@@ -34,6 +34,7 @@ export class AuthService {
 
   loginChangedSource: Observable<AuthStatus>
   userInformationLoaded = new ReplaySubject<boolean>(1)
+  tokensRefreshed = new ReplaySubject<void>(1)
   hotkeys: Hotkey[]
 
   private clientId: string = peertubeLocalStorage.getItem(AuthService.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID)
@@ -48,13 +49,10 @@ export class AuthService {
     private hotkeysService: HotkeysService,
     private restExtractor: RestExtractor,
     private router: Router
-    ) {
+  ) {
     this.loginChanged = new Subject<AuthStatus>()
     this.loginChangedSource = this.loginChanged.asObservable()
 
-    // Return null if there is nothing to load
-    this.user = AuthUser.load()
-
     // Set HotKeys
     this.hotkeys = [
       new Hotkey('m s', (event: KeyboardEvent): boolean => {
@@ -76,6 +74,10 @@ export class AuthService {
     ]
   }
 
+  buildAuthUser (userInfo: Partial<User>, tokens: UserTokens) {
+    this.user = new AuthUser(userInfo, tokens)
+  }
+
   loadClientCredentials () {
     // Fetch the client_id/client_secret
     this.http.get<OAuthClientLocal>(AuthService.BASE_CLIENT_URL)
@@ -180,8 +182,6 @@ Ensure you have correctly configured PeerTube (config/ directory), in particular
 
     this.user = null
 
-    AuthUser.flush()
-
     this.setStatus(AuthStatus.LoggedOut)
 
     this.hotkeysService.remove(this.hotkeys)
@@ -206,7 +206,9 @@ Ensure you have correctly configured PeerTube (config/ directory), in particular
     this.refreshingTokenObservable = this.http.post<UserRefreshToken>(AuthService.BASE_TOKEN_URL, body, { headers })
                                          .pipe(
                                            map(res => this.handleRefreshToken(res)),
-                                           tap(() => { this.refreshingTokenObservable = null }),
+                                           tap(() => {
+                                             this.refreshingTokenObservable = null
+                                           }),
                                            catchError(err => {
                                              this.refreshingTokenObservable = null
 
@@ -237,7 +239,6 @@ Ensure you have correctly configured PeerTube (config/ directory), in particular
         .subscribe({
           next: res => {
             this.user.patch(res)
-            this.user.save()
 
             this.userInformationLoaded.next(true)
           }
@@ -260,7 +261,6 @@ Ensure you have correctly configured PeerTube (config/ directory), in particular
     }
 
     this.user = new AuthUser(obj, hashTokens)
-    this.user.save()
 
     this.setStatus(AuthStatus.LoggedIn)
     this.userInformationLoaded.next(true)
@@ -270,7 +270,7 @@ Ensure you have correctly configured PeerTube (config/ directory), in particular
 
   private handleRefreshToken (obj: UserRefreshToken) {
     this.user.refreshTokens(obj.access_token, obj.refresh_token)
-    this.user.save()
+    this.tokensRefreshed.next()
   }
 
   private setStatus (status: AuthStatus) {