Users

Unless you're building an application entirely for your own use - or an application where all data is created anonymously and globally visible - at some point, you will need to think about users. In fact, when designing an application, we spend time upfront thinking about the user's perspective when we write user stories - i.e. how will a user experience and navigate the application.

Users are, in some ways, just like any other resource in an application, with the same seven actions as posts or companies. So let's start there.

Getting set up

Head over to https://github.com/dsgn425-fall2026/auth and choose Use this template, then Create a new repository, in your own account, and choose Private. On your copy, choose Code, then Codespaces, then Create codespace on main. When it's up, start the server in the terminal:

bin/rails server

Open port 3000 in the browser. The home page lists every page in the app; click Tacostagram.

What's new

This application picks up where web-apps-2 left off: Tacostagram, where anyone can post a taco and anyone can read the feed. Companies and contacts are still here too. What's new is users.

Look at the navbar at the top of every page: Sign up and Log in. Open the data page (/data) and you'll see a users table with two rows already in it. Its columns are first_name, last_name, email and password (and password_digest, which we'll get to in a later post). Nothing new here that we haven't already seen.

Next, take a look at the users controller (app/controllers/users_controller.rb). This code should also be familiar. There's a show action that displays one user. And there are new and create actions to display a sign-up form, then receive the data from that form and insert it into the database:

def create
  # start with a new User
  user = User.new
  # assign the form's values to the User's columns
  user["first_name"] = params["first_name"]
  user["last_name"] = params["last_name"]
  user["email"] = params["email"]
  user["password"] = params["password"]
  # save the User row
  user.save
  # log the new user in, then redirect
  session["user_id"] = user["id"]
  flash["notice"] = "Welcome, #{user["first_name"]}."
  redirect_to("/posts")
end

This isn't any different than creating a company or a post. The last three lines are new, and we'll come back to them. However, conceptually this is more than merely the C in CRUD for a user - it represents an almost ubiquitous user story in every application:

  • As a visitor, I want to create my own account with an email and password.

This is a very basic version of the user story, but you'll see some variant of it in almost every application. Sometimes account creation is referred to as registration or sign-up. Sometimes a username is required instead of, or in addition to, an email. Regardless, the expected behavior is the same - a non-user becomes a user by submitting some credentials that can later be used for identification.

Try it

Click Sign up, fill out the form and submit. You land on the posts with a welcome message, and your first name is in the navbar. Click your name: that's your account page, /users/3 or whatever your id is. It shows your name and email.

What isn't displayed?

Passwords

The password is missing. That's very intentional - when deciding what information to display on a resource's show page, you don't have to (and rarely) show everything. You'll need to decide how to handle sensitive information in your applications, but as we'll soon see, passwords should never be exposed.

Not showing it isn't the same as protecting it, though. Open /data again, or in a second terminal run:

bin/rails runner scripts/show_data.rb User

And there, in plain text, you'll see the password you just signed up with, next to the two users who were there before you (ben@example.com with tacos123, and brian@example.com with password). To reiterate, anyone with access to the database - an employee, a backup file, a hacker - has access to all the user passwords! 😱

We'll come back to this in a bit when we start fixing the security vulnerabilities, but for now we'll just put a pin in it. A user can sign up with their email and password. These are our app's authentication credentials - meaning, the data a user will use to identify themselves when
they log in.

Authentication and authorization

When dealing with users in a software application, there are generally two steps to consider: authentication and authorization.

Think of an airport. At security, they check your ID against your face: are you who you say you are? That's authentication. Once you're through, you can walk around the terminal, but your boarding pass only gets you onto one plane, and only some passengers get into the lounge. What you're allowed to do once you're in is authorization.

Or more simply, authentication asks "who is using the application?" and authorization asks "what can this user do within the application?" We'll start with authentication (next post).