Skip to content

Commit e98f4b3

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

File tree

6 files changed

+48
-3
lines changed

6 files changed

+48
-3
lines changed

server/logout.route.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
3+
import {Request, Response} from 'express';
4+
import {sessionStore} from "./session-store";
5+
6+
7+
8+
export function logout(req: Request, res: Response) {
9+
10+
const sessionId = req.cookies['SESSIONID'];
11+
12+
sessionStore.destroySession(sessionId);
13+
14+
res.clearCookie("SESSIONID");
15+
16+
res.sendStatus(200);
17+
}

server/server.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as https from 'https';
77
import {readAllLessons} from "./read-all-lessons.route";
88
import {createUser} from "./create-user.route";
99
import {getUser} from "./get-user.route";
10+
import {logout} from "./logout.route";
1011
const bodyParser = require('body-parser');
1112
const cookieParser = require('cookie-parser');
1213

@@ -35,6 +36,9 @@ app.route('/api/signup')
3536
app.route('/api/user')
3637
.get(getUser);
3738

39+
app.route('/api/logout')
40+
.post(logout);
41+
3842

3943
if (options.secure) {
4044

server/session-store.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class SessionStore {
2424

2525
return session && session.isValid();
2626
}
27+
28+
destroySession(sessionId: string) {
29+
delete this.sessions[sessionId];
30+
}
2731
}
2832

2933

src/app/app.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export class AppComponent implements OnInit {
2424

2525
logout() {
2626

27+
this.authService.logout().subscribe();
28+
2729
}
2830

2931
}

src/app/services/auth.service.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,17 @@ export class AuthService {
3434

3535
}
3636

37-
}
37+
logout() : Observable<any> {
38+
return this.http.post('/api/logout', null)
39+
.shareReplay()
40+
.do(user => this.subject.next(ANONYMOUS_USER));
41+
}
42+
}
43+
44+
45+
46+
47+
48+
49+
50+

src/app/signup/signup.component.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export class SignupComponent implements OnInit {
2222
};
2323

2424

25-
constructor(private fb: FormBuilder, private authService: AuthService) {
25+
constructor(private fb: FormBuilder, private authService: AuthService,
26+
private router:Router) {
2627
this.form = this.fb.group({
2728
email: ['',Validators.required],
2829
password: ['',Validators.required],
@@ -43,7 +44,11 @@ export class SignupComponent implements OnInit {
4344

4445
this.authService.signUp(val.email, val.password)
4546
.subscribe(
46-
() => console.log("User created successfully"),
47+
() => {
48+
this.router.navigateByUrl('/');
49+
50+
console.log("User created successfully")
51+
},
4752
response => this.errors = response.error.errors
4853
);
4954

0 commit comments

Comments
 (0)