Coder Social home page Coder Social logo

sessions_controller_readme-houston-web-051319's Introduction

Sessions Controller

Overview

We've covered how cookies can be used to store data in a user's browser.

One of the most common uses of cookies is for login. In this lesson, we'll cover how to use the Rails session to log users in.

Objectives

  1. Describe where session data is stored.
  2. Create a Sessions controller in Rails.
  3. Log a user in with it.

How login works on the web

Nearly every website in the world uses what I am going to call the "wristband" pattern. A lot of nightclubs use this pattern as well.

You arrive at the club. The bouncer checks your ID. They put a wristband on your wrist (or stamp your hand). They let you into the club.

If you leave and come back, the bouncer doesn't look at your ID, they just look for your wristband. If you buy a drink, the bartender doesn't need to see your ID, since your wristband proves you're old enough to buy alcohol.

You arrive at gmail.com. You submit your username and password. Google's servers check to see if your credentials are correct. If they are, Google's servers issue a cookie to your browser. If you visit another page on gmail, or anywhere on google.com for that matter, your browser will show the cookie to the server. The server verifies this cookie, and lets you load your inbox.

How this looks in Rails

Let's look at what the simplest possible login system might look like in Rails.

The flow will look like this:

  • The user GETs /login
  • The user enters their username. There is no password.
  • The user submits the form, POSTing to /login.
  • In the create action of the SessionsController we set a cookie on the user's browser by writing their username into the session hash.
  • Thereafter, the user is logged in. session[:username] will hold their username.

Let's write a SessionsController to handle these routes. This controller has two actions, new and create, which we'll map in routes.rb to get and post on /login. They will look like the below syntax:

get /login”, to: “sessions#new”
post /login”, to: “sessions#create”

Typically, your create method would look up a user in the database, verify their login credentials, and then store the authenticated user's id in the session.

We're not going to do any of that right now. Our sessions controller is just going to trust that you are who you say you are.

class SessionsController < ApplicationController
	def new
		# nothing to do here!
	end

	def create
		session[:username] = params[:username]
		redirect_to '/'
	end
end

There's no way for the server to log you out right now. To log yourself out, you'll have to delete the cookie from your browser.

We'll make a very small login form for new.html.erb,

<form method='post'>
  <input name='username'>
  <input type='submit' value='login'>
</form>

Ordinarily, we would use form_for @user, but in this example, we don't have a user model at all!

When the user submits the form, they'll be logged in!

Logging out

The log out flow is even simpler. We add a SessionsController#destroy method, which will clear the username out of the session.

def destroy
  session.delete :username
end

The most common way to route this action is to post '/logout'. This means that our logout link will actually be a submit button that we style to look like a link.

It's tempting, but don't attach this to a get route. HTTP specifies that get routes don't change anything—logging out is definitely changing something. You don't actually want someone to be able to put a link to http://www.yoursite.com/logout in an email or message board post. It's not a security flaw, but it's pretty annoying to be logged out because of mailing list hijinks.

Conclusion

At its base, login is very simple: the user provides you with credentials in a POST, you verify those credentials and set a token in the session. In this example, our token was literally the username the user typed. In a more complex app, it would most likely be their user id.

Resources

View Sessions Controller on Learn.co and start learning to code for free.

sessions_controller_readme-houston-web-051319's People

Contributors

ahimmelstoss avatar annjohn avatar queerviolet avatar blake41 avatar bhollan avatar gj avatar drakeltheryuujin avatar mendelb avatar

Watchers

 avatar  avatar Victoria Thevenot avatar Belinda Black avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar David John Baker avatar  avatar Matt avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Kaeland Chatman avatar Lisa Jiang avatar Caryn McCarthy avatar Vicki Aubin avatar Maxwell Benton avatar  avatar  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.