Skip to content
This repository was archived by the owner on Apr 19, 2023. It is now read-only.

Commit 4ee19e7

Browse files
✨ Add generating JWT
1 parent 9ad7747 commit 4ee19e7

File tree

6 files changed

+132
-2
lines changed

6 files changed

+132
-2
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"@types/express": "^4.16.1",
1919
"@types/fs-extra": "^5.0.5",
2020
"@types/jest": "^24.0.11",
21+
"@types/jsonwebtoken": "^8.3.2",
2122
"@types/marked": "^0.6.5",
2223
"@types/mysql": "^2.15.5",
2324
"@types/node": "^11.13.6",
@@ -36,6 +37,7 @@
3637
"@types/mustache": "^0.8.32",
3738
"bcrypt": "^3.0.6",
3839
"fs-extra": "^7.0.1",
40+
"jsonwebtoken": "^8.5.1",
3941
"marked": "^0.6.2",
4042
"mustache": "^3.0.1",
4143
"mysql": "^2.17.1",

src/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ export const SES_EMAIL = process.env.SES_EMAIL || "";
1818
export const SES_REGION = process.env.SES_REGION || "eu-west-1";
1919
export const SES_ACCESS = process.env.SES_ACCESS || "";
2020
export const SES_SECRET = process.env.SES_SECRET || "";
21+
22+
// Auth and tokens
23+
export const JWT_SECRET = process.env.JWT_SECRET || "staart";
24+
export const JWT_ISSUER = process.env.JWT_ISSUER || "staart";

src/helpers/jwt.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { sign, Secret } from "jsonwebtoken";
2+
import { JWT_ISSUER, JWT_SECRET } from "../config";
3+
4+
export const generateToken = (
5+
payload: string | object | Buffer,
6+
expiresIn: string | number,
7+
subject: string
8+
) =>
9+
new Promise((resolve, reject) => {
10+
sign(
11+
payload,
12+
JWT_SECRET,
13+
{
14+
expiresIn,
15+
subject,
16+
issuer: JWT_ISSUER
17+
},
18+
(error, token) => {
19+
if (error) return reject(error);
20+
resolve(token);
21+
}
22+
);
23+
});
24+
25+
export const emailVerificationToken = async (id: number) =>
26+
generateToken({ id }, "7d", "email-verify");

src/rest/auth.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createUser, updateUser } from "../crud/user";
33
import { InsertResult } from "../interfaces/mysql";
44
import { createEmail } from "../crud/email";
55
import { mail } from "../helpers/mail";
6+
import { emailVerificationToken } from "../helpers/jwt";
67

78
export const register = async (
89
user: User,
@@ -21,7 +22,17 @@ export const register = async (
2122
});
2223
const emailId = newEmail.insertId;
2324
await updateUser(userId, { primaryEmail: emailId });
24-
await mail(email, "verify-email", { name: user.name, email });
25+
await sendEmailVerification(emailId, email, user);
2526
}
2627
return { created: true };
2728
};
29+
30+
export const sendEmailVerification = async (
31+
id: number,
32+
email: string,
33+
user: User
34+
) => {
35+
const token = emailVerificationToken(id);
36+
await mail(email, "verify-email", { name: user.name, email, token });
37+
return;
38+
};

src/templates/verify-email.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Dear {{name}},
44

5-
Click here to verify your email address: https://example.com?email={{email}}.
5+
Click here to verify your email address: https://example.com?email={{email}}&token={{token}}.
66

77
If you didn't request this email, you can safely ignore it.
88

yarn.lock

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,13 @@
389389
dependencies:
390390
"@types/jest-diff" "*"
391391

392+
"@types/jsonwebtoken@^8.3.2":
393+
version "8.3.2"
394+
resolved "https://registry.yarnpkg.com/@types/jsonwebtoken/-/jsonwebtoken-8.3.2.tgz#e3d5245197152346fae7ee87d5541aa5a92d0362"
395+
integrity sha512-Mkjljd9DTpkPlrmGfTJvcP4aBU7yO2QmW7wNVhV4/6AEUxYoacqU7FJU/N0yFEHTsIrE4da3rUrjrR5ejicFmA==
396+
dependencies:
397+
"@types/node" "*"
398+
392399
"@types/marked@^0.6.5":
393400
version "0.6.5"
394401
resolved "https://registry.yarnpkg.com/@types/marked/-/marked-0.6.5.tgz#3cf2a56ef615dad24aaf99784ef90a9eba4e29d8"
@@ -812,6 +819,11 @@ bser@^2.0.0:
812819
dependencies:
813820
node-int64 "^0.4.0"
814821

822+
buffer-equal-constant-time@1.0.1:
823+
version "1.0.1"
824+
resolved "https://registry.yarnpkg.com/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz#f8e71132f7ffe6e01a5c9697a4c6f3e48d5cc819"
825+
integrity sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk=
826+
815827
buffer-from@1.x, buffer-from@^1.0.0:
816828
version "1.1.1"
817829
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
@@ -1283,6 +1295,13 @@ ecc-jsbn@~0.1.1:
12831295
jsbn "~0.1.0"
12841296
safer-buffer "^2.1.0"
12851297

1298+
ecdsa-sig-formatter@1.0.11:
1299+
version "1.0.11"
1300+
resolved "https://registry.yarnpkg.com/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz#ae0f0fa2d85045ef14a817daa3ce9acd0489e5bf"
1301+
integrity sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==
1302+
dependencies:
1303+
safe-buffer "^5.0.1"
1304+
12861305
ee-first@1.1.1:
12871306
version "1.1.1"
12881307
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
@@ -2702,6 +2721,22 @@ jsonfile@^4.0.0:
27022721
optionalDependencies:
27032722
graceful-fs "^4.1.6"
27042723

2724+
jsonwebtoken@^8.5.1:
2725+
version "8.5.1"
2726+
resolved "https://registry.yarnpkg.com/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz#00e71e0b8df54c2121a1f26137df2280673bcc0d"
2727+
integrity sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w==
2728+
dependencies:
2729+
jws "^3.2.2"
2730+
lodash.includes "^4.3.0"
2731+
lodash.isboolean "^3.0.3"
2732+
lodash.isinteger "^4.0.4"
2733+
lodash.isnumber "^3.0.3"
2734+
lodash.isplainobject "^4.0.6"
2735+
lodash.isstring "^4.0.1"
2736+
lodash.once "^4.0.0"
2737+
ms "^2.1.1"
2738+
semver "^5.6.0"
2739+
27052740
jsprim@^1.2.2:
27062741
version "1.4.1"
27072742
resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2"
@@ -2712,6 +2747,23 @@ jsprim@^1.2.2:
27122747
json-schema "0.2.3"
27132748
verror "1.10.0"
27142749

2750+
jwa@^1.4.1:
2751+
version "1.4.1"
2752+
resolved "https://registry.yarnpkg.com/jwa/-/jwa-1.4.1.tgz#743c32985cb9e98655530d53641b66c8645b039a"
2753+
integrity sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==
2754+
dependencies:
2755+
buffer-equal-constant-time "1.0.1"
2756+
ecdsa-sig-formatter "1.0.11"
2757+
safe-buffer "^5.0.1"
2758+
2759+
jws@^3.2.2:
2760+
version "3.2.2"
2761+
resolved "https://registry.yarnpkg.com/jws/-/jws-3.2.2.tgz#001099f3639468c9414000e99995fa52fb478304"
2762+
integrity sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==
2763+
dependencies:
2764+
jwa "^1.4.1"
2765+
safe-buffer "^5.0.1"
2766+
27152767
kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0:
27162768
version "3.2.2"
27172769
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"
@@ -2796,6 +2848,41 @@ locate-path@^3.0.0:
27962848
p-locate "^3.0.0"
27972849
path-exists "^3.0.0"
27982850

2851+
lodash.includes@^4.3.0:
2852+
version "4.3.0"
2853+
resolved "https://registry.yarnpkg.com/lodash.includes/-/lodash.includes-4.3.0.tgz#60bb98a87cb923c68ca1e51325483314849f553f"
2854+
integrity sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8=
2855+
2856+
lodash.isboolean@^3.0.3:
2857+
version "3.0.3"
2858+
resolved "https://registry.yarnpkg.com/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz#6c2e171db2a257cd96802fd43b01b20d5f5870f6"
2859+
integrity sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY=
2860+
2861+
lodash.isinteger@^4.0.4:
2862+
version "4.0.4"
2863+
resolved "https://registry.yarnpkg.com/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz#619c0af3d03f8b04c31f5882840b77b11cd68343"
2864+
integrity sha1-YZwK89A/iwTDH1iChAt3sRzWg0M=
2865+
2866+
lodash.isnumber@^3.0.3:
2867+
version "3.0.3"
2868+
resolved "https://registry.yarnpkg.com/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz#3ce76810c5928d03352301ac287317f11c0b1ffc"
2869+
integrity sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w=
2870+
2871+
lodash.isplainobject@^4.0.6:
2872+
version "4.0.6"
2873+
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"
2874+
integrity sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs=
2875+
2876+
lodash.isstring@^4.0.1:
2877+
version "4.0.1"
2878+
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
2879+
integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=
2880+
2881+
lodash.once@^4.0.0:
2882+
version "4.1.1"
2883+
resolved "https://registry.yarnpkg.com/lodash.once/-/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac"
2884+
integrity sha1-DdOXEhPHxW34gJd9UEyI+0cal6w=
2885+
27992886
lodash.sortby@^4.7.0:
28002887
version "4.7.0"
28012888
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"

0 commit comments

Comments
 (0)