Skip to content

Commit 73684ea

Browse files
committed
Angular Security course
1 parent 7711c5c commit 73684ea

File tree

6 files changed

+26
-25
lines changed

6 files changed

+26
-25
lines changed

server/read-all-lessons.route.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11

22

33
import {db} from "./database";
4+
import {sessionStore} from "./session-storage";
45

56

67

78
export function readAllLessons(req, res) {
89

10+
const sessionId = req.cookies['SESSIONID'];
11+
12+
const isSessionValid = sessionStore.isSessionValid(sessionId);
13+
14+
if (!isSessionValid) {
15+
res.sendStatus(403);
16+
}
17+
else {
18+
res.status(200).json(db.readAllLessons());
19+
}
920

10-
return res.status(200).json(db.readAllLessons());
1121
}

server/session-storage.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,19 @@ class SessionStore {
4848

4949
findUserbySession(sessionId:string) : User {
5050

51+
const isSessionValid = this.isSessionValid(sessionId);
52+
53+
return isSessionValid ? this.sessions[sessionId].user : undefined;
54+
}
55+
56+
isSessionValid(sessionId:string) {
57+
5158
const session = this.sessions[sessionId];
5259

5360
const isSessionValid = session && session.isValid();
5461

5562
return isSessionValid ? session.user : undefined;
63+
5664
}
5765

5866
}

src/app/app.component.html

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
src="https://angular-academy.s3.amazonaws.com/main-logo/main-page-logo-small-hat.png">
77
</a>
88
</li>
9-
10-
119
<li>
1210
<a routerLink="/lessons">Lessons</a>
1311
</li>
@@ -20,13 +18,9 @@
2018
<li *ngIf="isLoggedIn$ | async" (click)="logout()">
2119
<a>Logout</a>
2220
</li>
23-
24-
2521
</ul>
26-
2722
</header>
2823

29-
3024
<main>
3125

3226
<div class="course-header">
@@ -35,7 +29,6 @@ <h3>Angular Security MasterClass</h3>
3529
src="https://s3-us-west-1.amazonaws.com/angular-university/course-images/angular-security-thumbnail.png">
3630
</div>
3731

38-
3932
<router-outlet></router-outlet>
4033

4134
</main>

src/app/app.component.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,23 @@ import {User} from "./model/user";
88
templateUrl: './app.component.html',
99
styleUrls: ['./app.component.css']
1010
})
11-
export class AppComponent implements OnInit{
11+
export class AppComponent implements OnInit {
1212

1313
isLoggedIn$: Observable<boolean>;
1414
isLoggedOut$: Observable<boolean>;
1515

16-
1716
constructor(private authService:AuthService) {
1817

1918
}
2019

21-
2220
ngOnInit() {
23-
2421
this.isLoggedIn$ = this.authService.isLoggedIn$;
2522
this.isLoggedOut$ = this.authService.isLoggedOut$;
26-
2723
}
2824

29-
30-
3125
logout() {
32-
33-
this.authService.logout()
34-
.subscribe();
35-
26+
this.authService.logout().subscribe();
3627
}
3728

3829
}
30+

src/app/login/login.component.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Component, OnInit } from '@angular/core';
22
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
3+
import {AuthService} from "../services/auth.service";
34

45
@Component({
56
selector: 'login',
@@ -10,7 +11,7 @@ export class LoginComponent implements OnInit {
1011

1112
form:FormGroup;
1213

13-
constructor(private fb:FormBuilder) {
14+
constructor(private fb:FormBuilder, private authService:AuthService) {
1415

1516
this.form = this.fb.group({
1617
email: ['',Validators.required],
@@ -28,7 +29,8 @@ export class LoginComponent implements OnInit {
2829

2930
const formValue = this.form.value;
3031

31-
//TODO
32+
33+
3234

3335

3436
}

src/app/services/auth.service.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,23 @@ export class AuthService {
2121

2222
isLoggedOut$: Observable<boolean> = this.isLoggedIn$.map(isLoggedIn => !isLoggedIn);
2323

24-
2524
constructor(private http: HttpClient) {
2625
http.get<User>('/api/user')
2726
.subscribe(user => this.subject.next(user ? user : ANONYMOUS_USER));
2827
}
2928

30-
3129
signUp(email: string, password: string) {
3230
return this.http.post<User>('/api/signup', {email, password})
3331
.shareReplay()
3432
.do(user => this.subject.next(user));
3533
}
3634

37-
3835
logout(): Observable<any> {
3936
return this.http.post('/api/logout', null)
4037
.shareReplay()
4138
.do(user => this.subject.next(ANONYMOUS_USER));
4239
}
4340

44-
4541
}
4642

4743

0 commit comments

Comments
 (0)