]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/core/auth/auth.service.ts
Use instance name for page titles
[github/Chocobozzz/PeerTube.git] / client / src / app / core / auth / auth.service.ts
index e2b8b6ba5d84726955e1b4404d1ab00132b2584b..8700e8c742b541026655decfd430630472e01ec2 100644 (file)
@@ -9,12 +9,11 @@ import 'rxjs/add/operator/mergeMap'
 import { Observable } from 'rxjs/Observable'
 import { ReplaySubject } from 'rxjs/ReplaySubject'
 import { Subject } from 'rxjs/Subject'
-import { OAuthClientLocal, User as UserServerModel, UserRefreshToken, UserRole, VideoChannel } from '../../../../../shared'
-import { Account } from '../../../../../shared/models/accounts'
+import { OAuthClientLocal, User as UserServerModel, UserRefreshToken } from '../../../../../shared'
+import { User } from '../../../../../shared/models/users'
 import { UserLogin } from '../../../../../shared/models/users/user-login.model'
 import { environment } from '../../../environments/environment'
 import { RestExtractor } from '../../shared/rest'
-import { UserConstructorHash } from '../../shared/users/user.model'
 import { AuthStatus } from './auth-status.model'
 import { AuthUser } from './auth-user.model'
 
@@ -25,19 +24,7 @@ interface UserLoginWithUsername extends UserLogin {
   username: string
 }
 
-interface UserLoginWithUserInformation extends UserLogin {
-  access_token: string
-  refresh_token: string
-  token_type: string
-  username: string
-  id: number
-  role: UserRole
-  displayNSFW: boolean
-  email: string
-  videoQuota: number
-  account: Account
-  videoChannels: VideoChannel[]
-}
+type UserLoginWithUserInformation = UserLoginWithUsername & User
 
 @Injectable()
 export class AuthService {
@@ -190,11 +177,7 @@ export class AuthService {
     this.mergeUserInformation(obj)
       .subscribe(
         res => {
-          this.user.displayNSFW = res.displayNSFW
-          this.user.role = res.role
-          this.user.videoChannels = res.videoChannels
-          this.user.account = res.account
-
+          this.user.patch(res)
           this.user.save()
 
           this.userInformationLoaded.next(true)
@@ -207,40 +190,17 @@ export class AuthService {
     const headers = new HttpHeaders().set('Authorization', `${obj.token_type} ${obj.access_token}`)
 
     return this.http.get<UserServerModel>(AuthService.BASE_USER_INFORMATION_URL, { headers })
-                    .map(res => {
-                      const newProperties = {
-                        id: res.id,
-                        role: res.role,
-                        displayNSFW: res.displayNSFW,
-                        email: res.email,
-                        videoQuota: res.videoQuota,
-                        account: res.account,
-                        videoChannels: res.videoChannels
-                      }
-
-                      return Object.assign(obj, newProperties)
-                    }
-    )
+                    .map(res => Object.assign(obj, res))
   }
 
   private handleLogin (obj: UserLoginWithUserInformation) {
-    const hashUser: UserConstructorHash = {
-      id: obj.id,
-      username: obj.username,
-      role: obj.role,
-      email: obj.email,
-      displayNSFW: obj.displayNSFW,
-      videoQuota: obj.videoQuota,
-      videoChannels: obj.videoChannels,
-      account: obj.account
-    }
     const hashTokens = {
       accessToken: obj.access_token,
       tokenType: obj.token_type,
       refreshToken: obj.refresh_token
     }
 
-    this.user = new AuthUser(hashUser, hashTokens)
+    this.user = new AuthUser(obj, hashTokens)
     this.user.save()
 
     this.setStatus(AuthStatus.LoggedIn)