X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fcore%2Fauth%2Fauth.service.ts;h=e887dde1ff5be7c023144ff277140e1c88d2b72c;hb=fada8d75550dc7365f7e18ee1569b9406251d660;hp=9ac9ba7bb1fcd7a20b2f168af4253d779db998d4;hpb=bcd9f81eff05ffd930c5d8175fb907d4d371432a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/core/auth/auth.service.ts b/client/src/app/core/auth/auth.service.ts index 9ac9ba7bb..e887dde1f 100644 --- a/client/src/app/core/auth/auth.service.ts +++ b/client/src/app/core/auth/auth.service.ts @@ -1,27 +1,24 @@ +import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http' import { Injectable } from '@angular/core' import { Router } from '@angular/router' -import { Observable } from 'rxjs/Observable' -import { Subject } from 'rxjs/Subject' -import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http' -import 'rxjs/add/operator/map' -import 'rxjs/add/operator/mergeMap' -import 'rxjs/add/observable/throw' import { NotificationsService } from 'angular2-notifications' +import 'rxjs/add/observable/throw' +import 'rxjs/add/operator/do' +import 'rxjs/add/operator/map' +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 { UserLogin } from '../../../../../shared/models/users/user-login.model' +// Do not use the barrel (dependency loop) +import { RestExtractor } from '../../shared/rest' +import { UserConstructorHash } from '../../shared/users/user.model' import { AuthStatus } from './auth-status.model' import { AuthUser } from './auth-user.model' -import { - OAuthClientLocal, - UserRole, - UserRefreshToken, - VideoChannel, - User as UserServerModel -} from '../../../../../shared' -// Do not use the barrel (dependency loop) -import { RestExtractor } from '../../shared/rest' -import { UserLogin } from '../../../../../shared/models/users/user-login.model' -import { User, UserConstructorHash } from '../../shared/users/user.model' interface UserLoginWithUsername extends UserLogin { access_token: string @@ -40,10 +37,7 @@ interface UserLoginWithUserInformation extends UserLogin { displayNSFW: boolean email: string videoQuota: number - author: { - id: number - uuid: string - } + account: Account videoChannels: VideoChannel[] } @@ -54,6 +48,7 @@ export class AuthService { private static BASE_USER_INFORMATION_URL = API_URL + '/api/v1/users/me' loginChangedSource: Observable + userInformationLoaded = new ReplaySubject(1) private clientId: string private clientSecret: string @@ -126,12 +121,6 @@ export class AuthService { return this.user } - isAdmin () { - if (this.user === null) return false - - return this.user.isAdmin() - } - isLoggedIn () { return !!this.getAccessToken() } @@ -180,19 +169,15 @@ export class AuthService { return this.http.post(AuthService.BASE_TOKEN_URL, body, { headers }) .map(res => this.handleRefreshToken(res)) - .catch(res => { - // The refresh token is invalid? - if (res.status === 400 && res.error.error === 'invalid_grant') { - console.error('Cannot refresh token -> logout...') - this.logout() - this.router.navigate(['/login']) - - return Observable.throw({ - error: 'You need to reconnect.' - }) - } - - return this.restExtractor.handleError(res) + .catch(err => { + console.error(err) + console.log('Cannot refresh token -> logout...') + this.logout() + this.router.navigate(['/login']) + + return Observable.throw({ + error: 'You need to reconnect.' + }) }) } @@ -205,16 +190,18 @@ 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.author = res.author - - this.user.save() - } - ) + .subscribe( + res => { + this.user.displayNSFW = res.displayNSFW + this.user.role = res.role + this.user.videoChannels = res.videoChannels + this.user.account = res.account + + this.user.save() + + this.userInformationLoaded.next(true) + } + ) } private mergeUserInformation (obj: UserLoginWithUsername): Observable { @@ -229,7 +216,7 @@ export class AuthService { displayNSFW: res.displayNSFW, email: res.email, videoQuota: res.videoQuota, - author: res.author, + account: res.account, videoChannels: res.videoChannels } @@ -247,7 +234,7 @@ export class AuthService { displayNSFW: obj.displayNSFW, videoQuota: obj.videoQuota, videoChannels: obj.videoChannels, - author: obj.author + account: obj.account } const hashTokens = { accessToken: obj.access_token, @@ -259,6 +246,7 @@ export class AuthService { this.user.save() this.setStatus(AuthStatus.LoggedIn) + this.userInformationLoaded.next(true) } private handleRefreshToken (obj: UserRefreshToken) {