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 { MyUser as UserServerModel, OAuthClientLocal, User, UserLogin, UserRefreshToken } from '@shared/models'
+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'
import { AuthStatus } from './auth-status.model'
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)
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 => {
]
}
+ 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)
.pipe(catchError(res => this.restExtractor.handleError(res)))
- .subscribe(
- res => {
+ .subscribe({
+ next: res => {
this.clientId = res.client_id
this.clientSecret = res.client_secret
console.log('Client credentials loaded.')
},
- error => {
- let errorMessage = error.message
+ error: err => {
+ let errorMessage = err.message
- if (error.status === 403) {
- errorMessage = $localize`Cannot retrieve OAuth Client credentials: ${error.text}.
+ if (err.status === HttpStatusCode.FORBIDDEN_403) {
+ errorMessage = $localize`Cannot retrieve OAuth Client credentials: ${err.text}.
Ensure you have correctly configured PeerTube (config/ directory), in particular the "webserver" section.`
}
// We put a bigger timeout: this is an important message
this.notifier.error(errorMessage, $localize`Error`, 7000)
}
- )
+ })
}
getRefreshToken () {
const headers = new HttpHeaders().set('Authorization', authHeaderValue)
this.http.post<{ redirectUrl?: string }>(AuthService.BASE_REVOKE_TOKEN_URL, {}, { headers })
- .subscribe(
- res => {
- if (res.redirectUrl) {
- window.location.href = res.redirectUrl
- }
- },
+ .subscribe({
+ next: res => {
+ if (res.redirectUrl) {
+ window.location.href = res.redirectUrl
+ }
+ },
- err => console.error(err)
- )
+ error: err => console.error(err)
+ })
this.user = null
- AuthUser.flush()
-
this.setStatus(AuthStatus.LoggedOut)
this.hotkeysService.remove(this.hotkeys)
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
this.logout()
this.router.navigate([ '/login' ])
- return observableThrowError({
+ return observableThrowError(() => ({
error: $localize`You need to reconnect.`
- })
+ }))
}),
share()
)
}
this.mergeUserInformation(obj)
- .subscribe(
- res => {
+ .subscribe({
+ next: res => {
this.user.patch(res)
- this.user.save()
this.userInformationLoaded.next(true)
}
- )
+ })
}
private mergeUserInformation (obj: UserLoginWithUsername): Observable<UserLoginWithUserInformation> {
}
this.user = new AuthUser(obj, hashTokens)
- this.user.save()
this.setStatus(AuthStatus.LoggedIn)
this.userInformationLoaded.next(true)
private handleRefreshToken (obj: UserRefreshToken) {
this.user.refreshTokens(obj.access_token, obj.refresh_token)
- this.user.save()
+ this.tokensRefreshed.next()
}
private setStatus (status: AuthStatus) {