1
0
Fork 0

Fix error message on token expiration

This commit is contained in:
Chocobozzz 2018-05-07 18:40:51 +02:00
parent fd5e57bbe2
commit 47f8de2834
No known key found for this signature in database
GPG key ID: 583A612D890159BE
2 changed files with 22 additions and 12 deletions

View file

@ -39,6 +39,7 @@ export class AuthService {
private clientSecret: string private clientSecret: string
private loginChanged: Subject<AuthStatus> private loginChanged: Subject<AuthStatus>
private user: AuthUser = null private user: AuthUser = null
private refreshingTokenObservable: Observable<any>
constructor ( constructor (
private http: HttpClient, private http: HttpClient,
@ -144,6 +145,8 @@ export class AuthService {
} }
refreshAccessToken () { refreshAccessToken () {
if (this.refreshingTokenObservable) return this.refreshingTokenObservable
console.log('Refreshing token...') console.log('Refreshing token...')
const refreshToken = this.getRefreshToken() const refreshToken = this.getRefreshToken()
@ -157,18 +160,23 @@ export class AuthService {
const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded') const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
return this.http.post<UserRefreshToken>(AuthService.BASE_TOKEN_URL, body, { headers }) this.refreshingTokenObservable = this.http.post<UserRefreshToken>(AuthService.BASE_TOKEN_URL, body, { headers })
.map(res => this.handleRefreshToken(res)) .map(res => this.handleRefreshToken(res))
.catch(err => { .do(() => this.refreshingTokenObservable = null)
console.error(err) .catch(err => {
console.log('Cannot refresh token -> logout...') this.refreshingTokenObservable = null
this.logout()
this.router.navigate(['/login'])
return Observable.throw({ console.error(err)
error: 'You need to reconnect.' console.log('Cannot refresh token -> logout...')
}) this.logout()
}) this.router.navigate([ '/login' ])
return Observable.throw({
error: 'You need to reconnect.'
})
})
return this.refreshingTokenObservable
} }
refreshUserInformation () { refreshUserInformation () {

View file

@ -33,13 +33,15 @@ export class RestExtractor {
return target return target
} }
handleError (err: HttpErrorResponse) { handleError (err: any) {
let errorMessage let errorMessage
if (err.error instanceof Error) { if (err.error instanceof Error) {
// A client-side or network error occurred. Handle it accordingly. // A client-side or network error occurred. Handle it accordingly.
errorMessage = err.error.message errorMessage = err.error.message
console.error('An error occurred:', errorMessage) console.error('An error occurred:', errorMessage)
} else if (typeof err.error === 'string') {
errorMessage = err.error
} else if (err.status !== undefined) { } else if (err.status !== undefined) {
// A server-side error occurred. // A server-side error occurred.
if (err.error && err.error.errors) { if (err.error && err.error.errors) {