Adding a protected account page and an unauthorized page

This commit is contained in:
Zoe Roux 2020-03-11 00:52:36 +01:00
parent b1aae349fd
commit 8507a2a3cd
11 changed files with 109 additions and 3 deletions

View File

@ -0,0 +1 @@
<p>account works!</p>

View File

View File

@ -0,0 +1,15 @@
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-account',
templateUrl: './account.component.html',
styleUrls: ['./account.component.scss']
})
export class AccountComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}

View File

@ -13,6 +13,9 @@ import { ShowResolverService } from './services/show-resolver.service';
import { StreamResolverService } from "./services/stream-resolver.service"; import { StreamResolverService } from "./services/stream-resolver.service";
import { ShowDetailsComponent } from './show-details/show-details.component'; import { ShowDetailsComponent } from './show-details/show-details.component';
import {LoginComponent} from "./login/login.component"; import {LoginComponent} from "./login/login.component";
import {AccountComponent} from "./account/account.component";
import {AuthenticatedGuard} from "./guards/authenticated-guard.service";
import {UnauthorizedComponent} from "./unauthorized/unauthorized.component";
const routes: Routes = [ const routes: Routes = [
{ path: "browse", component: BrowseComponent, pathMatch: "full", resolve: { shows: LibraryResolverService } }, { path: "browse", component: BrowseComponent, pathMatch: "full", resolve: { shows: LibraryResolverService } },
@ -23,6 +26,8 @@ const routes: Routes = [
{ path: "watch/:item", component: PlayerComponent, resolve: { item: StreamResolverService } }, { path: "watch/:item", component: PlayerComponent, resolve: { item: StreamResolverService } },
{ path: "search/:query", component: SearchComponent, resolve: { items: SearchResolverService } }, { path: "search/:query", component: SearchComponent, resolve: { items: SearchResolverService } },
{ path: "login", component: LoginComponent }, { path: "login", component: LoginComponent },
{ path: "account", component: AccountComponent, canActivate: [AuthenticatedGuard], canLoad: [AuthenticatedGuard] },
{ path: "unauthorized", component: UnauthorizedComponent },
{ path: "**", component: NotFoundComponent } { path: "**", component: NotFoundComponent }
]; ];

View File

@ -31,7 +31,7 @@
</button> </button>
</li> </li>
<mat-menu #accountMenu="matMenu"> <mat-menu #accountMenu="matMenu">
<button mat-menu-item>Settings</button> <a class="link" mat-menu-item href="/account" routerLink="/account">Settings</a>
<button mat-menu-item (click)="this.authManager.logout()">Logout</button> <button mat-menu-item (click)="this.authManager.logout()">Logout</button>
</mat-menu> </mat-menu>
</ng-template> </ng-template>

View File

@ -18,6 +18,17 @@
} }
} }
.link
{
outline: none;
color: inherit;
&:hover
{
text-decoration: none !important;
}
}
.nav-link .nav-link
{ {
padding: 12px; padding: 12px;

View File

@ -37,6 +37,9 @@ import {
OidcSecurityService, OidcSecurityService,
OpenIdConfiguration OpenIdConfiguration
} from "angular-auth-oidc-client"; } from "angular-auth-oidc-client";
import { AccountComponent } from './account/account.component';
import {AuthenticatedGuard} from "./guards/authenticated-guard.service";
import { UnauthorizedComponent } from './unauthorized/unauthorized.component';
export function loadConfig(oidcConfigService: OidcConfigService) export function loadConfig(oidcConfigService: OidcConfigService)
{ {
@ -56,7 +59,9 @@ export function loadConfig(oidcConfigService: OidcConfigService)
PeopleListComponent, PeopleListComponent,
ShowsListComponent, ShowsListComponent,
LoginComponent, LoginComponent,
PasswordValidator PasswordValidator,
AccountComponent,
UnauthorizedComponent
], ],
imports: [ imports: [
BrowserModule, BrowserModule,
@ -88,7 +93,9 @@ export function loadConfig(oidcConfigService: OidcConfigService)
useFactory: loadConfig, useFactory: loadConfig,
deps: [OidcConfigService], deps: [OidcConfigService],
multi: true multi: true
},], },
AuthenticatedGuard
],
bootstrap: [AppComponent] bootstrap: [AppComponent]
}) })
export class AppModule export class AppModule

View File

@ -0,0 +1,39 @@
import { Injectable } from '@angular/core';
import {
CanActivate,
CanLoad,
Route,
UrlSegment,
ActivatedRouteSnapshot,
RouterStateSnapshot,
UrlTree,
Router
} from '@angular/router';
import { Observable } from 'rxjs';
import {AuthService} from "../services/auth.service";
@Injectable({
providedIn: 'root'
})
export class AuthenticatedGuard implements CanActivate, CanLoad
{
constructor(private router: Router, private authManager: AuthService) {}
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree
{
return this.checkPermissions();
}
canLoad(route: Route, segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean
{
return this.checkPermissions();
}
checkPermissions() : boolean
{
if (this.authManager.isAuthenticated)
return true;
this.router.navigate(["/unauthorized"]);
return false;
}
}

View File

@ -0,0 +1,11 @@
<br/>
<br/>
<br/>
<br/>
<br/>
<div class="text-center">
<h1>Unauthorized</h1>
<p>You don't have enough permissions to view this page.
<span *ngIf="!this.isLoggedIn()"><br/>Sign in and try again.</span>
</p>
</div>

View File

@ -0,0 +1,17 @@
import { Component, OnInit } from '@angular/core';
import {AuthService} from "../services/auth.service";
@Component({
selector: 'app-unauthorized',
templateUrl: './unauthorized.component.html',
styleUrls: ['./unauthorized.component.scss']
})
export class UnauthorizedComponent
{
constructor(private authManager: AuthService) { }
isLoggedIn() : boolean
{
return this.authManager.isAuthenticated;
}
}