Skip to content

Commit b515792

Browse files
committed
Angular Security course
1 parent 4f7aec0 commit b515792

14 files changed

+24
-256
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"scripts": {
99
"ng": "ng",
10-
"start": "ng serve --proxy-config ./proxy.json --ssl 1 --ssl-key key.pem --ssl-cert cert.pem",
10+
"start": "ng serve --proxy-config ./proxy.json",
1111
"start-server": "./node_modules/.bin/ts-node ./server/server.ts --secure",
1212
"server": "./node_modules/.bin/nodemon -w ./server --ext \".ts\" --exec \"npm run start-server\"",
1313
"hash": "node ./demos/hash.js",

server/create-user.route.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1+
12
import {Request, Response} from "express";
23
import {db} from "./database";
34
import {USERS} from "./database-data";
45
import * as argon2 from 'argon2';
56
import {validatePassword} from "./password-validation";
6-
import {sessionStore} from "./session-storage";
7-
import {initializeUserSession, randomBytes} from "./security.utils";
87

98

109

11-
export function createUser(req: Request, res: Response) {
10+
export function createUser(req: Request, res:Response) {
1211

1312
const credentials = req.body;
1413

@@ -18,24 +17,16 @@ export function createUser(req: Request, res: Response) {
1817
res.status(400).json({errors});
1918
}
2019
else {
20+
argon2.hash(credentials.password)
21+
.then(passwordDigest => {
2122

22-
createUserAndSession(res, credentials)
23-
.catch(err => res.status(500).json({errors: ["err_user"]}));
24-
25-
}
26-
27-
}
28-
29-
async function createUserAndSession(res: Response, credentials) {
23+
const user = db.createUser(credentials.email, passwordDigest);
3024

31-
const passwordDigest = await argon2.hash(credentials.password);
25+
console.log(USERS);
3226

33-
console.log("passwordDigest", passwordDigest);
34-
35-
const user = db.createUser(credentials.email, passwordDigest);
36-
37-
return initializeUserSession(user, res);
38-
39-
}
27+
res.status(200).json({id:user.id, email:user.email});
4028

29+
});
30+
}
4131

32+
}

server/get-user.route.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

server/login.route.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

server/logout.route.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

server/read-all-lessons.route.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11

2-
32
import {db} from "./database";
4-
import {sessionStore} from "./session-storage";
5-
63

74

85
export function readAllLessons(req, res) {
96

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-
}
7+
res.status(200).json(db.readAllLessons());
208

219
}

server/security.utils.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

server/server.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import * as fs from 'fs';
66
import * as https from 'https';
77
import {readAllLessons} from "./read-all-lessons.route";
88
import {createUser} from "./create-user.route";
9-
import {getUser} from "./get-user.route";
10-
import {logout} from "./logout.route";
11-
import {login} from "./login.route";
129
const bodyParser = require('body-parser');
1310
const cookieParser = require('cookie-parser');
1411

@@ -34,14 +31,6 @@ app.route('/api/lessons')
3431
app.route('/api/signup')
3532
.post(createUser);
3633

37-
app.route('/api/user')
38-
.get(getUser);
39-
40-
app.route('/api/logout')
41-
.post(logout);
42-
43-
app.route('/api/login')
44-
.post(login);
4534

4635
if (options.secure) {
4736

server/session-storage.ts

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/app/app.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class AppComponent implements OnInit {
2323
}
2424

2525
logout() {
26-
this.authService.logout().subscribe();
26+
2727
}
2828

2929
}

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

44
<h2>All Lessons</h2>
55

src/app/login/login.component.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,7 @@ export class LoginComponent implements OnInit {
3737

3838
if (val.email && val.password) {
3939

40-
this.authService.login(val.email, val.password)
41-
.subscribe(
42-
() => {
43-
console.log("User logged in successfully");
44-
this.router.navigateByUrl('/');
45-
},
46-
response => this.errors = response.error.errors
47-
);
40+
//TODO
4841

4942
}
5043

src/app/services/auth.service.ts

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,25 @@ export const ANONYMOUS_USER: User = {
1313
@Injectable()
1414
export class AuthService {
1515

16-
private subject = new BehaviorSubject<User>(undefined);
16+
private subject = new BehaviorSubject<User>(ANONYMOUS_USER);
1717

18-
user$: Observable<User> = this.subject.asObservable().filter(user => !!user);
18+
user$: Observable<User> = this.subject.asObservable();
1919

2020
isLoggedIn$: Observable<boolean> = this.user$.map(user => !!user.id);
2121

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

2424
constructor(private http: HttpClient) {
25-
http.get<User>('/api/user')
26-
.subscribe(user => this.subject.next(user ? user : ANONYMOUS_USER));
25+
26+
2727
}
2828

29-
signUp(email: string, password: string) {
29+
signUp(email:string, password:string ) {
30+
3031
return this.http.post<User>('/api/signup', {email, password})
3132
.shareReplay()
3233
.do(user => this.subject.next(user));
33-
}
3434

35-
login(email: string, password: string) {
36-
return this.http.post<User>('/api/login', {email, password})
37-
.shareReplay()
38-
.do(user => this.subject.next(user));
3935
}
4036

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

src/app/signup/signup.component.ts

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

2424

25-
constructor(private fb: FormBuilder, private authService: AuthService, private router:Router) {
25+
constructor(private fb: FormBuilder, private authService: AuthService) {
2626
this.form = this.fb.group({
2727
email: ['',Validators.required],
2828
password: ['',Validators.required],
@@ -43,12 +43,7 @@ export class SignupComponent implements OnInit {
4343

4444
this.authService.signUp(val.email, val.password)
4545
.subscribe(
46-
() => {
47-
console.log("User created successfully");
48-
49-
this.router.navigateByUrl('/');
50-
51-
},
46+
() => console.log("User created successfully"),
5247
response => this.errors = response.error.errors
5348
);
5449

0 commit comments

Comments
 (0)