vubao2303 / analyze-code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

analyze-code

This application provides a sign up form, a sign in form using passport and bcrypt to track user uniqueness and protect user input. Using MVC ( Model–view–controller), a software design pattern commonly, to develop user interfaces that divides the related program logic into three interconnected elements, in this case, Model-Public-Route.

Google Docs

Google Docs

Page Structure

Page Structure

Table of Contents

Tittle

Google Docs

Page Structure

Table of Contents

Description of Page Building

Code Snippet

Technologies Used

Author

License

Description of Page Building

  • In server.js file

    • require all the nmp packages
    • Setting up port and requiring models for syncing
    • Creating express app and configuring middleware needed for authentication.
    • requiring routes from the routes folder
    • Syncing our database and logging a message to the user upon success
    • listen to port to kicks up the whole thing and starts the server
  • In js folder

    • get document by id and class
    • validate user data
    • post to our "api/login" route
    • html route serves up html interface
  • In config folder

    • Use Passport to authenticate requests.
    • Appply Passport-local strategy for authenticating with a username and password.
    • serialize and deserialize the user input

Code Snippet

Install npm package

Required variables

var express = require("express");
var session = require("express-session");
var passport = require("./config/passport");
var PORT = process.env.PORT || 8080;
var db = require("./models");
var app = express();

Set routes to handle when user "visit" the page

app.post("/api/login", passport.authenticate("local"), function(req, res) {
   res.json(req.user);
 });
app.get("/logout", function(req, res) {
   req.logout();
   res.redirect("/");
 });

bcrypt salt and hash of passord for safety

 User.addHook("beforeCreate", function(user) {
    user.password = bcrypt.hashSync(user.password, bcrypt.genSaltSync(10), null);
  });
  return User;

Passport to authenticate requests.

passport.use(new LocalStrategy(
  {usernameField: "email"},
  function(email, password, done) {
      db.User.findOne({where: {email: email}
    }).then(function(dbUser) {
      if (!dbUser) {
        return done(null, false, {
          message: "Incorrect email."
        });}
      else if (!dbUser.validPassword(password)) {
        return done(null, false, {
          message: "Incorrect password."
        });}
      return done(null, dbUser);
    });}
));

Serialize takes your basic data structure and flatten it out into a string, by reading top to bottom. Now you have one big ass string. want to duplicate or extract, deserialize take that string and construct it again, make back into an object

passport.serializeUser(function(user, cb) {
 cb(null, user);
});
passport.deserializeUser(function(obj, cb) {
 cb(null, obj);}); 
module.exports = passport;

Technologies Used

  • Node - an open-source, cross-platform, back-end JavaScript runtime environment that executes JavaScript code outside a web browser.
  • Git - version control system to track changes to source code
  • GitHub - hosts repository that can be deployed to GitHub Pages
  • Express - a Node js web application server framework, which is specifically designed for building single-page, multi-page, and hybrid web applications
  • Bcryptjs - is a secured way to store passwords in database
  • Express-session - Create a session middleware with the given option
  • Mysql12 - Create a session middleware with the given option
  • Passport - authenticate requests through an extensible set of plugins known as strategies.
  • Passport-local - a strategy authenticate users using a username and password
  • Sequelize - Create table for database

Author

  • B Tram Vu

License

© 2021 Trilogy Education Services, a 2U, Inc. brand. All Rights Reserved.

About


Languages

Language:JavaScript 79.4%Language:HTML 20.4%Language:CSS 0.2%