Skip to content

Commit 3436918

Browse files
committed
Loads protected data after login
1 parent 85d2df0 commit 3436918

13 files changed

Lines changed: 143 additions & 5 deletions

src/app/app-routing.module.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { NgModule } from '@angular/core';
22
import { RouterModule, Routes } from '@angular/router';
3+
import { AuthenticatedGuard } from './authentication/guard/authenticated.guard';
34

45
const loginModule = () => import('@app/login/login.module').then(m => m.LoginModule);
6+
const dataModule = () => import('@app/data/data.module').then(m => m.DataModule);
57

68
const routes: Routes = [
7-
{ path: '', redirectTo: '/login', pathMatch: 'full' },
8-
{ path: 'login', loadChildren: loginModule }
9+
{ path: '', redirectTo: '/data', pathMatch: 'full' },
10+
{ path: 'login', loadChildren: loginModule },
11+
{ path: 'data', loadChildren: dataModule, canActivate: [AuthenticatedGuard] }
912
];
1013

1114
@NgModule({

src/app/app.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ import { MenuLink } from './navigation/model/menu-link';
99
export class AppComponent {
1010
title = 'angular-http-basic-auth-example';
1111

12-
links: MenuLink[] = [{ name: 'login', path: '/login' }];
12+
links: MenuLink[] = [{ name: 'login', path: '/login' },{ name: 'data', path: '/data' }];
1313

1414
}

src/app/authentication/service/authentication.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { HttpClient } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
3+
import { Response } from '@app/api/model/response';
34
import { environment } from '@environments/environment';
45
import { Observable } from 'rxjs';
56
import { map } from 'rxjs/operators';
67
import { User } from '../model/user';
7-
import { Response } from '@app/api/model/response';
88

99
@Injectable({
1010
providedIn: 'root',
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<table class="table">
2+
<thead>
3+
<tr>
4+
<th scope="col">Id</th>
5+
<th scope="col">name</th>
6+
</tr>
7+
</thead>
8+
<tbody>
9+
<tr *ngFor="let row of data;">
10+
<td>{{row.id}}</td>
11+
<td>{{row.name}}</td>
12+
</tr>
13+
</tbody>
14+
</table>

src/app/data/data-list/data-list.component.sass

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { DataListComponent } from './data-list.component';
4+
5+
describe('DataListComponent', () => {
6+
let component: DataListComponent;
7+
let fixture: ComponentFixture<DataListComponent>;
8+
9+
beforeEach(async () => {
10+
await TestBed.configureTestingModule({
11+
declarations: [ DataListComponent ]
12+
})
13+
.compileComponents();
14+
});
15+
16+
beforeEach(() => {
17+
fixture = TestBed.createComponent(DataListComponent);
18+
component = fixture.componentInstance;
19+
fixture.detectChanges();
20+
});
21+
22+
it('should create', () => {
23+
expect(component).toBeTruthy();
24+
});
25+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import { Data } from '../model/data';
3+
import { DataService } from '../service/data.service';
4+
5+
@Component({
6+
selector: 'data-list',
7+
templateUrl: './data-list.component.html',
8+
styleUrls: ['./data-list.component.sass']
9+
})
10+
export class DataListComponent {
11+
12+
data: Data[] = [];
13+
14+
constructor(private dataService: DataService) {
15+
dataService.getData().subscribe(data => this.data = data);
16+
}
17+
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { NgModule } from '@angular/core';
2+
import { RouterModule, Routes } from '@angular/router';
3+
import { DataListComponent } from './data-list/data-list.component';
4+
5+
6+
const routes: Routes = [
7+
{
8+
path: '', component: DataListComponent
9+
}
10+
];
11+
12+
@NgModule({
13+
imports: [RouterModule.forChild(routes)],
14+
exports: [RouterModule]
15+
})
16+
export class DataRoutingModule { }

src/app/data/data.module.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { NgModule } from '@angular/core';
2+
import { CommonModule } from '@angular/common';
3+
import { DataListComponent } from './data-list/data-list.component';
4+
import { DataRoutingModule } from './data-routing.module';
5+
import { DataService } from './service/data.service';
6+
7+
8+
9+
@NgModule({
10+
declarations: [
11+
DataListComponent
12+
],
13+
imports: [
14+
DataRoutingModule,
15+
CommonModule
16+
],
17+
providers: [
18+
DataService
19+
]
20+
})
21+
export class DataModule { }

src/app/data/model/data.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface Data {
2+
id: number;
3+
name: string;
4+
}

0 commit comments

Comments
 (0)