Tuesday, February 28, 2017

#1.5 Node.js Basics - authentication + JWT

This part is focused on authentication and json web token (jwt):

#1.4 Node.js Basics - user model

Create a model for user, together with some functions to interact with the database:

1. Create /model/user.js as below:

const mongoose = require('mongoose');
const bcrypt   = require('bcryptjs');
const config   = require('../config/database');

// User Schema
const userSchema = mongoose.Schema({
    name:     { type: String },
    email:    { type: String, required: true },
    username: { type: String, required: true },
    password: { type: String, required: true }
});

const user = module.exports = mongoose.model('User', userSchema);

module.exports.getUserById = function (id, callback) {
    user.findById(id, callback)
}

module.exports.getUserByUsername = function (username, callback) {
    const query = { username: username };
    user.findOne(query, callback);
}

module.exports.addUser= function (newUser, callback) {
    bcrypt.genSalt(10, (err, salt) => {
        bcrypt.hash(newUser.password, salt, (err, hash) => {
            if(err) throw err;
            newUser.password = hash;
            newUser.save(callback);
        })
    })
}

Described above is the DB schema (userSchema), two functions to query user data from DB and one to add user data to DB.

#1.3 Node.js Basics - using npm and angular cli

Prerequisites: node.js, npm

1. Create a directory for the project e.g.:
$ mkdir /Projects/meanapp
$ cd  /Projects/meanapp

Monday, February 27, 2017

#1.2 Angular2 Basics - start cloning angular quickstart

Prerequisites: node.js, npm

In the arch linux shell:

1. create a directory for the project e.g. /Projects

#1.1 MongoDB basics

1. Installing on archLinux: Install mongodb from official repositories:

# pacman -S mongodb