Skip to content

Commit c3b3e44

Browse files
author
Your Name
committed
angular security course
1 parent 94468f4 commit c3b3e44

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

server/read-all-lessons.route.ts

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

22
import {db} from "./database";
3+
import {sessionStore} from "./session-store";
34

45

56
export function readAllLessons(req, res) {
67

7-
res.status(200).json(db.readAllLessons());
8+
const sessionId = req.cookies["SESSIONID"];
9+
10+
const isSessionValid = sessionStore.isSessionValid(sessionId);
11+
12+
if (!isSessionValid) {
13+
res.sendStatus(403);
14+
}
15+
else {
16+
res.status(200).json({lessons:db.readAllLessons()});
17+
}
818

919
}

server/session-store.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ class SessionStore {
1515

1616
const session = this.sessions[sessionId];
1717

18-
const isSessionValid = session && session.isValid();
19-
20-
return isSessionValid ? session.user : undefined;
18+
return this.isSessionValid(sessionId) ? session.user : undefined;
2119
}
2220

21+
isSessionValid(sessionId: string): boolean {
22+
23+
const session = this.sessions[sessionId];
24+
25+
return session && session.isValid();
26+
}
2327
}
2428

2529

src/app/lessons/lessons.component.html

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

2-
<div class="lessons-list-container v-h-center-block-parent">
2+
<div class="lessons-list-container v-h-center-block-parent" *ngIf="isLoggedIn$ | async">
33

44
<h2>All Lessons</h2>
55

src/app/services/lessons.service.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import {Injectable} from "@angular/core";
33
import {HttpClient} from "@angular/common/http";
44
import {Lesson} from "../model/lesson";
5+
import {Observable} from "rxjs/Observable";
56

67

78
@Injectable()
@@ -11,8 +12,9 @@ export class LessonsService {
1112

1213
}
1314

14-
loadAllLessons() {
15-
return this.http.get<Lesson[]>('/api/lessons');
15+
loadAllLessons() : Observable<Lesson[]> {
16+
return this.http.get<any>('/api/lessons')
17+
.map(res => res.lessons);
1618
}
1719

1820
findLessonById(id:number) {

0 commit comments

Comments
 (0)