Changes in / [466d1ac:8b6791f]


Ignore:
Location:
src
Files:
1 deleted
10 edited

Legend:

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

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

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

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

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

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

    r466d1ac r8b6791f  
    11import { Injectable } from '@angular/core';
    2 import { User, UserManager } from 'oidc-client';
    3 import { Observable, of } from 'rxjs';
     2import { UserManager } from 'oidc-client';
     3import { Observable, from, of } from 'rxjs';
     4import { map, switchMap } from 'rxjs/operators';
    45
    56import { environment } from '@env/environment';
     
    1415    authority: environment.identityRoute,
    1516    client_id: environment.identityClientId,
    16     redirect_uri: `${window.location.origin}/auth-callback`,
     17    redirect_uri: `${window.location.origin}`,
    1718    response_type: 'id_token token',
    1819    scope: 'openid app.api.finki-chattery profile',
    1920    post_logout_redirect_uri: window.location.origin
    2021  });
    21 
    22   public user: ApplicationUser | null = null;
    23   public oidcUser: User | null = null;
    2422
    2523  constructor(private baseApi: BaseApiService) {}
     
    3331  }
    3432
    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    );
    4047  }
    4148
    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    );
    4465  }
    4566
    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        }
    5073
    51     return '';
     74        return '';
     75      })
     76    );
    5277  }
    5378
    5479  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    );
    5988  }
    6089
    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));
    7292  }
    7393}
  • src/Clients/Angular/finki-chattery/src/app/core/services/redirect.service.ts

    r466d1ac r8b6791f  
    11import { Injectable } from '@angular/core';
    22import { Router } from '@angular/router';
     3import { switchMap } from 'rxjs/operators';
    34
    45import { ApplicationUserType } from 'src/app/shared-app/models';
     
    1213
    1314  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      });
    2529  }
    2630}
  • src/Clients/Angular/finki-chattery/src/app/shared-app/components/generic/header/header.component.html

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

    r466d1ac r8b6791f  
    1010export class HeaderComponent implements OnInit {
    1111  ButtonType = ButtonType;
     12  loggedIn = false;
    1213
    13   constructor(public auth: AuthService) {}
     14  constructor(private auth: AuthService) {}
    1415
    15   ngOnInit(): void {}
     16  ngOnInit(): void {
     17    this.auth.isLoggedIn().subscribe((loggedIn) => (this.loggedIn = loggedIn));
     18  }
    1619
    1720  logout(): void {
  • src/FinkiChattery/FinkiChattery.Identity/appsettings.Development.json

    r466d1ac r8b6791f  
    1010                                {
    1111                                        "allowedCorsOrigins": [ "http://localhost:4200" ],
    12                                         "redirectUris": [ "http://localhost:4200/auth-callback" ],
     12                                        "redirectUris": [ "http://localhost:4200" ],
    1313                                        "postLogoutRedirectUris": [ "http://localhost:4200" ]
    1414                                }
Note: See TracChangeset for help on using the changeset viewer.