Up Next: Users and Security

Look back at what Module 3 lets you build, meet the Recipe Box API where you'll show it, and see what Module 4 adds, accounts, signing in and rules about who may do what, so an API knows who is asking.

Apprentice · Node.js · MongoDB · Updated

By

After this lesson you can

  • Describe what accounts, signing in and permissions will let an API do

Key takeaways

  • After Module 3, your APIs keep their data in MongoDB, with every database call in a repository and tests on a throwaway database.
  • The Recipe Box API puts the whole module to work, from a recipe document you design to a search by ingredient.
  • Rep Counter has no idea who sends a request, so anyone who knows a workout's id can delete it.
  • Module 4 adds accounts with hashed passwords, signing in with a token or a session, and rules that answer 401 or 403 when someone may not do something.

At the start of this module, every API you built forgot everything when it stopped. Now Rep Counter's workouts are still there tomorrow, and next month, which is what makes an API something people can rely on. This lesson looks back at what you can do, shows you the project where you'll put it all together, and gives you a first look at Module 4.

What you can do now

After Module 3, you can:

  • shape documents for MongoDB, embedding what's read together and referencing what isn't;
  • save, find, change and delete them with the official driver: filters and operators, sorting and paging, $set and $push, and indexes for searches that stay fast;
  • keep every database call in a repository, which gives back plain objects with their id as text;
  • wire MongoDB into Express: one client for the whole API, createApp(db), and a 404 for any id that isn't a workout's;
  • test the whole API against a real, throwaway MongoDB with mongodb-memory-server and Supertest;
  • read a Mongoose project, and tell it from one that uses the driver.

Show what you can do: the Recipe Box API

Module 3's project is the Recipe Box API: an API that keeps your recipes and finds one by what's in your fridge. Each recipe holds its ingredients and its steps inside it, and part of the job is deciding that shape and saying why in your README. It does the following:

  • creates, reads, updates and deletes recipes, with a 404 for a bad or missing id;
  • searches by words in a recipe's title, with a text index, and by an ingredient, such as every recipe with chickpeas;
  • filters by how long a recipe takes and by its tags, such as vegetarian;
  • sorts and pages the results, so a box of 500 recipes comes back 20 at a time.

Its database code goes in a repository module, and its tests run on mongodb-memory-server with Supertest, the way the Workout Log API's do. Build it from an empty folder, with everything this module taught.

The missing piece: who's asking?

Here's a request any person on the internet could send to Rep Counter, if it were online:

DELETE http://localhost:3100/workouts/6afc949f38ef3f5b865931b3
HTTP/1.1 204 No Content

Dana's workout is gone, and the API never asked who wanted it deleted. It can't: it has no idea who anyone is. Every real app with accounts has to answer two questions about every request. Who are you? That's authentication. And are you allowed to do this? That's authorization.

What Module 4 unlocks

Module 4 gives your APIs people:

  • Passwords, stored safely. An app never keeps a password itself, only a hash of it: a scrambled version that can be checked but not turned back. You'll hash with bcryptjs, then build sign-up and sign-in routes.

  • Staying signed in. After signing in, the app gets a ticket that proves who it is on every request: a session cookie, or a token called a JWT (read "jot"), sent in a header like this:

    GET http://localhost:3100/me
    Authorization: Bearer <the token from sign-in>
    
  • Who may do what. Middleware that answers 401 (we don't know who you are) or 403 (we know who you are, and you may not do that), so a member edits only their own things, and only an organizer creates events.

  • An API that's ready to go online. Which web pages may call it, the security headers every answer should carry, a limit on how fast anyone can guess passwords, and secrets kept out of your code.

The users go in MongoDB, through a repository, and every rule gets a test on a throwaway database: the work of this module carries straight on.

Check yourself

2 questions to try on the lesson page.

So what

Module 3 made your data last. Module 4 decides who gets to see it and change it, which is the difference between a project on your laptop and an app people can trust with their accounts.

Key terms

Related