Skip to content

Commit

Permalink
Example app makeover
Browse files Browse the repository at this point in the history
  • Loading branch information
doregg committed Mar 10, 2024
1 parent a499dc5 commit 16fa3a0
Show file tree
Hide file tree
Showing 27 changed files with 598 additions and 81 deletions.
26 changes: 20 additions & 6 deletions projects/example/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { FronteggAuthGuard } from '@frontegg/angular';
import { PrivateRouteComponent } from './components/private-route.component';
import { NotFoundComponent } from './components/not-found.component';
import { AppHomeComponent } from './components/home.component';
import { FronteggAuthGuard } from '@frontegg/angular';
import { HomePage } from './components/home-page/home-page.component';
import { StepUpHighMaxAgePage } from './components/step-up/step-up-high-max-age-page.component';
import { EntitlementsPage } from './components/entitlements-page/entitlements-page.component';

import { StepUpSmallMaxAgePage } from './components/step-up/step-up-small-max-age-page.component';
import { StepUpNoMaxAgePage } from './components/step-up/step-up-no-max-age-page.component';
import { StepUpFull } from './components/step-up/step-up-full/step-up-full.component';
import { AuthorizedContentPage } from './components/authorized-content/authorized-content-page.component';
import { ROUTE_PATHS } from './links';

const protectSingleRoutes: Routes = [
{ path: '', component: AppHomeComponent },
{ path: 'test-private-route', component: PrivateRouteComponent, canActivate: [FronteggAuthGuard] },
{ path: 'test', pathMatch: 'full', redirectTo: 'nested-router' },
{ path: ROUTE_PATHS.HOME_PAGE, component: HomePage },
{ path: ROUTE_PATHS.ENTITLEMENTS, component: EntitlementsPage },
{ path: ROUTE_PATHS.STEP_UP_HIGH_MAX_AGE, component: StepUpHighMaxAgePage },
{ path: ROUTE_PATHS.STEP_UP_SMALL_MAX_AGE, component: StepUpSmallMaxAgePage },
{ path: ROUTE_PATHS.STEP_UP_NO_MAX_AGE, component: StepUpNoMaxAgePage },
{ path: ROUTE_PATHS.STEPPED_UP_FULL, component: StepUpFull },
{ path: ROUTE_PATHS.AUTHORIZED_CONTENT, component: AuthorizedContentPage },
{ path: ROUTE_PATHS.TEST_PRIVATE_ROUTE, component: PrivateRouteComponent, canActivate: [FronteggAuthGuard] },
{ path: ROUTE_PATHS.TEST, pathMatch: 'full', redirectTo: ROUTE_PATHS.NESTED_ROUTER },
{
path: 'nested-router',
path: ROUTE_PATHS.NESTED_ROUTER,
loadChildren: () => import('./nested-module/nested-module.module').then(m => m.NestedModule),
},
{ path: '**', component: NotFoundComponent },
Expand Down
38 changes: 35 additions & 3 deletions projects/example/src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
<div *ngIf="!isLoading">
<router-outlet></router-outlet>
<div *authorizedContent="['Admin']">Authorized content section for Admin's (example)</div>
<div *ngIf="!isLoading" class="centered-flex-container">
<h1>Frontegg App</h1>

<div class='buttons-list'>
<button *ngIf="!authenticated && fronteggAppService.fronteggApp.options.hostedLoginBox === true" data-test-id="open-hosted" (click)="loginWithRedirect()">Login</button>
<a *ngIf="!authenticated && fronteggAppService.fronteggApp.options.hostedLoginBox === false" data-test-id="open-embedded" routerLink="/account/login"><button>Login</button></a>

<button *ngIf="authenticated" data-test-id="open-admin-portal-btn" (click)="showApp()">Open Admin Portal</button>

<span *ngIf="authenticated" (click)="logout()"><button>Logout</button></span>
</div>

<div *ngIf="authenticated">
<p>Authenticated as {{user?.name}}</p>
</div>

<div>
{{ fronteggAppService.fronteggApp.options.hostedLoginBox ? 'Hosted' : 'Embedded' }}
</div>

<nav class="buttons-list">
Pages:
<a routerLink="{{ROUTE_PATHS.HOME_PAGE}}"><button>Home</button></a>
<a routerLink="/{{ROUTE_PATHS.ENTITLEMENTS}}"><button>Entitlements</button></a>
<a routerLink="/{{ROUTE_PATHS.STEP_UP_NO_MAX_AGE}}"><button>Step up - no max age</button></a>
<a routerLink="/{{ROUTE_PATHS.STEP_UP_HIGH_MAX_AGE}}"><button>Step up - max age 5000</button></a>
<a routerLink="/{{ROUTE_PATHS.STEP_UP_SMALL_MAX_AGE}}"><button>Step up - max age 35</button></a>
<a routerLink="/{{ROUTE_PATHS.STEPPED_UP_FULL}}"><button>Stepped up (full)</button></a>
<a routerLink="/{{ROUTE_PATHS.AUTHORIZED_CONTENT}}"><button>Authorized content</button></a>
<a routerLink="/{{ROUTE_PATHS.TEST_PRIVATE_ROUTE}}"><button>Private route</button></a>
<a routerLink="/{{ROUTE_PATHS.UNKNOWN_ROUTE}}"><button>Fallback route</button></a>
</nav>

<section class='router-content'>
<router-outlet></router-outlet>
</section>
</div>
22 changes: 22 additions & 0 deletions projects/example/src/app/app.component.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.router-content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-width: 600px;
min-height: 400px;
margin: 25px 0;
padding: 10px;
border: 2px dotted rgb(21, 125, 243);
}

.buttons-list {
display: flex;
justify-content: center;
align-items: center;
margin-top: 10px;
}

.buttons-list > * {
margin-left: 4px;
}
47 changes: 43 additions & 4 deletions projects/example/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,63 @@
import { Component, OnDestroy, OnInit } from '@angular/core';
import { FronteggAppService } from '@frontegg/angular';
import { Router } from '@angular/router';
import { FronteggAppService, FronteggAuthService } from '@frontegg/angular';
import { Subscription } from 'rxjs';
import { ROUTE_PATHS } from './links';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit, OnDestroy {
isLoading = true;
loadingSubscription: Subscription;
isAuthenticatedSubscription: Subscription;
userSubscription?: Subscription;
authenticated = false;
user?: any;
ROUTE_PATHS = ROUTE_PATHS;

constructor(private fronteggAppService: FronteggAppService) {
this.loadingSubscription = fronteggAppService.isLoading$.subscribe((isLoading) => this.isLoading = isLoading);
constructor(
public fronteggAppService: FronteggAppService,
private fronteggAuthService: FronteggAuthService,
private router: Router,
) {
this.loadingSubscription = fronteggAppService.isLoading$.subscribe((isLoading) => {
this.isLoading = isLoading;
});

this.isAuthenticatedSubscription = this.fronteggAppService.isAuthenticated$.subscribe((isAuthenticated: boolean) => {
this.authenticated = isAuthenticated;
});
}

showApp(): void {
this.fronteggAppService.showAdminPortal();
}

logout(): void {
if (this.fronteggAppService.fronteggApp.options.hostedLoginBox) {
this.fronteggAuthService.logout();
return;
}

this.router.navigateByUrl(this.fronteggAppService.authRoutes.logoutUrl);
}

loginWithRedirect(): void {
this.fronteggAuthService.loginWithRedirect();
}

ngOnInit(): void {
console.log('AppComponent', 'ngOnInit');
this.userSubscription = this.fronteggAuthService.user$.subscribe((user: any) => {
this.user = user;
});
}

ngOnDestroy(): void {
this.loadingSubscription.unsubscribe();
this.isAuthenticatedSubscription.unsubscribe();
this.userSubscription?.unsubscribe();
}
}
31 changes: 28 additions & 3 deletions projects/example/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,34 @@ import { CommonModule } from '@angular/common';
import { NotFoundComponent } from './components/not-found.component';
import { EmptyAppComponent } from './components/empty.component';
import { PrivateRouteComponent } from './components/private-route.component';
import { AppHomeComponent } from './components/home.component';
import { FronteggComponent, FronteggAppModule } from '@frontegg/angular';
import { HomePage } from './components/home-page/home-page.component';
import { FronteggAppModule } from '@frontegg/angular';
import { CheckoutDialogModule } from './checkout-dialog/checkout-dialog.module';
import { StepUpHighMaxAgePage } from './components/step-up/step-up-high-max-age-page.component';
import { EntitlementsPage } from './components/entitlements-page/entitlements-page.component';
import { BaseStepUp } from './components/step-up/base-step-up/base-step-up.component';
import { StepUpSmallMaxAgePage } from './components/step-up/step-up-small-max-age-page.component';
import { StepUpNoMaxAgePage } from './components/step-up/step-up-no-max-age-page.component';
import { StepUpFull } from './components/step-up/step-up-full/step-up-full.component';
import { AuthorizedContentPage } from './components/authorized-content/authorized-content-page.component';

const IS_HOSTED_MODE = false;

@NgModule({
declarations: [ AppComponent, NotFoundComponent, AppHomeComponent, EmptyAppComponent, PrivateRouteComponent ],
declarations: [
AppComponent,
NotFoundComponent,
HomePage,
BaseStepUp,
StepUpHighMaxAgePage,
StepUpSmallMaxAgePage,
StepUpNoMaxAgePage,
StepUpFull,
EntitlementsPage,
EmptyAppComponent,
AuthorizedContentPage,
PrivateRouteComponent,
],
imports: [
CommonModule,
BrowserModule,
Expand All @@ -25,9 +46,13 @@ import { CheckoutDialogModule } from './checkout-dialog/checkout-dialog.module';
baseUrl: 'https://demo.frontegg.com',
clientId: 'b6adfe4c-d695-4c04-b95f-3ec9fd0c6cca',
},
hostedLoginBox: IS_HOSTED_MODE, // don't remove it. Change it via the const value above
authOptions: {
keepSessionAlive: true,
},
entitlementsOptions: {
enabled: true
}
},
),
CheckoutDialogModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<section *ngIf="!authenticated">
Not Authenticated
</section>

<div><b>Admin</b> role section: (empty when not authorized)</div>
<section class="authorized-section-container">
<div *authorizedContent="['Admin']">
Authorized content section for <b>Admin</b> role
</div>
</section>

<div><b>Dora & Admin</b> role section: (empty when not authorized for both)</div>
<section class="authorized-section-container">
<div *authorizedContent="['Dora', 'Admin']">
Authorized content section for <b>Dora & Admin</b> role
</div>
</section>

<div><b>Dora</b> role section: (empty when not authorized)</div>
<section class="authorized-section-container">
<div *authorizedContent="['Dora']">
Authorized content section for <b>Dora</b> role
</div>
</section>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.authorized-section-container {
padding: 10px;
margin: 8px 0 24px 0;
border: 2px dotted rgb(21, 125, 243);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Component, OnDestroy, OnInit } from '@angular/core';
import { FronteggAppService } from '@frontegg/angular';
import { Subscription } from 'rxjs';

@Component({
selector: 'authorized-content-page',
templateUrl: 'authorized-content-page.component.html',
styleUrls: ['authorized-content-page.component.scss'],
})
export class AuthorizedContentPage implements OnInit, OnDestroy {
authenticated = false;
isAuthenticatedSubscription?: Subscription;

constructor(private fronteggAppService: FronteggAppService) {}

ngOnInit(): void {
this.isAuthenticatedSubscription = this.fronteggAppService.isAuthenticated$.subscribe((isAuthenticated: boolean) => {
this.authenticated = isAuthenticated;
});
}

ngOnDestroy(): void {
this.isAuthenticatedSubscription?.unsubscribe();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<section *ngIf="!authenticated">
Not Authenticated
</section>

<section *ngIf="authenticated" class="outer-container">
<div class="boxes-container">
<span *ngFor="let ent of entitlementsResults | keyvalue">
<div *ngIf="ent.value.isEntitled" class="truthy">
{{ ent.value.name }}
</div>

<div *ngIf="!ent.value.isEntitled" class="falsy">
{{ ent.value.name }}
<br/><br/>
<i>{{ent.value.justification?.toString()}}</i>
</div>
</span>
</div>

<div class="centered-flex-container" [ngStyle]="{ 'flex-direction': 'row' }">
<button (click)="onLoadEntitlementsClicked()">Load entitlements</button>
<button (click)="onLoadEntitlementsWithCallbackClicked()">Load entitlements with callback</button>
</div>
</section>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.truthy {
background-color: lightgreen;
}

.falsy {
background-color: pink;
}

.truthy, .falsy {
display: inline-block;
height: 100px;
width: 420px;
word-wrap: break-word;
margin: 10px;
padding: 5px;
border: 1px dotted black;

animation: fadeInAnimation ease 3s;
animation-iteration-count: 1;
animation-fill-mode: forwards;

@keyframes fadeInAnimation {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
}

.outer-container {
display: flex;
align-items: center;
flex-direction: column;
}

.boxes-container {
display: flex;
flex-wrap: wrap;
max-width: 900px;
justify-content: center;
align-items: center;
text-align: center;
}

.centered-flex-container button {
margin: 10px 10px 0 0;
}
Loading

0 comments on commit 16fa3a0

Please sign in to comment.