diff --git a/src/app/authentication/services/auth.service.ts b/src/app/authentication/services/auth.service.ts index f5d40f6457417891f25df521bde406347b267631..503a65155f8e6d629a6c738343178c784d57c348 100644 --- a/src/app/authentication/services/auth.service.ts +++ b/src/app/authentication/services/auth.service.ts @@ -42,7 +42,7 @@ export class AuthService { FdpSignUp(user: SmartHarvesterUser): Observable<any> { const httpOptions = { - headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Authorization' : `Baerer ${this.getToken()}` }) + headers: new HttpHeaders({ 'Content-Type': 'application/json', Authorization : `Baerer ${this.getToken()}` }) }; return this.http.post(`${this.baseUrl}/harvester/auth/signup`, { @@ -72,7 +72,7 @@ export class AuthService { } updateToken(token) { - this.tokenService.saveTokenSmartHarveser(token) + this.tokenService.saveTokenSmartHarveser(token); } fetchToken(code: string, state: string): Observable<any> { @@ -98,4 +98,15 @@ export class AuthService { logout() { return this.http.post(this.baseUrl + '/harvester/logout', this.getToken(), this.httpOptions); } + + getUser(accessToken: string): Observable<any> { + const options = { + headers: new HttpHeaders({ + 'Content-Type': 'application/json', + 'Accept': 'application/json', + 'Authorization': 'Bearer ' + accessToken + }) + }; + return this.http.get(this.baseUrl + '/harvester/api/username', options); + } } diff --git a/src/app/callback/callback.component.ts b/src/app/callback/callback.component.ts index 2940c875570e2179d5cf03dbb6675b21d36588e7..cf15828fbbfa0183248f05c7bd1b46b1a1bfb198 100644 --- a/src/app/callback/callback.component.ts +++ b/src/app/callback/callback.component.ts @@ -1,6 +1,11 @@ import { Component, OnInit } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { AuthService } from '../authentication/services/auth.service'; +import {environment} from '../../environments/environment'; +import {takeUntil} from 'rxjs/operators'; +import {Subject} from 'rxjs'; +import {SmartHarvesterUser} from '../user/model/user'; +import {TokenStorageService} from '../authentication/services/token-storage.service'; @Component({ selector: 'app-callback', @@ -9,15 +14,35 @@ import { AuthService } from '../authentication/services/auth.service'; }) export class CallbackComponent implements OnInit { - constructor(private route: ActivatedRoute, private router: Router, private authService: AuthService) { } + user: SmartHarvesterUser = new SmartHarvesterUser(); + + constructor(private route: ActivatedRoute, private router: Router, private authService: AuthService, + private tokenService: TokenStorageService) { } ngOnInit(): void { this.route.queryParams.subscribe(p => { this.authService.fetchToken(p.code, p.state).subscribe(data => { this.authService.updateToken(data.accessToken); - this.router.navigate(['/dashboard']); + this.authService.getUser(data.accessToken) + .subscribe( + response => { + this.user.email = response['principal']['userInfo']['email']; + this.user.lastName = response['principal']['userInfo']['familyName']; + this.user.firstName = response['principal']['userInfo']['givenName']; + this.tokenService.saveUser(this.user); + }, + err => { + console.log(err.error.message); + this.authService.logout().subscribe( + value => this.router.navigateByUrl('/login'), + error => console.log(error), + () => this.tokenService.removeToken() + ); + }, + () => this.router.navigate(['/dashboard'])); }); }); + } } diff --git a/src/app/dashboard/dashboard.component.ts b/src/app/dashboard/dashboard.component.ts index 632c8d7e08ad4d88fa633eba0d23fd32c374e6b9..a66ce8dd0c0ddd54d712bcf8c8251e1f2bb285b4 100644 --- a/src/app/dashboard/dashboard.component.ts +++ b/src/app/dashboard/dashboard.component.ts @@ -76,8 +76,8 @@ export class DashboardComponent implements OnInit { ]; constructor(private readonly sidebarService: NbSidebarService, - private authService: AuthService, - private tokeService: TokenStorageService, private route: Router, private http: HttpClient) { } + private authService: AuthService, + private tokeService: TokenStorageService, private route: Router, private http: HttpClient) { } ngOnInit(): void { this.routerUrl = this.route.url; @@ -86,28 +86,9 @@ export class DashboardComponent implements OnInit { this.routerUrl = event.url; } }); - if (this.tokeService.getUser() === null) { - const httpOptions = { - headers: new HttpHeaders({ - 'Accept': 'application/json', - 'Content-Type': 'application/json', - 'Authorization': 'Bearer ' + this.authService.getToken() - }) - }; - this.http.get(environment.smartharvesterUrl + '/harvester/api/username', httpOptions) - .pipe(takeUntil(this._isDead)) - .subscribe( - data => { - this.user.email = data['principal']['userInfo']['email']; - this.user.lastName = data['principal']['userInfo']['familyName']; - this.user.firstName = data['principal']['userInfo']['givenName']; - this.tokeService.saveUser(this.user); - }, - err => console.log(err.error.message), - () => {}); - } else { - this.user = this.tokeService.getUser(); - } + + this.user = this.tokeService.getUser(); + } toggleSidebar(): boolean {