How to keep your web app users passwords safe and getting started with authentication in bcrypt

Search for a command to run...

No comments yet. Be the first to comment.
Hey there! Are you feeling stuck in the crowd? Then I've gotta way out! π There is no alternative to hands-on for succeeding as a developer! basic must-knows googling the stuff you don't know that's the most basic thing to know first, as a develo...

Let's make your web app blazing fast!

Hey! I made a TODO web app just for trying out VueJS, with not so cool design. But it's okay for me, as an experiment. This is what I created π¨βπ» Github Repo Create these three files: index.html app.js style.css index.html Things wrapped in {...

The world is developing rapidly with rapid improvements in technology. In this fast-moving world, we the developers also need to tie our shoes tight to focus on running faster! πββοΈπ₯ Today, I'm talking about the serverless architecture that will he...

What? π€ AWS recently released Amplify Admin UI, which enables you to seamlessly create back-end and cloud databases for your next project! Supported for: web, android, iOS Support for Android: Java, Kotlin Support for iOS: SwiftUI Frameworks suppor...

Hope you guys are having a good day π.
This article is more focused on how user credentials on a website are generated, stored, secured, and authenticated. π We'll cover it all and will see how to implement bcrypt practically so that you can too follow this approach. π¦ΈββοΈ
Be it Instagram, Facebook, Twitter, Github, or any other platform that requires users to signing using a password. Did you ever wonder how you are authenticated? and how your passwords are stored?
Let's imagine that you went to a concert π, their entry was based on tickets. Your ticket has an ID, date, and stamp. And only if those ID, date, and stamp are valid, then you will be granted entry. β
Similar is with websites when you sign in successfully, a unique encrypted token is generated π which contains some identification information of the user such as - username or email.
Now, this token will grant you access across the website's secured pages. π One of the most commonly used tokens is JSON Web Tokens
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022
}
This is pretty much how authentication works. βNo, they're not directly stored in the database. β
It is first encrypted into a hash using various hashing methods, one of the most popularly used methods is bcrypt. β
Let's say the company had a bad day and their database gets breached by hackers. In this situation at least the user's original credentials are still protected and non-disclosed.π€« Which prevents the worst case to happen. Therefore, it's always advisable to don't store plain text passwords in the database. Instead, store it in as an encrypted or hashed form.
Setting up bcrypt (javascript)
const bcrypt = require('bcrypt');
const saltRounds = 10;
const myCorrectPlaintextPassword = 's0/\/\P4$$w0rD';
const notCorrectPlaintextPassword = 'this_is_incorrect_password';
Hashing passwords, here we are generating a salt and then using that salt to hash the password
const salt = bcrypt.genSaltSync(saltRounds);
const hash = bcrypt.hashSync(myCorrectPlaintextPassword, salt);
// Store hash in your password DB.
Checking password when a user attempts to log in.
// Load hash from your password DB.
bcrypt.compare(myCorrectPlaintextPassword, hash).then(function(result) {
// result == true
// access granted
});
bcrypt.compare(notCorrectPlaintextPassword, hash).then(function(result) {
// result == false
// access denied
});
Feel free to reach me on Twitter @amaancodes for any query. Happy to help ππ€
Lastly, don't forget to drop your views about this article in the comments! π See ya!