import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { Router } from '@angular/router'
-import { NotificationsService } from 'angular2-notifications'
-import { OAuthClientLocal, User as UserServerModel, UserRefreshToken } from '../../../../../shared'
+import { Notifier } from '@app/core/notification/notifier.service'
+import { OAuthClientLocal, MyUser 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 { RestExtractor } from '../../shared/rest/rest-extractor.service'
import { AuthStatus } from './auth-status.model'
import { AuthUser } from './auth-user.model'
import { objectToUrlEncoded } from '@app/shared/misc/utils'
-import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
+import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage'
import { I18n } from '@ngx-translate/i18n-polyfill'
-import { HotkeysService, Hotkey } from 'angular2-hotkeys'
+import { Hotkey, HotkeysService } from 'angular2-hotkeys'
interface UserLoginWithUsername extends UserLogin {
access_token: string
export class AuthService {
private static BASE_CLIENT_URL = environment.apiUrl + '/api/v1/oauth-clients/local'
private static BASE_TOKEN_URL = environment.apiUrl + '/api/v1/users/token'
+ private static BASE_REVOKE_TOKEN_URL = environment.apiUrl + '/api/v1/users/revoke-token'
private static BASE_USER_INFORMATION_URL = environment.apiUrl + '/api/v1/users/me'
private static LOCAL_STORAGE_OAUTH_CLIENT_KEYS = {
CLIENT_ID: 'client_id',
loginChangedSource: Observable<AuthStatus>
userInformationLoaded = new ReplaySubject<boolean>(1)
hotkeys: Hotkey[]
- redirectUrl: string
private clientId: string = peertubeLocalStorage.getItem(AuthService.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID)
private clientSecret: string = peertubeLocalStorage.getItem(AuthService.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_SECRET)
constructor (
private http: HttpClient,
- private notificationsService: NotificationsService,
+ private notifier: Notifier,
private hotkeysService: HotkeysService,
private restExtractor: RestExtractor,
private router: Router,
)
}
- // We put a bigger timeout
- // This is an important message
- this.notificationsService.error(this.i18n('Error'), errorMessage, { timeOut: 7000 })
+ // We put a bigger timeout: this is an important message
+ this.notifier.error(errorMessage, this.i18n('Error'), 7000)
}
)
}
return !!this.getAccessToken()
}
- login (username: string, password: string) {
+ login (username: string, password: string, token?: string) {
// Form url encoded
const body = {
client_id: this.clientId,
password
}
+ if (token) Object.assign(body, { externalAuthToken: token })
+
const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
return this.http.post<UserLogin>(AuthService.BASE_TOKEN_URL, objectToUrlEncoded(body), { headers })
.pipe(
}
logout () {
- // TODO: make an HTTP request to revoke the tokens
+ const authHeaderValue = this.getRequestHeaderValue()
+ const headers = new HttpHeaders().set('Authorization', authHeaderValue)
+
+ this.http.post<void>(AuthService.BASE_REVOKE_TOKEN_URL, {}, { headers })
+ .subscribe(
+ () => { /* nothing to do */ },
+
+ err => console.error(err)
+ )
+
+
this.user = null
AuthUser.flush()
this.setStatus(AuthStatus.LoggedOut)
this.hotkeysService.remove(this.hotkeys)
-
- this.redirectUrl = null
}
refreshAccessToken () {
}
refreshUserInformation () {
- const obj = {
+ const obj: UserLoginWithUsername = {
access_token: this.user.getAccessToken(),
refresh_token: null,
token_type: this.user.getTokenType(),