Changes in / [466d1ac:8b6791f]
- Location:
- src
- Files:
-
- 1 deleted
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Clients/Angular/finki-chattery/src/app/app-routing.module.ts
r466d1ac r8b6791f 1 1 import { NgModule } from '@angular/core'; 2 2 import { Routes, RouterModule } from '@angular/router'; 3 import { AuthCallbackComponent } from './auth-callback/auth-callback.component';4 3 import { AuthorizedGuard } from './core/guards/authorized.guard'; 5 4 6 5 const routes: Routes = [ 7 {8 path: 'auth-callback',9 component: AuthCallbackComponent10 },11 6 { 12 7 path: 'questioning', -
src/Clients/Angular/finki-chattery/src/app/app.component.ts
r466d1ac r8b6791f 1 1 import { Component, OnInit } from '@angular/core'; 2 import { LoaderService } from './core/services';2 import { LoaderService, RedirectService } from './core/services'; 3 3 4 4 @Component({ … … 8 8 }) 9 9 export class AppComponent implements OnInit { 10 constructor(public loader: LoaderService ) {}10 constructor(public loader: LoaderService, private redirect: RedirectService) {} 11 11 12 ngOnInit(): void {} 12 ngOnInit(): void { 13 this.redirect.redirectLoggedInUser(); 14 } 13 15 } -
src/Clients/Angular/finki-chattery/src/app/app.module.ts
r466d1ac r8b6791f 10 10 import { CoreModule } from './core/core.module'; 11 11 import { translateConfiguration, TranslateFromJsonService } from './shared-app/services'; 12 import { AuthCallbackComponent } from './auth-callback/auth-callback.component';13 12 14 13 @NgModule({ 15 declarations: [AppComponent , AuthCallbackComponent],14 declarations: [AppComponent], 16 15 imports: [ 17 16 BrowserModule, -
src/Clients/Angular/finki-chattery/src/app/core/guards/authorized.guard.ts
r466d1ac r8b6791f 1 1 import { Injectable } from '@angular/core'; 2 2 import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; 3 3 import { Observable } from 'rxjs'; 4 import { map } from 'rxjs/operators'; 4 5 import { AuthService } from '../services'; 5 6 … … 10 11 constructor(private auth: AuthService) {} 11 12 12 canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { 13 if (this.auth.isLoggedIn()) { 14 return true; 15 } 13 canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> { 14 return this.auth.isLoggedIn().pipe( 15 map((loggedIn) => { 16 if (!loggedIn) { 17 this.auth.login(); 18 return false; 19 } 16 20 17 this.auth.login(); 18 return false; 21 return true; 22 }) 23 ); 19 24 } 20 25 } -
src/Clients/Angular/finki-chattery/src/app/core/interceptors/token.interceptor.ts
r466d1ac r8b6791f 2 2 import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http'; 3 3 import { Observable } from 'rxjs'; 4 import { switchMap } from 'rxjs/operators'; 4 5 5 6 import { AuthService } from '../services'; … … 15 16 } 16 17 17 const requestToForward = request.clone({ 18 setHeaders: { Authorization: `Bearer ${this.auth.currentUserToken()}` } 19 }); 20 21 return next.handle(requestToForward); 18 return this.auth.currentUserToken().pipe( 19 switchMap((token) => { 20 const requestToForward = request.clone({ 21 setHeaders: { Authorization: `Bearer ${token}` } 22 }); 23 return next.handle(requestToForward); 24 }) 25 ); 22 26 } 23 27 } -
src/Clients/Angular/finki-chattery/src/app/core/services/auth.service.ts
r466d1ac r8b6791f 1 1 import { Injectable } from '@angular/core'; 2 import { User, UserManager } from 'oidc-client'; 3 import { Observable, of } from 'rxjs'; 2 import { UserManager } from 'oidc-client'; 3 import { Observable, from, of } from 'rxjs'; 4 import { map, switchMap } from 'rxjs/operators'; 4 5 5 6 import { environment } from '@env/environment'; … … 14 15 authority: environment.identityRoute, 15 16 client_id: environment.identityClientId, 16 redirect_uri: `${window.location.origin} /auth-callback`,17 redirect_uri: `${window.location.origin}`, 17 18 response_type: 'id_token token', 18 19 scope: 'openid app.api.finki-chattery profile', 19 20 post_logout_redirect_uri: window.location.origin 20 21 }); 21 22 public user: ApplicationUser | null = null;23 public oidcUser: User | null = null;24 22 25 23 constructor(private baseApi: BaseApiService) {} … … 33 31 } 34 32 35 public isLoggedIn(): boolean { 36 if (this.oidcUser) { 37 return !this.oidcUser.expired; 38 } 39 return false; 33 public isLoggedIn(): Observable<boolean> { 34 return from(this.userManager.getUser()).pipe( 35 map((user) => { 36 if (user) { 37 if (user.expired) { 38 return false; 39 } 40 41 return true; 42 } 43 44 return false; 45 }) 46 ); 40 47 } 41 48 42 public currentUser(): ApplicationUser | null { 43 return this.user; 49 public currentUser(): Observable<ApplicationUser | null> { 50 return from(this.userManager.getUser()).pipe( 51 map((user) => { 52 if (!user) { 53 return null; 54 } 55 56 return new ApplicationUser( 57 user.profile.id, 58 user.profile.userType, 59 user.profile.emailAddress, 60 user.profile.username, 61 user.profile.isVerified 62 ); 63 }) 64 ); 44 65 } 45 66 46 public currentUserToken(): string { 47 if (this.oidcUser) { 48 return this.oidcUser.access_token; 49 } 67 public currentUserToken(): Observable<string> { 68 return from(this.userManager.getUser()).pipe( 69 map((user) => { 70 if (user?.access_token) { 71 return user.access_token; 72 } 50 73 51 return ''; 74 return ''; 75 }) 76 ); 52 77 } 53 78 54 79 public selfUserDto(): Observable<SelfUserResponse | null> { 55 if (this.isLoggedIn()) { 56 return this.baseApi.getSelfUser(); 57 } 58 return of(null); 80 return this.isLoggedIn().pipe( 81 switchMap((loggedIn) => { 82 if (loggedIn) { 83 return this.baseApi.getSelfUser(); 84 } 85 return of(null); 86 }) 87 ); 59 88 } 60 89 61 public async completeAuthentication(): Promise<void> { 62 return await this.userManager.signinRedirectCallback().then((user: User) => { 63 this.oidcUser = user; 64 this.user = new ApplicationUser( 65 user.profile.id, 66 user.profile.userType, 67 user.profile.emailAddress, 68 user.profile.username, 69 user.profile.isVerified 70 ); 71 }); 90 public signupCallback(): Observable<boolean> { 91 return from(this.userManager.signinRedirectCallback()).pipe(map((user) => user !== null)); 72 92 } 73 93 } -
src/Clients/Angular/finki-chattery/src/app/core/services/redirect.service.ts
r466d1ac r8b6791f 1 1 import { Injectable } from '@angular/core'; 2 2 import { Router } from '@angular/router'; 3 import { switchMap } from 'rxjs/operators'; 3 4 4 5 import { ApplicationUserType } from 'src/app/shared-app/models'; … … 12 13 13 14 public redirectLoggedInUser(): void { 14 const currentUser = this.auth.user; 15 16 if (currentUser) { 17 switch (currentUser.userType) { 18 case ApplicationUserType.Student: 19 this.router.navigateByUrl(`questioning/preview`); 20 break; 21 case ApplicationUserType.Teacher: 22 break; 23 } 24 } 15 this.auth 16 .signupCallback() 17 .pipe(switchMap(() => this.auth.currentUser())) 18 .subscribe((currentUser) => { 19 if (currentUser) { 20 switch (currentUser.userType) { 21 case ApplicationUserType.Student: 22 this.router.navigateByUrl(`questioning/preview`); 23 break; 24 case ApplicationUserType.Teacher: 25 break; 26 } 27 } 28 }); 25 29 } 26 30 } -
src/Clients/Angular/finki-chattery/src/app/shared-app/components/generic/header/header.component.html
r466d1ac r8b6791f 2 2 <span>FinkiChattery</span> 3 3 <span class="right"></span> 4 <app-button *ngIf=" auth.isLoggedIn()" (action)="logout()" [buttonType]="ButtonType.CallToAction">Logout</app-button>4 <app-button *ngIf="loggedIn" (action)="logout()" [buttonType]="ButtonType.CallToAction">Logout</app-button> 5 5 </mat-toolbar> -
src/Clients/Angular/finki-chattery/src/app/shared-app/components/generic/header/header.component.ts
r466d1ac r8b6791f 10 10 export class HeaderComponent implements OnInit { 11 11 ButtonType = ButtonType; 12 loggedIn = false; 12 13 13 constructor(p ublicauth: AuthService) {}14 constructor(private auth: AuthService) {} 14 15 15 ngOnInit(): void {} 16 ngOnInit(): void { 17 this.auth.isLoggedIn().subscribe((loggedIn) => (this.loggedIn = loggedIn)); 18 } 16 19 17 20 logout(): void { -
src/FinkiChattery/FinkiChattery.Identity/appsettings.Development.json
r466d1ac r8b6791f 10 10 { 11 11 "allowedCorsOrigins": [ "http://localhost:4200" ], 12 "redirectUris": [ "http://localhost:4200 /auth-callback" ],12 "redirectUris": [ "http://localhost:4200" ], 13 13 "postLogoutRedirectUris": [ "http://localhost:4200" ] 14 14 }
Note:
See TracChangeset
for help on using the changeset viewer.