Skip to content

Commit 5207ad5

Browse files
author
Your Name
committed
angular security course
1 parent b515792 commit 5207ad5

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

server/create-user.route.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {db} from "./database";
44
import {USERS} from "./database-data";
55
import * as argon2 from 'argon2';
66
import {validatePassword} from "./password-validation";
7+
import {randomBytes} from "./security.utils";
78

89

910

@@ -17,16 +18,32 @@ export function createUser(req: Request, res:Response) {
1718
res.status(400).json({errors});
1819
}
1920
else {
20-
argon2.hash(credentials.password)
21-
.then(passwordDigest => {
2221

23-
const user = db.createUser(credentials.email, passwordDigest);
22+
createUserAndSession(res, credentials);
23+
24+
}
25+
26+
}
27+
28+
async function createUserAndSession(res:Response, credentials) {
29+
30+
const passwordDigest = await argon2.hash(credentials.password);
31+
32+
const user = db.createUser(credentials.email, passwordDigest);
33+
34+
const sessionId = await randomBytes(32).then(bytes => bytes.toString('hex'));
35+
36+
console.log("sessionId",sessionId );
37+
38+
res.status(200).json({id:user.id, email:user.email});
39+
40+
41+
}
42+
43+
44+
45+
2446

25-
console.log(USERS);
2647

27-
res.status(200).json({id:user.id, email:user.email});
2848

29-
});
30-
}
3149

32-
}

server/security.utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
const util = require('util');
3+
const crypto = require('crypto');
4+
5+
6+
export const randomBytes = util.promisify(crypto.randomBytes);

0 commit comments

Comments
 (0)