From 07ae264402195a918cd3d615bd66452278032bb7 Mon Sep 17 00:00:00 2001 From: ThomasFransolet Date: Sat, 13 Jul 2019 01:14:05 +0200 Subject: [PATCH] CP # Login (redirect if not), 404 page (skeleton), Creation app routing structure (WIP) --- src/app/app-routing.module.ts | 25 ++++++++++++++--- src/app/app.component.html | 2 +- src/app/app.component.ts | 9 ++++++- src/app/app.module.ts | 18 ++++++++++--- .../home/home.component.css} | 0 .../home/home.component.html} | 0 .../home/home.component.spec.ts} | 12 ++++----- src/app/control-panel/home/home.component.ts | 19 +++++++++++++ .../not-found/not-found.component.css | 0 .../not-found/not-found.component.html | 3 +++ .../not-found/not-found.component.spec.ts | 25 +++++++++++++++++ .../not-found/not-found.component.ts | 15 +++++++++++ .../profile/config/config.component.css | 0 .../profile/config/config.component.html | 3 +++ .../profile/config/config.component.spec.ts | 25 +++++++++++++++++ .../profile/config/config.component.ts | 15 +++++++++++ .../edit-profile/edit-profile.component.css | 0 .../edit-profile/edit-profile.component.html | 3 +++ .../edit-profile.component.spec.ts | 25 +++++++++++++++++ .../edit-profile/edit-profile.component.ts | 15 +++++++++++ .../profile/profile/profile.component.css | 0 .../profile/profile/profile.component.html | 3 +++ .../profile/profile/profile.component.spec.ts | 25 +++++++++++++++++ .../profile/profile/profile.component.ts | 15 +++++++++++ src/app/home-page/home-page.component.ts | 19 ------------- src/app/utils/auth.guard.spec.ts | 15 +++++++++++ src/app/utils/auth.guard.ts | 24 +++++++++++++++++ src/app/utils/auth.service.ts | 27 +++++++++++++++++++ 28 files changed, 308 insertions(+), 34 deletions(-) rename src/app/{home-page/home-page.component.css => control-panel/home/home.component.css} (100%) rename src/app/{home-page/home-page.component.html => control-panel/home/home.component.html} (100%) rename src/app/{home-page/home-page.component.spec.ts => control-panel/home/home.component.spec.ts} (55%) create mode 100644 src/app/control-panel/home/home.component.ts create mode 100644 src/app/control-panel/not-found/not-found.component.css create mode 100644 src/app/control-panel/not-found/not-found.component.html create mode 100644 src/app/control-panel/not-found/not-found.component.spec.ts create mode 100644 src/app/control-panel/not-found/not-found.component.ts create mode 100644 src/app/control-panel/profile/config/config.component.css create mode 100644 src/app/control-panel/profile/config/config.component.html create mode 100644 src/app/control-panel/profile/config/config.component.spec.ts create mode 100644 src/app/control-panel/profile/config/config.component.ts create mode 100644 src/app/control-panel/profile/edit-profile/edit-profile.component.css create mode 100644 src/app/control-panel/profile/edit-profile/edit-profile.component.html create mode 100644 src/app/control-panel/profile/edit-profile/edit-profile.component.spec.ts create mode 100644 src/app/control-panel/profile/edit-profile/edit-profile.component.ts create mode 100644 src/app/control-panel/profile/profile/profile.component.css create mode 100644 src/app/control-panel/profile/profile/profile.component.html create mode 100644 src/app/control-panel/profile/profile/profile.component.spec.ts create mode 100644 src/app/control-panel/profile/profile/profile.component.ts delete mode 100644 src/app/home-page/home-page.component.ts create mode 100644 src/app/utils/auth.guard.spec.ts create mode 100644 src/app/utils/auth.guard.ts create mode 100644 src/app/utils/auth.service.ts diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index aca6dd3..707e46a 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -1,11 +1,30 @@ import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; -import { HomePageComponent } from './home-page/home-page.component'; +import { HomeComponent } from './control-panel/home/home.component'; +import { ConfigComponent } from './control-panel/profile/config/config.component'; +import { AuthGuard } from './utils/auth.guard'; +import { NotFoundComponent } from './control-panel/not-found/not-found.component'; +import { ProfileComponent } from './control-panel/profile/profile/profile.component'; +import { EditProfileComponent } from './control-panel/profile/edit-profile/edit-profile.component'; const routes: Routes = [ - { path : 'home', component: HomePageComponent }, - { path : '', redirectTo: 'home', pathMatch: 'full'} + { path : '', redirectTo: 'home', pathMatch: 'full'}, + { path : 'home', component: HomeComponent }, + { + path: 'profile', + canActivate: [AuthGuard], + children: [ + { path: '', component: ProfileComponent}, + { path: ':profileId', children: [ + { path: '', component: ProfileComponent}, + { path: 'edit', component: EditProfileComponent}, + { path: 'config', component: ConfigComponent } + ]} + ] + }, + { path: '**', component: NotFoundComponent} + ]; @NgModule({ diff --git a/src/app/app.component.html b/src/app/app.component.html index 9ae94dd..39d6c8e 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -9,7 +9,7 @@ - + diff --git a/src/app/app.component.ts b/src/app/app.component.ts index cb85dc3..6ee99ce 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -2,6 +2,8 @@ import { Component, OnInit } from '@angular/core'; import { MenuItem } from 'primeng/api'; import { AppService } from './app.service'; import { TranslateService } from '@ngx-translate/core'; +import { AuthService } from './utils/auth.service'; +import { Router } from '@angular/router'; @Component({ @@ -27,7 +29,9 @@ export class AppComponent implements OnInit{ constructor( private _appService: AppService, - private _translateService: TranslateService + private _translateService: TranslateService, + private _authService: AuthService, + private _router: Router ){} ngOnInit(){ @@ -49,6 +53,9 @@ export class AppComponent implements OnInit{ this.DisplayLoginModal = false; this.Username = null; this.Password = null; + this._authService.IsLoggedIn = true; + + this._router.navigate(['/profile/01']); }, () => { console.log('ERROR'); }); diff --git a/src/app/app.module.ts b/src/app/app.module.ts index a807a82..5314c62 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -4,7 +4,7 @@ import { NgModule, Provider, APP_INITIALIZER } from '@angular/core'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; -import { HomePageComponent } from './home-page/home-page.component'; +import { HomeComponent } from './control-panel/home/home.component'; import { HttpClientModule, HttpClient } from '@angular/common/http'; import { AlertModule } from 'ngx-bootstrap/alert'; @@ -24,6 +24,11 @@ import { CalendarModule } from 'primeng/calendar'; import {TranslateModule, TranslateLoader} from '@ngx-translate/core'; import {TranslateHttpLoader} from '@ngx-translate/http-loader'; +import { AuthService } from './utils/auth.service'; +import { ConfigComponent } from './control-panel/profile/config/config.component'; +import { NotFoundComponent } from './control-panel/not-found/not-found.component'; +import { ProfileComponent } from './control-panel/profile/profile/profile.component'; +import { EditProfileComponent } from './control-panel/profile/edit-profile/edit-profile.component'; @@ -50,8 +55,12 @@ export function HttpLoaderFactory(http: HttpClient) { @NgModule({ declarations: [ AppComponent, - HomePageComponent, - BorderCardDirective + HomeComponent, + BorderCardDirective, + ConfigComponent, + NotFoundComponent, + ProfileComponent, + EditProfileComponent ], imports: [ BrowserModule, @@ -79,7 +88,8 @@ export function HttpLoaderFactory(http: HttpClient) { ], providers: [ INIT_API_CONFIGURATION, - AppService + AppService, + AuthService ], bootstrap: [AppComponent] }) diff --git a/src/app/home-page/home-page.component.css b/src/app/control-panel/home/home.component.css similarity index 100% rename from src/app/home-page/home-page.component.css rename to src/app/control-panel/home/home.component.css diff --git a/src/app/home-page/home-page.component.html b/src/app/control-panel/home/home.component.html similarity index 100% rename from src/app/home-page/home-page.component.html rename to src/app/control-panel/home/home.component.html diff --git a/src/app/home-page/home-page.component.spec.ts b/src/app/control-panel/home/home.component.spec.ts similarity index 55% rename from src/app/home-page/home-page.component.spec.ts rename to src/app/control-panel/home/home.component.spec.ts index eaa5d91..03efd74 100644 --- a/src/app/home-page/home-page.component.spec.ts +++ b/src/app/control-panel/home/home.component.spec.ts @@ -1,21 +1,21 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; -import { HomePageComponent } from './home-page.component'; +import { HomeComponent } from './home.component'; -describe('HomePageComponent', () => { - let component: HomePageComponent; - let fixture: ComponentFixture; +describe('HomeComponent', () => { + let component: HomeComponent; + let fixture: ComponentFixture; beforeEach(async(() => { TestBed.configureTestingModule({ - declarations: [ HomePageComponent ] + declarations: [ HomeComponent ] }) .compileComponents(); })); beforeEach(() => { - fixture = TestBed.createComponent(HomePageComponent); + fixture = TestBed.createComponent(HomeComponent); component = fixture.componentInstance; fixture.detectChanges(); }); diff --git a/src/app/control-panel/home/home.component.ts b/src/app/control-panel/home/home.component.ts new file mode 100644 index 0000000..76b5d6a --- /dev/null +++ b/src/app/control-panel/home/home.component.ts @@ -0,0 +1,19 @@ +import { Component, OnInit } from '@angular/core'; +import { Router } from '@angular/router' + + + +@Component({ + selector: 'app-home', + templateUrl: './home.component.html', + styleUrls: ['./home.component.css'] +}) +export class HomeComponent implements OnInit { + + constructor() { } + + ngOnInit() { + + } + +} diff --git a/src/app/control-panel/not-found/not-found.component.css b/src/app/control-panel/not-found/not-found.component.css new file mode 100644 index 0000000..e69de29 diff --git a/src/app/control-panel/not-found/not-found.component.html b/src/app/control-panel/not-found/not-found.component.html new file mode 100644 index 0000000..ff60493 --- /dev/null +++ b/src/app/control-panel/not-found/not-found.component.html @@ -0,0 +1,3 @@ +

+ not-found works! +

diff --git a/src/app/control-panel/not-found/not-found.component.spec.ts b/src/app/control-panel/not-found/not-found.component.spec.ts new file mode 100644 index 0000000..35189ed --- /dev/null +++ b/src/app/control-panel/not-found/not-found.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { NotFoundComponent } from './not-found.component'; + +describe('NotFoundComponent', () => { + let component: NotFoundComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ NotFoundComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(NotFoundComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/control-panel/not-found/not-found.component.ts b/src/app/control-panel/not-found/not-found.component.ts new file mode 100644 index 0000000..029bd54 --- /dev/null +++ b/src/app/control-panel/not-found/not-found.component.ts @@ -0,0 +1,15 @@ +import { Component, OnInit } from '@angular/core'; + +@Component({ + selector: 'app-not-found', + templateUrl: './not-found.component.html', + styleUrls: ['./not-found.component.css'] +}) +export class NotFoundComponent implements OnInit { + + constructor() { } + + ngOnInit() { + } + +} diff --git a/src/app/control-panel/profile/config/config.component.css b/src/app/control-panel/profile/config/config.component.css new file mode 100644 index 0000000..e69de29 diff --git a/src/app/control-panel/profile/config/config.component.html b/src/app/control-panel/profile/config/config.component.html new file mode 100644 index 0000000..9f99914 --- /dev/null +++ b/src/app/control-panel/profile/config/config.component.html @@ -0,0 +1,3 @@ +

+ config works! +

diff --git a/src/app/control-panel/profile/config/config.component.spec.ts b/src/app/control-panel/profile/config/config.component.spec.ts new file mode 100644 index 0000000..ec5d3be --- /dev/null +++ b/src/app/control-panel/profile/config/config.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ConfigComponent } from './config.component'; + +describe('ConfigComponent', () => { + let component: ConfigComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ ConfigComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(ConfigComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/control-panel/profile/config/config.component.ts b/src/app/control-panel/profile/config/config.component.ts new file mode 100644 index 0000000..5813d4e --- /dev/null +++ b/src/app/control-panel/profile/config/config.component.ts @@ -0,0 +1,15 @@ +import { Component, OnInit } from '@angular/core'; + +@Component({ + selector: 'app-config', + templateUrl: './config.component.html', + styleUrls: ['./config.component.css'] +}) +export class ConfigComponent implements OnInit { + + constructor() { } + + ngOnInit() { + } + +} diff --git a/src/app/control-panel/profile/edit-profile/edit-profile.component.css b/src/app/control-panel/profile/edit-profile/edit-profile.component.css new file mode 100644 index 0000000..e69de29 diff --git a/src/app/control-panel/profile/edit-profile/edit-profile.component.html b/src/app/control-panel/profile/edit-profile/edit-profile.component.html new file mode 100644 index 0000000..e4ae426 --- /dev/null +++ b/src/app/control-panel/profile/edit-profile/edit-profile.component.html @@ -0,0 +1,3 @@ +

+ edit-profile works! +

diff --git a/src/app/control-panel/profile/edit-profile/edit-profile.component.spec.ts b/src/app/control-panel/profile/edit-profile/edit-profile.component.spec.ts new file mode 100644 index 0000000..5b24456 --- /dev/null +++ b/src/app/control-panel/profile/edit-profile/edit-profile.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { EditProfileComponent } from './edit-profile.component'; + +describe('EditProfileComponent', () => { + let component: EditProfileComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ EditProfileComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(EditProfileComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/control-panel/profile/edit-profile/edit-profile.component.ts b/src/app/control-panel/profile/edit-profile/edit-profile.component.ts new file mode 100644 index 0000000..11ee186 --- /dev/null +++ b/src/app/control-panel/profile/edit-profile/edit-profile.component.ts @@ -0,0 +1,15 @@ +import { Component, OnInit } from '@angular/core'; + +@Component({ + selector: 'app-edit-profile', + templateUrl: './edit-profile.component.html', + styleUrls: ['./edit-profile.component.css'] +}) +export class EditProfileComponent implements OnInit { + + constructor() { } + + ngOnInit() { + } + +} diff --git a/src/app/control-panel/profile/profile/profile.component.css b/src/app/control-panel/profile/profile/profile.component.css new file mode 100644 index 0000000..e69de29 diff --git a/src/app/control-panel/profile/profile/profile.component.html b/src/app/control-panel/profile/profile/profile.component.html new file mode 100644 index 0000000..cd8b801 --- /dev/null +++ b/src/app/control-panel/profile/profile/profile.component.html @@ -0,0 +1,3 @@ +

+ profile works! +

diff --git a/src/app/control-panel/profile/profile/profile.component.spec.ts b/src/app/control-panel/profile/profile/profile.component.spec.ts new file mode 100644 index 0000000..692b234 --- /dev/null +++ b/src/app/control-panel/profile/profile/profile.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ProfileComponent } from './profile.component'; + +describe('ProfileComponent', () => { + let component: ProfileComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ ProfileComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(ProfileComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/control-panel/profile/profile/profile.component.ts b/src/app/control-panel/profile/profile/profile.component.ts new file mode 100644 index 0000000..a747936 --- /dev/null +++ b/src/app/control-panel/profile/profile/profile.component.ts @@ -0,0 +1,15 @@ +import { Component, OnInit } from '@angular/core'; + +@Component({ + selector: 'app-profile', + templateUrl: './profile.component.html', + styleUrls: ['./profile.component.css'] +}) +export class ProfileComponent implements OnInit { + + constructor() { } + + ngOnInit() { + } + +} diff --git a/src/app/home-page/home-page.component.ts b/src/app/home-page/home-page.component.ts deleted file mode 100644 index b008480..0000000 --- a/src/app/home-page/home-page.component.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { Component, OnInit } from '@angular/core'; -import { Router } from '@angular/router' - - - -@Component({ - selector: 'app-home-page', - templateUrl: './home-page.component.html', - styleUrls: ['./home-page.component.css'] -}) -export class HomePageComponent implements OnInit { - - constructor() { } - - ngOnInit() { - - } - -} diff --git a/src/app/utils/auth.guard.spec.ts b/src/app/utils/auth.guard.spec.ts new file mode 100644 index 0000000..7ed05ee --- /dev/null +++ b/src/app/utils/auth.guard.spec.ts @@ -0,0 +1,15 @@ +import { TestBed, async, inject } from '@angular/core/testing'; + +import { AuthGuard } from './auth.guard'; + +describe('AuthGuard', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [AuthGuard] + }); + }); + + it('should ...', inject([AuthGuard], (guard: AuthGuard) => { + expect(guard).toBeTruthy(); + })); +}); diff --git a/src/app/utils/auth.guard.ts b/src/app/utils/auth.guard.ts new file mode 100644 index 0000000..403d4d4 --- /dev/null +++ b/src/app/utils/auth.guard.ts @@ -0,0 +1,24 @@ +import { Injectable } from '@angular/core'; +import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, UrlTree } from '@angular/router'; +import { Observable } from 'rxjs'; + +import { AuthService } from './auth.service'; + +@Injectable({ + providedIn: 'root' +}) +export class AuthGuard implements CanActivate { + + constructor(private _authService: AuthService, private router: Router){} + + canActivate( + next: ActivatedRouteSnapshot, + state: RouterStateSnapshot): Observable | Promise | boolean | UrlTree { + if (this._authService.IsLoggedIn){ + return true; + } + else { + return this.router.parseUrl('/home'); + } + } +} diff --git a/src/app/utils/auth.service.ts b/src/app/utils/auth.service.ts new file mode 100644 index 0000000..d4cf31b --- /dev/null +++ b/src/app/utils/auth.service.ts @@ -0,0 +1,27 @@ +import { Injectable } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; +import { Observable } from 'rxjs'; +import { TokenService } from '../api/services/token.service'; + +@Injectable({ + providedIn: 'root' +}) + +export class AuthService { + + public IsLoggedIn = false; + + constructor( + protected http: HttpClient, + private _tokenService: TokenService + ) { } + + public GetToken(username: string, password: string): Observable { + return this._tokenService.Create({username: username, password: password}); + } + + public GetAuthStatus() { + return this.IsLoggedIn; + } + +}