Skip to content

Commit 23dfa9b

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

File tree

5 files changed

+106
-1
lines changed

5 files changed

+106
-1
lines changed

server/login.route.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
3+
import {Request, Response} from "express";
4+
import {db} from "./database";
5+
import * as argon2 from 'argon2';
6+
import {User} from "../src/app/model/user";
7+
import {DbUser} from "./db-user";
8+
import {randomBytes} from "./security.utils";
9+
import {sessionStore} from "./session-store";
10+
11+
12+
13+
export function login(req: Request, res: Response) {
14+
15+
const credentials = req.body;
16+
17+
const user = db.findUserByEmail(credentials.email);
18+
19+
if (!user) {
20+
res.sendStatus(403);
21+
}
22+
else {
23+
loginAndBuildResponse(credentials, user, res);
24+
}
25+
26+
}
27+
28+
async function loginAndBuildResponse(credentials:any, user:DbUser, res: Response) {
29+
30+
try {
31+
32+
const sessionId = await attemptLogin(credentials, user);
33+
34+
console.log("Login successful");
35+
36+
res.cookie("SESSIONID", sessionId, {httpOnly:true, secure:true});
37+
38+
res.status(200).json({id:user.id, email:user.email});
39+
40+
41+
}
42+
catch(err) {
43+
44+
console.log("Login failed!");
45+
46+
res.sendStatus(403);
47+
48+
}
49+
}
50+
51+
52+
async function attemptLogin(credentials:any, user:DbUser) {
53+
54+
const isPasswordValid = await argon2.verify(user.passwordDigest,
55+
credentials.password);
56+
57+
if (!isPasswordValid) {
58+
throw new Error("Password Invalid");
59+
}
60+
61+
const sessionId = await randomBytes(32).then(bytes => bytes.toString('hex'));
62+
63+
console.log("sessionId",sessionId );
64+
65+
sessionStore.createSession(sessionId, user);
66+
67+
return sessionId;
68+
}
69+
70+
71+
72+
73+
74+

server/security.utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11

2+
import {User} from "../src/app/model/user";
3+
import {sessionStore} from "./session-store";
4+
5+
import { Response} from "express";
6+
27
const util = require('util');
38
const crypto = require('crypto');
49

510

611
export const randomBytes = util.promisify(crypto.randomBytes);
12+
13+

server/server.ts

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

@@ -39,6 +40,9 @@ app.route('/api/user')
3940
app.route('/api/logout')
4041
.post(logout);
4142

43+
app.route('/api/login')
44+
.post(login);
45+
4246

4347
if (options.secure) {
4448

src/app/login/login.component.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,26 @@ export class LoginComponent implements OnInit {
3737

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

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

4248
}
4349

4450

4551
}
4652

4753
}
54+
55+
56+
57+
58+
59+
60+
61+
62+

src/app/services/auth.service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ export class AuthService {
3131
return this.http.post<User>('/api/signup', {email, password})
3232
.shareReplay()
3333
.do(user => this.subject.next(user));
34+
}
3435

36+
login(email:string, password:string ) {
37+
return this.http.post<User>('/api/login', {email, password})
38+
.shareReplay()
39+
.do(user => this.subject.next(user));
3540
}
3641

3742
logout() : Observable<any> {

0 commit comments

Comments
 (0)