From 607443292182117903c1fa6f1ff1ab229a1040dd Mon Sep 17 00:00:00 2001 From: vishal-sharma-r Date: Sat, 31 Aug 2024 15:51:03 +0530 Subject: [PATCH] week 3 jwt assignment --- week-3/02-jwt/index.js | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/week-3/02-jwt/index.js b/week-3/02-jwt/index.js index fb7c604c81..6fcd8e08c9 100644 --- a/week-3/02-jwt/index.js +++ b/week-3/02-jwt/index.js @@ -1,7 +1,12 @@ -const jwt = require('jsonwebtoken'); -const jwtPassword = 'secret'; - +const jwt = require("jsonwebtoken"); +const jwtPassword = "secret"; +const zod = require("zod"); +/** + * Zod schema verify + */ +const emailSchema = zod.string().email(); +const passwordSchema = zod.string().min(6); /** * Generates a JWT for a given username and password. * @@ -14,7 +19,14 @@ const jwtPassword = 'secret'; * the password does not meet the length requirement. */ function signJwt(username, password) { - // Your code here + // Your code here + const verifyEmail = emailSchema.safeParse(username); + const verifyPassword = passwordSchema.safeParse(password); + if (!verifyEmail.success || !verifyPassword.success) { + return null; + } + const response = jwt.sign({ username}, jwtPassword); + return response; } /** @@ -26,7 +38,13 @@ function signJwt(username, password) { * using the secret key. */ function verifyJwt(token) { - // Your code here + // Your code here + try{ + jwt.verify(token,jwtPassword); + return true; + } + catch(e){} + return false; } /** @@ -37,10 +55,12 @@ function verifyJwt(token) { * Returns false if the token is not a valid JWT format. */ function decodeJwt(token) { - // Your code here + // Your code here + const decodeJwt = jwt.decode(token); + if(!decodeJwt) return false; + return true; } - module.exports = { signJwt, verifyJwt,