1
1
const jwt = require ( 'jsonwebtoken' ) ;
2
2
const jwtPassword = 'secret' ;
3
+ const z = require ( 'zod' ) ;
4
+
5
+
6
+ usernameSchema = z . string ( ) . email ( ) ;
7
+ passwordSchema = z . string ( ) . min ( 6 ) ;
3
8
4
9
5
10
/**
@@ -15,6 +20,17 @@ const jwtPassword = 'secret';
15
20
*/
16
21
function signJwt ( username , password ) {
17
22
// Your code here
23
+ const usernameResponse = usernameSchema . safeParse ( username ) ;
24
+ const passwordResponse = passwordSchema . safeParse ( password ) ;
25
+
26
+ if ( ! usernameResponse . success || ! passwordResponse . success ) {
27
+ return null ;
28
+ }
29
+
30
+ const signature = jwt . sign ( { username, password} , jwtPassword ) ;
31
+ console . log ( signature ) ;
32
+
33
+ return signature ;
18
34
}
19
35
20
36
/**
@@ -27,17 +43,29 @@ function signJwt(username, password) {
27
43
*/
28
44
function verifyJwt ( token ) {
29
45
// Your code here
46
+ try {
47
+ jwt . verify ( token , jwtPassword )
48
+ return true ;
49
+ } catch ( error ) {
50
+ return false ;
51
+ }
52
+
30
53
}
31
54
32
55
/**
33
56
* Decodes a JWT to reveal its payload without verifying its authenticity.
34
57
*
35
58
* @param {string } token - The JWT string to decode.
36
- * @returns {object|false } The decoded payload of the JWT if the token is a valid JWT format.
59
+ * @returns {object|false } Returns true if the decoded payload of the JWT if the token is a valid JWT format.
37
60
* Returns false if the token is not a valid JWT format.
38
61
*/
39
62
function decodeJwt ( token ) {
40
63
// Your code here
64
+ const decoded = jwt . decode ( token ) ;
65
+ if ( decoded ) {
66
+ return true ;
67
+ }
68
+ else return false ;
41
69
}
42
70
43
71
0 commit comments