Skip to content

Commit 064d583

Browse files
committed
added solution for 02-jwt
1 parent e94da43 commit 064d583

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

week-3/02-jwt/index.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
const jwt = require('jsonwebtoken');
22
const jwtPassword = 'secret';
3+
const z = require('zod');
4+
5+
6+
usernameSchema = z.string().email();
7+
passwordSchema = z.string().min(6);
38

49

510
/**
@@ -15,6 +20,17 @@ const jwtPassword = 'secret';
1520
*/
1621
function signJwt(username, password) {
1722
// 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;
1834
}
1935

2036
/**
@@ -27,17 +43,29 @@ function signJwt(username, password) {
2743
*/
2844
function verifyJwt(token) {
2945
// Your code here
46+
try {
47+
jwt.verify(token, jwtPassword)
48+
return true;
49+
} catch (error) {
50+
return false;
51+
}
52+
3053
}
3154

3255
/**
3356
* Decodes a JWT to reveal its payload without verifying its authenticity.
3457
*
3558
* @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.
3760
* Returns false if the token is not a valid JWT format.
3861
*/
3962
function decodeJwt(token) {
4063
// Your code here
64+
const decoded = jwt.decode(token);
65+
if (decoded) {
66+
return true;
67+
}
68+
else return false;
4169
}
4270

4371

week-3/02-jwt/tests/index.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,18 @@ describe('decodeJwt', () => {
4848
describe('verifyJwt', () => {
4949
test('cant decode a jwt with diff password correctly', () => {
5050
const token = jwt.sign({ username: '[email protected]', password: '123456' }, "randomPassword");
51-
const decoded = verifyJwt(token);
52-
expect(decoded).toBe(false);
51+
const verified = verifyJwt(token);
52+
expect(verified).toBe(false);
5353
});
5454

5555
test('decodes a jwt with same password correctly', () => {
5656
const token = jwt.sign({ username: '[email protected]', password: '123456' }, jwtPassword);
57-
const decoded = verifyJwt(token);
58-
expect(decoded).toBe(true);
57+
const verified = verifyJwt(token);
58+
expect(verified).toBe(true);
5959
});
6060

6161
test('cant decode a non jwt string', () => {
62-
const decoded = verifyJwt("token");
63-
expect(decoded).toBe(false);
62+
const verified = verifyJwt("token");
63+
expect(verified).toBe(false);
6464
});
6565
});

0 commit comments

Comments
 (0)