Skip to content

Commit 2bb0967

Browse files
author
Your Name
committed
angular security course
1 parent a9f8128 commit 2bb0967

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

server/create-user.route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {db} from "./database";
44
import * as argon2 from 'argon2';
55
import {validatePassword} from "./password-validation";
66
import moment = require("moment");
7+
import {createSessionToken} from "./security.utils";
78

89

910

@@ -31,8 +32,7 @@ async function createUserAndSession(res:Response, credentials) {
3132

3233
const user = db.createUser(credentials.email, passwordDigest);
3334

34-
// TODO replace with JWT
35-
const sessionToken = 1;
35+
const sessionToken = await createSessionToken(user.id.toString());
3636

3737
res.cookie("SESSIONID", sessionToken, {httpOnly:true, secure:true});
3838

server/security.utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as fs from "fs";
1111

1212
export const randomBytes = util.promisify(crypto.randomBytes);
1313

14+
export const signJwt = util.promisify(jwt.sign);
1415

1516

1617
const RSA_PRIVATE_KEY = fs.readFileSync('./demos/private.key');
@@ -20,3 +21,18 @@ const RSA_PUBLIC_KEY = fs.readFileSync('./demos/public.key');
2021
const SESSION_DURATION = 240;
2122

2223

24+
export async function createSessionToken(userId:string) {
25+
return signJwt({}, RSA_PRIVATE_KEY, {
26+
algorithm: 'RS256',
27+
expiresIn: 240,
28+
subject: userId
29+
});
30+
}
31+
32+
33+
34+
35+
36+
37+
38+

0 commit comments

Comments
 (0)