forked from TomDoesTech/Testing-Express-REST-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjwt.utils.ts
More file actions
30 lines (27 loc) · 681 Bytes
/
Copy pathjwt.utils.ts
File metadata and controls
30 lines (27 loc) · 681 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import jwt from "jsonwebtoken";
import config from "config";
const privateKey = config.get<string>("privateKey");
const publicKey = config.get<string>("publicKey");
export function signJwt(object: Object, options?: jwt.SignOptions | undefined) {
return jwt.sign(object, privateKey, {
...(options && options),
algorithm: "RS256",
});
}
export function verifyJwt(token: string) {
try {
const decoded = jwt.verify(token, publicKey);
return {
valid: true,
expired: false,
decoded,
};
} catch (e: any) {
console.error(e);
return {
valid: false,
expired: e.message === "jwt expired",
decoded: null,
};
}
}