A useful library to create and read environment variables.
Store/get information privately from a machine, without pushing it to Github.
npm i dotenv
// this loads all variables created in the .env file
import 'dotenv/config'
// We can then access any variable with:
process.env.MY_VARIABLE_NAME
Library that helps us create a hash or a password, which is garbage code that cannot be easily decoded
We do not want to store plain text passwords from our users directly in our DB, because then anybody with access to our DB would be able to see them. Storing this garbage code, we can let bcrypt compare the password the user enters when they sign in, with the password they created when they signed up, and can verify they are who they say they are without having to know their actual password.
npm i bcryptjs @types/bcryptjs
import bcrypt from 'bcryptjs'
// create a hash
const hash = bcrypt.hashSync(password, 8)
// check if a password matches a hash
const passwordMatches = bcrypt.compareSync(password, user.password)
A library that generates a token based on some data given and a secret. It can then verify that a token has a valid signature, and decode its data.
This token is a useful piece of information we can store in the browser. Sending this to our server lets our server know that we are holding a token issued by the server, and it can therefore trust we are who we say we are.
npm i jsonwebtoken @types/jsonwebtoken
import jwt from 'jsonwebtoken'
// create a token
const token = jwt.sign({ id: 3 }, process.env.MY_SECRET, { expiresIn: '3days' })
// verify and decode a token
// once this is done, we can trust this was a token issued by our server
// we also have the user's id and can find them on our DB
const decodedData = jwt.verify(token, process.env.MY_SECRET)
Once your client has received a token you can:
- store it in localStorage so you don't lose it on refresh
- send it over with every request you make to the server, so they know it's you
localStorage.token = tokenGoesHere
// alternative syntax
localStorage.setItem('token', tokenGoesHere)
localStorage.token
// alternative syntax
localStorage.getItem('token')
localStorage.removeItem('token')
fetch(someUrl, {
headers: {
Authorization: insertYourTokenHere
}
})
- Request: POST
/sign-up
{ email, password } - Response: { user, token }
- Request: POST
/sign-in
{ email, password } - Response: { user, token }
- Request: GET
/validate
- Headers { Authorization: token }
- Response: { user }