Skip to content

Commit 12f56ae

Browse files
authored
Merge pull request #114 from marc101101/bug/reload-routing
Bug/reload routing
2 parents 4010b11 + 640c086 commit 12f56ae

12 files changed

Lines changed: 49 additions & 33 deletions

client/app/app-routing.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { HomeComponent } from './components/home/home.component';
99
import { CategoriesComponent } from './components/home/categories/categories.component';
1010
import { ProfilComponent } from './components/home/profil/profil.component';
1111
import { SelectivePreloadingStrategy } from './selective-preloading-strategy';
12+
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
1213

1314
const routes: Routes = [
1415
{
@@ -27,6 +28,7 @@ import { SelectivePreloadingStrategy } from './selective-preloading-strategy';
2728
{
2829
path: 'home',
2930
loadChildren: './components/home/home.module#HomeModule',
31+
canActivate: [AuthGuard],
3032
data: {prelaod: true}
3133
},
3234
{
@@ -39,6 +41,6 @@ import { SelectivePreloadingStrategy } from './selective-preloading-strategy';
3941
@NgModule({
4042
imports: [RouterModule.forRoot(routes, { preloadingStrategy: SelectivePreloadingStrategy })],
4143
exports: [RouterModule],
42-
providers: [SelectivePreloadingStrategy]
44+
providers: [SelectivePreloadingStrategy, { provide: LocationStrategy, useClass: HashLocationStrategy}]
4345
})
4446
export class AppRoutingModule { }

client/app/components/home/home-routing.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const routes: Routes = [
99
{
1010
path: '',
1111
component: HomeComponent,
12-
//canActivate: [AuthGuard],
12+
canActivate: [AuthGuard],
1313
children: [
1414
{
1515
path: 'kategorien',
@@ -42,5 +42,6 @@ const routes: Routes = [
4242
@NgModule({
4343
imports: [RouterModule.forChild(routes)],
4444
exports: [RouterModule]
45+
4546
})
4647
export class HomeRoutingModule { }

client/app/components/home/profil/profil.component.html

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</div>
1010
</div>
1111
<div class="column is-10" style="z-index: 1;">
12-
<div style="font-size: 1.5rem; color:white; margin-top: 0.2rem;">
12+
<div *ngIf="dataIsAvailable" style="font-size: 1.5rem; color:white; margin-top: 0.2rem;">
1313
{{user.TEIL_VORNAME}} {{user.TEIL_NACHNAME}}
1414
</div>
1515
</div>
@@ -24,17 +24,14 @@
2424
#mail="ngModel">
2525
<br>
2626
<br>
27-
<input class="input is-medium" type="text" placeholder="Telefon / Handy" id="mobile" [(ngModel)]="user.TEIL_HANDY" name="mobile" #mail="ngModel">
27+
<input *ngIf="dataIsAvailable" class="input is-medium" type="text" placeholder="Adresse" id="address" [(ngModel)]="user.TEIL_ORT" name="address"
28+
#address="ngModel">
2829
<br>
2930
<br>
30-
<input class="input is-medium" type="text" placeholder="Adresse" id="address" [(ngModel)]="user.TEIL_ORT" name="address"
31-
#mail="ngModel">
32-
<br>
33-
<br>
34-
<input class="input is-medium" type="text" placeholder="BLZ" id="blz" [(ngModel)]="user.TEIL_BLZ" name="blz" #mail="ngModel" maxlength="8">
31+
<input *ngIf="dataIsAvailable" class="input is-medium" type="text" placeholder="BLZ" id="blz" [(ngModel)]="user.TEIL_BLZ" name="blz" #blz="ngModel" maxlength="8">
3532
<br>
3633
<br>
37-
<input class="input is-medium" type="text" placeholder="IBAN" id="iban" [(ngModel)]="user.TEIL_IBAN" name="iban" #mail="ngModel" maxlength="22">
34+
<input *ngIf="dataIsAvailable" class="input is-medium" type="text" placeholder="IBAN" id="iban" [(ngModel)]="user.TEIL_IBAN" name="iban" #iban="ngModel" maxlength="22">
3835
<br>
3936
<br>
4037
<div class="columns is-mobile">

client/app/components/home/profil/profil.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export class ProfilComponent implements OnInit {
2222

2323
ngOnInit() {
2424
this.userService.getUserMe().subscribe(response => {
25-
this.dataIsAvailable = true;
2625
this.user = response;
26+
this.dataIsAvailable = true;
2727
});
2828
}
2929

client/app/components/login/login.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class LoginComponent implements OnInit {
3232
if (data.token && !this.jwtHelper.isTokenExpired(data.token)) {
3333
// store jwt token in local storage to keep user logged in between page refreshes
3434
localStorage.setItem('token', data.token);
35-
this.router.navigate(['/home/profil']).catch(error => {
35+
this.router.navigate(['/home/kategorien']).catch(error => {
3636
console.log(error);
3737
});
3838
}

client/app/guards/auth.guard.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,17 @@ import { Injectable } from '@angular/core';
22
import { ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
33
import { AuthService } from '../services/auth.service';
44
import { JwtHelperService } from '@auth0/angular-jwt';
5+
import { log } from 'util';
6+
import { Observable } from 'rxjs';
57

68
@Injectable()
79
export class AuthGuard {
810

9-
private jwtHelper: JwtHelperService = new JwtHelperService();
10-
1111
constructor( private authService : AuthService, private router : Router ) {
1212
}
1313

14-
canActivate( route : ActivatedRouteSnapshot, state : RouterStateSnapshot ) {
15-
16-
if(this.authService.isLoggedIn() && !this.jwtHelper.isTokenExpired(localStorage.getItem('token'))){
17-
return true;
18-
}
19-
else{
20-
this.router.navigate(['/login']).catch(error => {
21-
console.log(error);
22-
});
23-
return false;
24-
}
14+
canActivate( route : ActivatedRouteSnapshot, state : RouterStateSnapshot ): Observable<boolean> {
15+
return this.authService.isLoggedIn();
2516
}
17+
2618
}

client/app/services/auth.service.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { environment } from '../../environments/environement';
55
import { UserData } from '../models/UserData';
66
import { AlertService } from './alert.service';
77
import { map, catchError } from 'rxjs/operators';
8+
import { log } from 'util';
9+
import { Router } from '@angular/router';
10+
import { JwtHelperService } from '@auth0/angular-jwt';
811

912
@Injectable({
1013
providedIn: 'root'
@@ -14,11 +17,30 @@ export class AuthService {
1417

1518
private authStatus: boolean = false;
1619
private url: string = environment.apiUrl;
20+
private jwtHelper: JwtHelperService = new JwtHelperService();
1721

18-
constructor(public http: HttpClient, public alertService: AlertService) { }
22+
constructor(public http: HttpClient, public alertService: AlertService, public router: Router) { }
1923

20-
isLoggedIn(): boolean{
21-
return this.authStatus;
24+
isLoggedIn(): Observable<boolean>{
25+
if(this.jwtHelper.isTokenExpired(localStorage.getItem('token'))){
26+
this.navToLogin();
27+
}
28+
else{
29+
this.authStatus = true;
30+
}
31+
32+
if(!this.authStatus){
33+
this.navToLogin();
34+
}
35+
36+
return Observable.of(this.authStatus);
37+
}
38+
39+
navToLogin():void{
40+
this.authStatus = false;
41+
this.router.navigate(['/login']).catch(error => {
42+
console.log(error);
43+
});
2244
}
2345

2446
login(user:UserData): Observable<any>{
@@ -35,7 +57,9 @@ export class AuthService {
3557
}
3658

3759
logout(): void{
38-
60+
localStorage.removeItem('token');
61+
this.authStatus = false;
62+
console.log("2", this.authStatus);
3963
}
4064

4165
register(): void{

dist/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515

1616
<body class="main" style="height: 100% ; overflow: hidden; position: relative;">
1717
<app-root>Loading...</app-root>
18-
<script type="text/javascript" src="runtime.b58f17c4d08aa46ec888.js"></script><script type="text/javascript" src="polyfills.3e60cfe9b1313ccedf9e.js"></script><script type="text/javascript" src="main.cecf8081710a9faaa299.js"></script></body>
18+
<script type="text/javascript" src="runtime.c959c8794a7c02220116.js"></script><script type="text/javascript" src="polyfills.3e60cfe9b1313ccedf9e.js"></script><script type="text/javascript" src="main.6c040a6ff9c9e2fb487e.js"></script></body>
1919
</html>
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)