Changeset 466d1ac for src/Clients


Ignore:
Timestamp:
10/24/21 11:29:58 (3 years ago)
Author:
Стојков Марко <mst@…>
Branches:
dev
Children:
7899209, a3b5f34
Parents:
8b6791f (diff), 5ad5988 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merged feature/restructured-oidc-client into dev

Location:
src/Clients/Angular/finki-chattery/src/app
Files:
1 added
9 edited

Legend:

Unmodified
Added
Removed
  • src/Clients/Angular/finki-chattery/src/app/app-routing.module.ts

    r8b6791f r466d1ac  
    11import { NgModule } from '@angular/core';
    22import { Routes, RouterModule } from '@angular/router';
     3import { AuthCallbackComponent } from './auth-callback/auth-callback.component';
    34import { AuthorizedGuard } from './core/guards/authorized.guard';
    45
    56const routes: Routes = [
     7  {
     8    path: 'auth-callback',
     9    component: AuthCallbackComponent
     10  },
    611  {
    712    path: 'questioning',
  • src/Clients/Angular/finki-chattery/src/app/app.component.ts

    r8b6791f r466d1ac  
    11import { Component, OnInit } from '@angular/core';
    2 import { LoaderService, RedirectService } from './core/services';
     2import { LoaderService } from './core/services';
    33
    44@Component({
     
    88})
    99export class AppComponent implements OnInit {
    10   constructor(public loader: LoaderService, private redirect: RedirectService) {}
     10  constructor(public loader: LoaderService) {}
    1111
    12   ngOnInit(): void {
    13     this.redirect.redirectLoggedInUser();
    14   }
     12  ngOnInit(): void {}
    1513}
  • src/Clients/Angular/finki-chattery/src/app/app.module.ts

    r8b6791f r466d1ac  
    1010import { CoreModule } from './core/core.module';
    1111import { translateConfiguration, TranslateFromJsonService } from './shared-app/services';
     12import { AuthCallbackComponent } from './auth-callback/auth-callback.component';
    1213
    1314@NgModule({
    14   declarations: [AppComponent],
     15  declarations: [AppComponent, AuthCallbackComponent],
    1516  imports: [
    1617    BrowserModule,
  • src/Clients/Angular/finki-chattery/src/app/core/guards/authorized.guard.ts

    r8b6791f r466d1ac  
    11import { Injectable } from '@angular/core';
    22import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
    3 import { Observable } from 'rxjs';
    4 import { map } from 'rxjs/operators';
     3
    54import { AuthService } from '../services';
    65
     
    1110  constructor(private auth: AuthService) {}
    1211
    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    }
    2016
    21         return true;
    22       })
    23     );
     17    this.auth.login();
     18    return false;
    2419  }
    2520}
  • src/Clients/Angular/finki-chattery/src/app/core/interceptors/token.interceptor.ts

    r8b6791f r466d1ac  
    22import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
    33import { Observable } from 'rxjs';
    4 import { switchMap } from 'rxjs/operators';
    54
    65import { AuthService } from '../services';
     
    1615    }
    1716
    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);
    2622  }
    2723}
  • src/Clients/Angular/finki-chattery/src/app/core/services/auth.service.ts

    r8b6791f r466d1ac  
    11import { Injectable } from '@angular/core';
    2 import { UserManager } from 'oidc-client';
    3 import { Observable, from, of } from 'rxjs';
    4 import { map, switchMap } from 'rxjs/operators';
     2import { User, UserManager } from 'oidc-client';
     3import { Observable, of } from 'rxjs';
    54
    65import { environment } from '@env/environment';
     
    1514    authority: environment.identityRoute,
    1615    client_id: environment.identityClientId,
    17     redirect_uri: `${window.location.origin}`,
     16    redirect_uri: `${window.location.origin}/auth-callback`,
    1817    response_type: 'id_token token',
    1918    scope: 'openid app.api.finki-chattery profile',
    2019    post_logout_redirect_uri: window.location.origin
    2120  });
     21
     22  public user: ApplicationUser | null = null;
     23  public oidcUser: User | null = null;
    2224
    2325  constructor(private baseApi: BaseApiService) {}
     
    3133  }
    3234
    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;
    4740  }
    4841
    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;
    6544  }
    6645
    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    }
    7350
    74         return '';
    75       })
    76     );
     51    return '';
    7752  }
    7853
    7954  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);
    8859  }
    8960
    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    });
    9272  }
    9373}
  • src/Clients/Angular/finki-chattery/src/app/core/services/redirect.service.ts

    r8b6791f r466d1ac  
    11import { Injectable } from '@angular/core';
    22import { Router } from '@angular/router';
    3 import { switchMap } from 'rxjs/operators';
    43
    54import { ApplicationUserType } from 'src/app/shared-app/models';
     
    1312
    1413  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    }
    2925  }
    3026}
  • src/Clients/Angular/finki-chattery/src/app/shared-app/components/generic/header/header.component.html

    r8b6791f r466d1ac  
    22  <span>FinkiChattery</span>
    33  <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>
    55</mat-toolbar>
  • src/Clients/Angular/finki-chattery/src/app/shared-app/components/generic/header/header.component.ts

    r8b6791f r466d1ac  
    1010export class HeaderComponent implements OnInit {
    1111  ButtonType = ButtonType;
    12   loggedIn = false;
    1312
    14   constructor(private auth: AuthService) {}
     13  constructor(public auth: AuthService) {}
    1514
    16   ngOnInit(): void {
    17     this.auth.isLoggedIn().subscribe((loggedIn) => (this.loggedIn = loggedIn));
    18   }
     15  ngOnInit(): void {}
    1916
    2017  logout(): void {
Note: See TracChangeset for help on using the changeset viewer.