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