Giter Club home page Giter Club logo

express-4.x-local-example's Introduction

This example illustrates how to use Express 4.x and Passport to sign users in with a username and password. Use this example as a starting point for your own web applications.

Quick Start

To get started with this example, clone the repository and install the dependencies.

$ git clone https://github.com/passport/express-4.x-local-example.git
$ cd express-4.x-local-example
$ npm install

Start the server.

$ npm start

Navigate to http://localhost:3000.

Overview

This example illustrates how to use Passport and the passport-local strategy within an Express application to sign users in with a username and password.

The example builds upon the scaffolding created by Express generator, and uses EJS as a view engine and plain CSS for styling. This scaffolding was generated by executing:

$ express --view ejs express-4.x-local-example

The example uses SQLite for storing user accounts. SQLite is a lightweight database that works well for development, including this example.

Added to the scaffolding are files which add authentication to the application.

  • boot/db.js

    This file initializes the database by creating the tables used to store user accounts and credentials.

  • boot/auth.js

    This file initializes Passport. It configures the password strategy with a verify callback. The callback verifies the password by finding the user account in the database. If the account is found, the callback hashes the password entered and compares it to the hashed password stored in the database. If the comparison is equal, the user is authenticated.

    This file also supplies the serialization functions used for session management.

  • routes/auth.js

    This file defines the routes used for authentication. In particular, there are two routes used to authenticate with a username and password:

    • GET /login

      This route renders a page that prompts the user to enter their username and password.

    • POST /login/password

      This route authenticates the user using their username and password.

  • routes/users.js

    This file defines the routes used for registration. In particular, there are two routes used to create an account:

    • GET /users/new

      This route renders a page that prompts the user to enter the information needed to register an acccount. This information consists of their name, preferred username, and password.

    • POST /users

      This route creates a new account using the information entered by the user. The password is first hashed and stored in hashed format.

License

The Unlicense

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.