Table of contents
Techniques
Hashing
Encryption
JSON Web Token
Local Storage
Hashing
Hashing is one directional. Once you get an output you cannot reverse it to get the input. Each input gives a unique output.
Changing the input a little does a drastic change on the output.
Encryption
Encryption is two way. A string can be encrypted using a password. The same password is used for decryption.
JSON Web Token
It uses signatures. Anyone can see this signature. But this signature can be verified only using a secret key.
Local Storage
A place in your browser where that can store some data.
JSON Web Token
Let's look at one example:
Create a basic app that creates a token when user signs in. This token can be decoded and it must return true. This token must be verified too.
const jwt = require('jsonwebtoken')
const jwtPassword = 'secret'
const zod = require('zod')
const emailSchema = zod.string().email();
const passwordSchema = zod.string().min(6);
function signUp(username, password){
const usernameRes = emailSchema.safeParse(username);
const passwordRes = passwordSchema.safeParse(password);
if (!usernameRes.success || !passwordRes.success){
return null;
}
const token = jwt.sign({username}, jwtPassword);
return token
}
function decodeJWT(token){
const decoded = jwt.decode(token);
if(decoded){
return true;
}else{
return false;
}
}
function verifyJWT(token){
try{
const verified = jwt.verify(token, jwtPassword);
return verified;
}catch(e){
return null;
}
}