Aurorachilles
Aurorachilles

Reputation: 81

How the make the JWT signature same for the same user with different keys

I am basically creating a server for authentication(sign-in/sign-up) where I am storing my hashed password in the database with the user during sign-up.

Sign-up Code

async function user_signup(user_name, pass_word) {
  //finding existing users
  const user_exist = await add_people.findOne({ username: user_name });
  if (user_exist) {
    return null; 
  }

  //creating a instance to push into the database
  const passable = new add_people({
    username: user_name,
    password: hasher(user_name, pass_word),
  });

  //saving and returning true
  passable.save();
  return true;
}

And during login, I am getting the username and password, hashing it and comparing it to the hash stored in my database.

Login Code

async function login(user_name, pass_word) {

  //finding whether the user exists or not
  const user_exist = await add_people.findOne({ username: user_name });
  const hashed = hasher(user_name, pass_word);

  if (user_exist.password === hashed) {
    return hashed;
  }
  return null;
}

Now the main issue is that, the hashes generated by both of these functions are different (the signature part). I tried looking up the documentations and https://jwt.io but the only thing I could figure out was to make it the same, I got to convert the signature to base64 encoded.

I have no idea how to code this out. Can y'all please tell me how I can implement this or a better way to do what I intend to do. Thank you.

EDIT: here is the code of my hasher

function hasher(user_name, pass_word) {
  const hash = jwt.sign(
    { username: user_name, password: pass_word },
    awt_global_pass
    });
  return hash;
}

Upvotes: 0

Views: 112

Answers (0)

Related Questions