diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 7744258..4893c33 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,5 +1,6 @@ -import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; import { AuthService } from './services/auth.service'; +import { ChatService } from './services/chat.service'; @Component({ // tslint:disable-next-line:component-selector @@ -11,11 +12,17 @@ export class AppComponent implements OnInit { showOverlay = true; constructor( - private authService: AuthService + private authService: AuthService, + private chatService: ChatService ) { } async ngOnInit(): Promise { - await this.authService.isLogged; + console.log('AppComponent.ngInit()'); + const isLogged = await this.authService.isLogged; this.showOverlay = false; + if (isLogged) { + console.log('GO TO SSO LOGIN'); + this.chatService.goToSsoLogin(); + } } } diff --git a/src/app/pages/edit-travel/edit-travel.component.ts b/src/app/pages/edit-travel/edit-travel.component.ts index 6a2e21a..a641917 100644 --- a/src/app/pages/edit-travel/edit-travel.component.ts +++ b/src/app/pages/edit-travel/edit-travel.component.ts @@ -23,7 +23,7 @@ export class EditTravelComponent implements OnInit { ngOnInit(): void { this.route.queryParams.subscribe(params => { - const travelId = this.route.snapshot.paramMap.get('id') as unknown as TravelId; + const travelId = params.get('id') as unknown as TravelId; this.apiService.getTravel(travelId) .subscribe(res => { diff --git a/src/app/pages/home/home.component.ts b/src/app/pages/home/home.component.ts index f1a0db4..4cb0add 100644 --- a/src/app/pages/home/home.component.ts +++ b/src/app/pages/home/home.component.ts @@ -1,6 +1,7 @@ import { Component, OnInit } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { AuthService } from 'src/app/services/auth.service'; +import { ChatService } from 'src/app/services/chat.service'; import { Travel } from '../../entities/travel'; import { ApiService } from '../../services/api.service'; @@ -29,11 +30,16 @@ export class HomeComponent implements OnInit { private route: ActivatedRoute, private router: Router, private authService: AuthService, - private apiService: ApiService + private apiService: ApiService, + private chatService: ChatService ) { } ngOnInit(): void { + const loginToken = this.route.snapshot.queryParams['loginToken']; + if (loginToken) { console.log('loginToken=' + this.route.snapshot.queryParams['loginToken']); + this.chatService.login(loginToken); + } } newTravel(): void { diff --git a/src/app/services/api.service.ts b/src/app/services/api.service.ts index a8946f5..8581820 100644 --- a/src/app/services/api.service.ts +++ b/src/app/services/api.service.ts @@ -1,13 +1,12 @@ -import { Injectable } from '@angular/core'; import { HttpClient, HttpErrorResponse } from '@angular/common/http'; +import { Injectable } from '@angular/core'; import { Observable, throwError } from 'rxjs'; import { catchError, retry } from 'rxjs/operators'; -import { ApiResponse } from '../entities/api-response'; -import { Travel, TravelId } from '../entities/travel'; import { PUBLIC_API_URL, TRAVEL_API_URL, USER_API_URL } from '../app.config'; +import { ApiResponse } from '../entities/api-response'; import { ListDto } from '../entities/list-dto'; +import { Travel, TravelId } from '../entities/travel'; import { User } from '../entities/user'; -import { AuthService } from './auth.service'; export type ApiCall = (params?: { [param: string]: any }) => (Observable>>); @@ -16,7 +15,6 @@ export type ApiCall = (params?: { [param: string]: any }) => (Observable( - // url: string, - // params?: { [param: string]: any }, - // body?: any - // ): Observable> { - // const observable = new Observable>(observer => { - // this.authService.configurePromise.then(logged => { - // if (logged) { - // this.callApi(url, params, body).subscribe(res => { - // observer.next(res); - // }); - // } - // else { - // console.error('callProtectedApi: NOT LOGGED'); - // observer.error('NOT LOGGED'); - // } - // }); - // }); - // return observable; - // } - private handleError(error: HttpErrorResponse): Observable { if (error.error instanceof ErrorEvent) { // A client-side or network error occurred. Handle it accordingly. diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index 432e743..6f649b5 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -1,7 +1,7 @@ import { Injectable } from '@angular/core'; import { OAuthService } from 'angular-oauth2-oidc'; +import { filter } from 'rxjs/operators'; import { authConfig } from '../auth.config'; -import { Travel } from '../entities/travel'; import { User } from '../entities/user'; @Injectable({ @@ -38,9 +38,19 @@ export class AuthService { this.oauthService.configure(authConfig); this.oauthService.setupAutomaticSilentRefresh(); + + // Automatically load user profile + this.oauthService.events + .pipe(filter((e) => e.type === 'token_received')) + .subscribe((_) => { + console.log('auth.service token_received state=', this.oauthService.state); + }); + return new Promise((resolve) => { this.oauthService.loadDiscoveryDocumentAndTryLogin().then((_) => { if (this.oauthService.getIdentityClaims() != null) { + console.log('IDENTITY CLAIMS'); + console.log(this.oauthService.getIdentityClaims()); if (this.oauthService.hasValidIdToken() && this.oauthService.hasValidAccessToken()) { console.log('NO NEED FOR TOKEN REFRESH'); resolve(true); @@ -62,18 +72,5 @@ export class AuthService { } }); }); - - // Automatically load user profile - // this.oauthService.events - // .pipe(filter((e) => e.type === 'token_received')) - // .subscribe((_) => { - // console.log('state', this.oauthService.state); - // this.oauthService.loadUserProfile(); - - // this.apiService.getUser().subscribe(user => { - // console.log('USER'); - // console.log(user); - // }); - // }); } } diff --git a/src/app/services/chat.service.ts b/src/app/services/chat.service.ts new file mode 100644 index 0000000..c60add2 --- /dev/null +++ b/src/app/services/chat.service.ts @@ -0,0 +1,31 @@ +import { HttpClient } from '@angular/common/http'; +import { Injectable } from '@angular/core'; +import { Router } from '@angular/router'; +// import sdk from 'matrix-js-sdk'; + +@Injectable({ + providedIn: 'root' +}) +export class ChatService { + constructor( + private router: Router, + private http: HttpClient + ) { } + + goToSsoLogin(): void { + // window.location.href = 'https://matrix.fosil.eu/_matrix/client/r0/login/sso/redirect?redirectUrl=http://localhost:4200/'; + } + + login(loginToken: string): Promise { + return this.http.post( + 'https://matrix.fosil.eu/_matrix/client/r0/login', + { + initial_device_display_name: 'Okupa mi coche', + token: loginToken, + type: 'm.login.token' + } + ).toPromise().then(res => { + + }); + } +} diff --git a/src/app/views/travel-form/travel-form.component.html b/src/app/views/travel-form/travel-form.component.html index b0af5d2..1a71537 100644 --- a/src/app/views/travel-form/travel-form.component.html +++ b/src/app/views/travel-form/travel-form.component.html @@ -31,7 +31,7 @@