Javascript ES6 — const vs let # 2

As a reminder from the previous video, you already know that the keyword var
is supposed to create global variables that can be accessed from the global scope, and you learn also that the var keyword is function scoped.

Now we’ve to deal with two new keywords, let and const which are block scoped,
that means that if you create any variable within a block, as an example a
function, a condition, or a for loop, it will be accessible only within its
scope, still there are some differences between let and const that we’ll
discover on this video.

Read More

Javascript ES6 — var scoping # 1

lets understand how the var keyword works to create variables, so let’s start by creating a number variable using the keyword var that have a value of 10, accessing to the value of number after the declaration will display as a result the number 10, now if I take the same variable number and without redeclaring it I change its value to 20, now accessing the value of number before declaring it will still the same, however accessing to the same variable after changing its value will give me the updated value which it’s 20. from that, we assume that the variable number can update its values through the program, until now, noting is alerting.

Read More

Use bcrypt to campare passwords using nodejs

Storing your password as a plain text is fast, however it is not secure, this why you have to make it complicated for hackers to get important information by using hashing, there is a couple of hashing functions like md5, we’ll not use md5 which it is not designed for passwords plus it’s cryptographically broken when the attacker can generate a list of common password and their corresponding hashes, then comparing the hashes to the site has stored.

You’ll use bcrypt which it is more designed for passwords, bcrypt use a salt to make a hash output unique even if your users use the same password, and this is a simple use case of bcrypt for a user who wanna update his password.

const bcrypt = require("bcryptjs");

// Generate Salt
const salt = bcrypt.genSaltSync(10);

// Plain Text Passwords
const currentpPassword = "abc123";
const oldPassword = "abc123";
const newPassword = "nWd6yCyj";

// Generate the Current User Password Hash
// by combining the salt and the password
const currentPasswordHash = bcrypt.hashSync(currentpPassword, salt);

// Compare the Old Password set by the user
// to the Current Password Hash
if (!bcrypt.compareSync(oldPassword, currentPasswordHash)) {
  console.log("The Current Password is Wrong");
}

// The new password should not be similar
// to the old password
if (bcrypt.compareSync(newPassword, currentPasswordHash)) {
  console.log(
    "The new password is similar to the new password, please choose a different one",
  );
}

Solve the Cannot read property hash of undefined error

After I update the next.js from the version 6.1.2 to the newest version at the time 7.0 I run yarn build to do the compilation and this is what I had as an errors:

[hardsource:efb84745] Could not freeze ./lib/queries.js: Cannot read property 'hash' of undefined
[hardsource:efb84745] Could not freeze ./pages/signin.js + 1 modules: Cannot read property 'hash' of undefined
[hardsource:efb84745] Could not freeze ./pages/create-account.js + 2 modules: Cannot read property 'hash' of undefined
[hardsource:efb84745] Could not freeze ./pages/activate-account.js + 1 modules: Cannot read property 'hash' of undefined
[hardsource:efb84745] Could not freeze ./pages/reset-password.js + 1 modules: Cannot read property 'hash' of undefined
[hardsource:efb84745] Could not freeze ./pages/reset-password-confirmation.js + 1 modules: Cannot read property 'hash' of undefined

To solve this problem I remove the hard-source folder from /node_modules/.cache/hard-source/ and I rerun yarn build again.

yarn build
yarn run v1.9.4
$ next build
[hardsource:8c7e1fbc] Using 7 MB of disk space.
[hardsource:8c7e1fbc] Tracking node dependencies with: package-lock.json, yarn.lock.
[hardsource:8c7e1fbc] Reading from cache 8c7e1fbc...
[hardsource:efb84745] Using 7 MB of disk space.
[11:57:22 AM] Compiling client
[hardsource:efb84745] Tracking node dependencies with: package-lock.json, yarn.lock.
[hardsource:efb84745] Reading from cache efb84745...
[11:57:49 AM] Compiling server
[11:57:50 AM] Compiled client in 28s
[11:57:50 AM] Compiled server in 662ms
✨  Done in 34.11s.

Capitalise the first letter of each word using javascript

As an Example you’ve a full name john doe that you want it to be converted to John Doe when each first letter of the full name has to be in uppercase, to achieve that you’ll create a function that analyse a string entry and use regular expression to convert first letter to the uppercase form:

const capitalize = str => {
  const lower = String(str).toLowerCase();
  return lower.replace(/(^| )(\w)/g, function(x) {
    return x.toUpperCase();
  });
};

And this is the output of using the function capitalize("john doe"):

John Doe

Use the ES6 import on node

If you decide to use the ES6 import instead of require, this is a simple presentation on how to achieve that using babel:

– First Install the core dependencies:

npm install babel-register babel-preset-env --save-dev

– Create a starter.js file that contain how your code has to be transpiled plus the file that contains your imports:

require("babel-register")({
  presets: ["env"],
});

// Import the rest of our application.
module.exports = require("./test.js");

And to now your can use import on your test.js file instead of require:

import validator from "validator";

const email = "yep";

if (!validator.isEmail(email)) {
  console.log("the email is not valid");
}

To test the output:

node starters.js