Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week-3 JWT Assignment #1037

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions week-3/02-jwt/index.js
Original file line number Diff line number Diff line change
@@ -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.
*
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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,
Expand Down