Giter Site home page Giter Site logo

authentication-with-passport's Introduction

Instructions


At the end of this activity you should have successfully implemented Authentication with passport Local Strategy (username and password) by completing the TODOs in server.js and routes/index.js.

To install this starter code on your computer, clone the repository and install dependencies with

npm install
  1. Create a .env file and include the values for port and private key. Check the .env-sample file for reference.

  2. In server.js on line 15 inside the the passport.use() method, configure passport local strategy.

    • instantiate the LocalStrategy object - new LocalStrategy().
    • pass an anonymous function as its only argument - new LocalStrategy(()=>{})
    • pass username, password and cb(callback function) as the arguments in the anonymous function - new LocalStrategy((username, password, cb) =>{})
    • put the database query method implementation (db.findByUsername()) in the block of the localstrategy object.
    • Your code should end up looking like this:
    passport.use(new LocalStrategy((username, password, cb) => {
    db.findByUsername(username, (err, user) => {
        //code to verify user with password here
        // return callback function invoked with a user object - cb(null, user)
    }));
  3. In server.js, with passport.serializeUser() function and using the user.id object, configure passport to serialize users.

  4. In server.js on line 46, with passport.serializeUser() function and using the user.id object, configure passport to serialize users.

  5. In server.js line 82, initialize passport with the passport.initialize() function.

  6. In server.js line 83, include the passport.session() function to restore authentication state from the session.

  7. Complete the logout route in routes/index.js. Use the req.logout() method passport provides. See reference here.

  8. Start the server. Make sure you are in the project's directory in our terminal:

npm start

Open a web browser and navigate to http://localhost:3000/ to see the code in action.

Log in with either of these details:

username: amy, password: strings

OR

username: anne, password: secret

Stretch Challenge

  • Try out using passport-github to do github oauth authentication with passport. See reference here

authentication-with-passport's People

Contributors

kangjoa avatar

Watchers

 avatar

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.