File tree 6 files changed +26
-25
lines changed 6 files changed +26
-25
lines changed Original file line number Diff line number Diff line change 1
1
2
2
3
3
import { db } from "./database" ;
4
+ import { sessionStore } from "./session-storage" ;
4
5
5
6
6
7
7
8
export function readAllLessons ( req , res ) {
8
9
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
+ }
9
20
10
- return res . status ( 200 ) . json ( db . readAllLessons ( ) ) ;
11
21
}
Original file line number Diff line number Diff line change @@ -48,11 +48,19 @@ class SessionStore {
48
48
49
49
findUserbySession ( sessionId :string ) : User {
50
50
51
+ const isSessionValid = this . isSessionValid ( sessionId ) ;
52
+
53
+ return isSessionValid ? this . sessions [ sessionId ] . user : undefined ;
54
+ }
55
+
56
+ isSessionValid ( sessionId :string ) {
57
+
51
58
const session = this . sessions [ sessionId ] ;
52
59
53
60
const isSessionValid = session && session . isValid ( ) ;
54
61
55
62
return isSessionValid ? session . user : undefined ;
63
+
56
64
}
57
65
58
66
}
Original file line number Diff line number Diff line change 6
6
src ="https://angular-academy.s3.amazonaws.com/main-logo/main-page-logo-small-hat.png ">
7
7
</ a >
8
8
</ li >
9
-
10
-
11
9
< li >
12
10
< a routerLink ="/lessons "> Lessons</ a >
13
11
</ li >
20
18
< li *ngIf ="isLoggedIn$ | async " (click) ="logout() ">
21
19
< a > Logout</ a >
22
20
</ li >
23
-
24
-
25
21
</ ul >
26
-
27
22
</ header >
28
23
29
-
30
24
< main >
31
25
32
26
< div class ="course-header ">
@@ -35,7 +29,6 @@ <h3>Angular Security MasterClass</h3>
35
29
src ="https://s3-us-west-1.amazonaws.com/angular-university/course-images/angular-security-thumbnail.png ">
36
30
</ div >
37
31
38
-
39
32
< router-outlet > </ router-outlet >
40
33
41
34
</ main >
Original file line number Diff line number Diff line change @@ -8,31 +8,23 @@ import {User} from "./model/user";
8
8
templateUrl : './app.component.html' ,
9
9
styleUrls : [ './app.component.css' ]
10
10
} )
11
- export class AppComponent implements OnInit {
11
+ export class AppComponent implements OnInit {
12
12
13
13
isLoggedIn$ : Observable < boolean > ;
14
14
isLoggedOut$ : Observable < boolean > ;
15
15
16
-
17
16
constructor ( private authService :AuthService ) {
18
17
19
18
}
20
19
21
-
22
20
ngOnInit ( ) {
23
-
24
21
this . isLoggedIn$ = this . authService . isLoggedIn$ ;
25
22
this . isLoggedOut$ = this . authService . isLoggedOut$ ;
26
-
27
23
}
28
24
29
-
30
-
31
25
logout ( ) {
32
-
33
- this . authService . logout ( )
34
- . subscribe ( ) ;
35
-
26
+ this . authService . logout ( ) . subscribe ( ) ;
36
27
}
37
28
38
29
}
30
+
Original file line number Diff line number Diff line change 1
1
import { Component , OnInit } from '@angular/core' ;
2
2
import { FormBuilder , FormGroup , Validators } from "@angular/forms" ;
3
+ import { AuthService } from "../services/auth.service" ;
3
4
4
5
@Component ( {
5
6
selector : 'login' ,
@@ -10,7 +11,7 @@ export class LoginComponent implements OnInit {
10
11
11
12
form :FormGroup ;
12
13
13
- constructor ( private fb :FormBuilder ) {
14
+ constructor ( private fb :FormBuilder , private authService : AuthService ) {
14
15
15
16
this . form = this . fb . group ( {
16
17
email : [ '' , Validators . required ] ,
@@ -28,7 +29,8 @@ export class LoginComponent implements OnInit {
28
29
29
30
const formValue = this . form . value ;
30
31
31
- //TODO
32
+
33
+
32
34
33
35
34
36
}
Original file line number Diff line number Diff line change @@ -21,27 +21,23 @@ export class AuthService {
21
21
22
22
isLoggedOut$ : Observable < boolean > = this . isLoggedIn$ . map ( isLoggedIn => ! isLoggedIn ) ;
23
23
24
-
25
24
constructor ( private http : HttpClient ) {
26
25
http . get < User > ( '/api/user' )
27
26
. subscribe ( user => this . subject . next ( user ? user : ANONYMOUS_USER ) ) ;
28
27
}
29
28
30
-
31
29
signUp ( email : string , password : string ) {
32
30
return this . http . post < User > ( '/api/signup' , { email, password} )
33
31
. shareReplay ( )
34
32
. do ( user => this . subject . next ( user ) ) ;
35
33
}
36
34
37
-
38
35
logout ( ) : Observable < any > {
39
36
return this . http . post ( '/api/logout' , null )
40
37
. shareReplay ( )
41
38
. do ( user => this . subject . next ( ANONYMOUS_USER ) ) ;
42
39
}
43
40
44
-
45
41
}
46
42
47
43
You can’t perform that action at this time.
0 commit comments