Monday, August 14, 2017

Authentication in React Applications

    πŸ’₯we have built the initial application with presentational and container components for the sign-up form, the login form, and the home component.

    πŸ’₯we need to add new dependencies

   πŸ‘Έ bcrypt is a package with the bcrypt algorithm implementation for hashing passwords
  πŸ‘Έ  jsonwebtoken is an implementation of JSON Web Token standard
  πŸ‘Έ mongoose is a MongoDB ORM library
  πŸ‘Έ passport is a flexible authentication library
  πŸ‘Έ passport-local is a Passport strategy for authenticating with an email and a password


    πŸ’₯We can install these packages by typing:
 
npm install --save bcrypt jsonwebtoken mongoose passport-local

    πŸ’₯The application will have the next directory structure



  πŸ’₯ Index.js file

const express = require('express');
const bodyParser = require('body-parser');
const passport = require('passport');
const config = require('./config');

// connect to the database and load models
require('./server/models').connect(config.dbUri);

const app = express();
// tell the app to look for static files in these directories
app.use(express.static('./server/static/'));
app.use(express.static('./client/dist/'));
// tell the app to parse HTTP body messages
app.use(bodyParser.urlencoded({ extended: false }));
// pass the passport middleware
app.use(passport.initialize());

// load passport strategies
const localSignupStrategy = require('./server/passport/local-signup');
const localLoginStrategy = require('./server/passport/local-login');
passport.use('local-signup', localSignupStrategy);
passport.use('local-login', localLoginStrategy);

// pass the authenticaion checker middleware
const authCheckMiddleware = require('./server/middleware/auth-check');
app.use('/api', authCheckMiddleware);

// routes
const authRoutes = require('./server/routes/auth');
const apiRoutes = require('./server/routes/api');
app.use('/auth', authRoutes);
app.use('/api', apiRoutes);


// start the server
app.listen(3000, () => {
  console.log('Server is running on http://localhost:3000 or http://127.0.0.1:3000');
});

The property jwtSecret of the config object contains a secret phrase our application will use to sign tokens.

config/index.json
 {
"dbUrl": "mongodb://localhost/react_app",
"jwtSecret": "a secret phrase! !"
}

 
 
      πŸ’₯The bcrypt module will generate a hash from a generated earlier salt string and a user’s password. This hash instead of a user’s password will be saved in the collection.

       user.password = hash;

 
     πŸ’₯The basic logic here is to check if a user with a given email exists. If so, we will compare the given password’s hash value with a value saved in the database. If the user exists and the password is correct, we will create a JSON Web Token (JWT).

          The token looks similar to this:


eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.
TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
 

 

Sunday, August 13, 2017

Tic-tac-toe

If you want to do Tic-tac-toe, here are the steps to follow:

     πŸ˜›Make sure you have a recent version of node js installed.
     😝Follow the installation instructions to create a new project.
         
             πŸ‘‰ npm install -g create-react-app
                 create-react-app my-app

                 cd my-app
                 npm start

         

  😝Delete all files in the src/ folder of the new project.
  😝 Add a file named index.css in the src/folder with css code.
      
     πŸ‘‰ body {
  font: 14px "Century Gothic", Futura, sans-serif;
  margin: 20px;
}

ol, ul {
  padding-left: 30px;
}

.board-row:after {
  clear: both;
  content: "";
  display: table;
}

.status {
  margin-bottom: 10px;
}

.square {
  background: #fff;
  border: 1px solid #999;
  float: left;
  font-size: 24px;
  font-weight: bold;
  line-height: 34px;
  height: 34px;
  margin-right: -1px;
  margin-top: -1px;
  padding: 0;
  text-align: center;
  width: 34px;
}

.square:focus {
  outline: none;
}

.kbd-navigation .square:focus {
  background: #ddd;
}

.game {
  display: flex;
  flex-direction: row;
}

.game-info {
  margin-left: 20px;
}







 πŸ˜œAdd a file named index.js in the src/ folder with Js code.

     πŸ‘‰import React from 'react';
      import ReactDOM from 'react-dom';
      import './index.css';

      class Square extends React.Component {
  render() {
    return (
      <button className="square">
        {/* TODO */}
      </button>
    );
  }
}

class Board extends React.Component {
  renderSquare(i) {
    return <Square />;
  }

  render() {
    const status = 'Next player: X';

    return (
      <div>
        <div className="status">{status}</div>
        <div className="board-row">
          {this.renderSquare(0)}
          {this.renderSquare(1)}
          {this.renderSquare(2)}
        </div>
        <div className="board-row">
          {this.renderSquare(3)}
          {this.renderSquare(4)}
          {this.renderSquare(5)}
        </div>
        <div className="board-row">
          {this.renderSquare(6)}
          {this.renderSquare(7)}
          {this.renderSquare(8)}
        </div>
      </div>
    );
  }
}

class Game extends React.Component {
  render() {
    return (
      <div className="game">
        <div className="game-board">
          <Board />
        </div>
        <div className="game-info">
          <div>{/* status */}</div>
          <ol>{/* TODO */}</ol>
        </div>
      </div>
    );
  }
}



ReactDOM.render(
  <Game />,
  document.getElementById('root')
);



😝In particular, we have three components:

  • Square
  • Board
  • Game
The Square component renders a single <button>, the Board renders 9 squares, and the Game component renders a board with some placeholders that we'll fill in later. None of the components are interactive at this point.

     
😝passing some data from the Board component to the Square component.

   πŸ‘‰class Square extends React.Component {
  render() {
    return (
      <button className="square">
        {this.props.value}
      </button>
    );
  }
}

class Board extends React.Component {
  renderSquare(i) {
    return <Square value={i} />;
  }

  render() {
    const status = 'Next player: X';

    return (
      <div>
        <div className="status">{status}</div>
        <div className="board-row">
          {this.renderSquare(0)}
          {this.renderSquare(1)}
          {this.renderSquare(2)}
        </div>
        <div className="board-row">
          {this.renderSquare(3)}
          {this.renderSquare(4)}
          {this.renderSquare(5)}
        </div>
        <div className="board-row">
          {this.renderSquare(6)}
          {this.renderSquare(7)}
          {this.renderSquare(8)}
        </div>
      </div>
    );
  }
}

class Game extends React.Component {
  render() {
    return (
      <div className="game">
        <div className="game-board">
          <Board />
        </div>
        <div className="game-info">
          <div>{/* status */}</div>
          <ol>{/* TODO */}</ol>
        </div>
      </div>
    );
  }
}


ReactDOM.render(
  <Game />,
  document.getElementById('root')
);
    

                 


😝Declaring a Winner

 when a game is won. Add this helper function

πŸ‘‰function calculateWinner(squares) {
  const lines = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
  ];
  for (let i = 0; i < lines.length; i++) {
    const [a, b, c] = lines[i]; 
if (squares[a] && squares[a] === squares[b] && squares[a] === 
squares[c]) {
      return squares[a];
    }
  }
  return null;
}

  😝If you want any changes in this game. you only change in index.js file.

final index.js code.

   πŸ‘‰ import React from 'react';
    import ReactDOM from 'react-dom';
    import './index.css';



function Square(props) {
  return (
    <button className="square" onClick={props.onClick}>
      {props.value}
    </button>
  );
}

class Board extends React.Component {
  renderSquare(i) {
    return (
      <Square
        value={this.props.squares[i]}
        onClick={() => this.props.onClick(i)}
      />
    );
  }

  render() {
    return (
      <div>
        <div className="board-row">
          {this.renderSquare(0)}
          {this.renderSquare(1)}
          {this.renderSquare(2)}

        </div>
        <div className="board-row">
          {this.renderSquare(3)}
          {this.renderSquare(4)}
          {this.renderSquare(5)}

        </div>
        <div className="board-row">
          {this.renderSquare(6)}
          {this.renderSquare(7)}
          {this.renderSquare(8)}

        </div>
      </div>
    );
  }
}

class Game extends React.Component {
  constructor() {
    super();
    this.state = {
      history: [{
        squares: Array(9).fill(null)
      }],
      xIsNext: true
    };
  }

  handleClick(i) {
    const history = this.state.history;
    const current = history[history.length - 1];
    const squares = current.squares.slice();
    if (calculateWinner(squares) || squares[i]) {
      return;
    }
    squares[i] = this.state.xIsNext ? 'X' : 'O';
    this.setState({
      history: history.concat([{
        squares: squares
      }]),
      xIsNext: !this.state.xIsNext,
    });
  }

  render() {
    const history = this.state.history;
    const current = history[history.length - 1];
    const winner = calculateWinner(current.squares);

    const moves = history.map((step, move) => {
      const desc = move ?
        'Move #' + move :
        'Game start';
      return (
        <li>
          <a href="#" onClick={() => this.jumpTo(move)}>{desc}</a>
        </li>
      );
    });

    let status;
    if (winner) {
      status = 'Winner: ' + winner;
    } else {
      status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
    }

    return (
      <div className="game">
        <div className="game-board">
          <Board
            squares={current.squares}
            onClick={(i) => this.handleClick(i)}
          />
        </div>
        <div className="game-info">
          <div>{status}</div>
          <ol>{moves}</ol>
        </div>
      </div>
    );
  }
}



ReactDOM.render(
  <Game />,
  document.getElementById('root')
);

function calculateWinner(squares) {
  const lines = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
  ];
  for (let i = 0; i < lines.length; i++) {
    const [a, b, c] = lines[i];
    if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
      return squares[a];
    }
  }
  return null;
}

If you intrested play this tic-tac-toe game,you will  go to this URL 

 https://ukifunwork3byabi.herokuapp.com/ 

Sunday, July 30, 2017

Heroku


Heroku is a cloud platform as a service supporting several programming languages that is used as a web application deployment model.


πŸ’₯How to use Heroku 

  1. Download the Heroku Toolbelt.
  2. Login: heroku login.
  3. Add your public key: heroku keys:add.
  4. Pull down your current application heroku git:clone -a app-name.
  5. Make your improvements.
  6. Git add and commit your changes.
  7. Push back to heroku: git push heroku master.


 πŸ’₯Advantage in Heroku

  • Heroku is easy to get started.
  • Heroku is the cheapest option for a low traffic site
  • we don't have to add our credit card for payment at early stage.
  • They offer no of Dyno for upgrade and downgrade app instance
  • Database integration is pretty simple with PostgreSQL

 πŸ’₯Disadvantage in Heroku
  • we have to pay very high, once we decide to handle more traffic.
  • we have to manually scale our application on hight traffic.
  • we can't login to your server via SSH(Secure Remote Login & File Transfer)

πŸ’₯What Is SSH Protocol Used for?

The protocol is used in corporate networks for:
  • providing secure access for users and automated processes
  • interactive and automated file transfers
  • issuing remote commands
  • managing network infrastructure and other mission-critical system components.

FriendsShip ...

   
 πŸ˜πŸ˜πŸ˜πŸ˜I'm the luckiest person in the world because I have lots of friends. That's why I am happy. 😍😍😍😍

 

 

Friday, July 28, 2017

Postman

A powerful GUI platform to make your API development faster & easier, from building API requests through testing, documentation and sharing. We recommend the free Postman App for Mac, Windows, Linux or Chrome. Developer Teams.

πŸ’₯ How to work postman
  1. Launch the Postman app.
  2. Go to the Authorization section: Postman Authorization Section.
  3. For the Type, select OAuth 2.0: Authorization Type.
  4. Click Get New Access Token.
  5. Click Request Token.
  6. Click on the name of the token to see you now have access to the new token: Access Token.

 πŸ’₯What are the benefits to use postman

  • Easily create test suites

    Postman allows we create collections of integration tests to ensure our API is working as expected. Tests are run in a specific order with each test being executed after the last is finished 

    ☺store information for running tests in different environments 


      Postman allows we to store specific information about different environments and automatically insert the correct environment configuration into our test. This could be a base URL, query parameters, request headers, and even body data for an HTTP post.




    ☺ Store data for use in other tests
          Postman also allows we to store data from previous tests into global variables. These variables can be used exactly like environment variables.we can store the response  and use that as part of a request header, post body, or URL for the subsequent API calls.

     ☺Easily move tests and environments to code repositories

            


UI & UX


      πŸ’₯ While User Experience is a conglomeration of tasks focused on optimization of a product for effective and enjoyable use; User Interface Design is its compliment, the look and feel, the presentation and interactivity of a product. But like UX, it is easily and often confused by the industries that employ UI Designers.

   πŸ’₯Unlike UX designers who are concerned with the overall feel of the product, user interface designers are particular about how the product is laid out. They are in charge of designing each screen or page with which a user interacts and ensuring that the UI visually communicates the path that a UX designer has laid out.

Free code Camp and Codecadamy & hackerrank



HackerRank is a technology company that focuses on competitive programming challenges for both consumers and businesses, where developers compete by trying to program according to provided specifications.HackerRank's programming challenges can be solved in a variety of programming languages (including Java, C++, PHP,Python,SQL) and span multiple computer science domains.
On the consumer side, when a programmer submits a solution to a programming challenge, their submission is scored on the accuracy of their output and the execution time of their solution.


    
       Codecademy is an online interactive platform that offers free coding classes in 12 different Progamming languages including Python,Java,PHP (jQuery,AngularsJS,React.js),Ruby,SQl,Sass,as well as Markup languages HTML and CSS.


FREE CODE CAMP


     FreeCodeCamp is a nonprofit organization that consists of an interactive learning web platform, an online community forum, chat rooms,medium publications, and local organizations that intend to make learning web development accessible to anyone. Beginning with tutorials that introduce students to HTML, CSS, and JavaScript, students progress to project assignments that they must complete either alone or in pairs. Upon completion of all project tasks, students are partnered with other nonprofits to build web applications, giving the students practical development experience.