Git Product home page Git Product logo

gatekeeper-middleware's Introduction

Challenge: Gatekeeper middleware

This is a challenge in Thinkful's Web Development Bootcamp.

You need to create a piece of middleware that parses request for a header with the name x-username-and-password. The value for that header should looke like this: [email protected]&pass=password.

The middleware will need to parse the value for this request header, and then attempt to find a user object for the user with that username and password. If the user is located, its object should be added to the request as req.user. If not, req.user will be undefined.

This app has a single endpoint, /api/users/me, which is meant to return the first name, last name, user name, id, and position (aka job title) of an authenticated user. Your middleware will support that endpoint, which looks like this:

app.get("/api/users/me", (req, res) => {
  if (req.user === undefined) {
    return res.status(403).json({message: 'Must supply valid user credentials'});
  }
  const {firstName, lastName, id, userName, position} = req.user;
  return res.json({firstName, lastName, id, userName, position});
});

In order to parse the request header, you will need to use the query-string package, which we've already added as a dependency for this app.

query-string's .parse method allows us to turn a string that looks like this: key1=val1&key2=val2&key3=val3 into:

{
    key1: 'val1',
    key2: 'val2',
    key3: 'val3'
}

So in your middlware, you'll need to use it to get an object with the user and pass from the request header x-username-and-password (if this request header was sent at all!).

gatekeeper-middleware's People

Contributors

katiewest820 avatar

Watchers

James Cloos 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.