Giter VIP home page Giter VIP logo

login_required_lab's Introduction

Login Required Lab

Objectives

We're going to make a Rails app that requires you to be logged in to see one of its pages.

Introduction

Our app has three pages:

  1. A login page, where the user enters their username. No passwords; we'll just trust them. After they're logged in, users are taken to...
  2. A welcome page, which says, "Hi, #{username}", and has a link to the secret page, which is...
  3. A page with a secret on it, which users must be logged in to see.

Instructions

  1. Build out the SessionsController with new, create, and destroy actions. You can use resources or write custom routes to handle these controller actions.
  2. Write a current_user method in the ApplicationController.
  3. Write a SecretsController with a show action. This controller should also use a before_action to prevent any route from being accessed without logging in. You can use resources or write a custom route to handle this controller action.

Use the specs as your guide, but we'd like the following behavior. We should be able to:

  • If the user is not logged in, visiting the root of the app should redirect them to a login page.
    • The login page should be handled via the SessionsController#new action.
  • If a user fails to enter their name on the login page, they should be redirected there until they successfully do so.
    • Submitting the login form should be handled via the SessionsController#create action.
  • Once logged in, a user be able see the welcome page at the root route. This page should greet the user and link them to the secret page.
  • The content and URL of the secret page are up to you. However, if we visit that URL without logging in, we should be redirected to the login page. Under no circumstances should we allow people who are not logged in to see the secrets.

Happy coding!

login_required_lab's People

Contributors

ahimmelstoss avatar annjohn avatar aviflombaum avatar blake41 avatar danielseehausen avatar genericlady avatar ihollander avatar lkwlala avatar maxwellbenton avatar pletcher avatar queerviolet avatar victhevenot avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

login_required_lab's Issues

typo

'following behavoir'

Rspec test syntax is deprecated

The rspec tests for this lab are are written in a style that is deprecated. The results are hard to read because of the syntax warnings. I rewrote the tests in the new syntax below. In short, get :show, { id: 1 }, nil is no longer good syntax; nil can be omitted, and params are to be explicitly notated, i.e. : get :show, params { id: 1 }

/spec/controllers/secrets_controller_spec.rb


RSpec.describe SecretsController do
  describe 'get show' do
    it "should redirect to login if you're not logged in" do
      get :show
      expect(response).to redirect_to controller: 'sessions', action: 'new'
    end

    it "should show you the secret if you're logged in" do
      # note, in this test we're sending a get request to the secretscontroller with no params.
      # if your route expects a param of id you'll get this error No route matches {:action=>"show", :controller=>"secrets"}
      get :show, session: {name: 'Maya Angelou'}
      assert_response 200
    end
  end
end

/spec/controllers/session_controller_spec.rb


RSpec.describe SessionsController do
  describe 'create' do
    it 'redirects to login page if :name is nil' do
      post :create, params: { name: nil }
      expect(response).to redirect_to controller: 'sessions', action: 'new'
    end

    it 'redirects to login page if :name is empty' do
      post :create, params: { name: '' }
      expect(response).to redirect_to controller: 'sessions', action: 'new'
    end

    it 'sets session[:name] if :name was given' do
      me = 'Werner Brandes'
      post :create, params: { name: me }
      expect(@request.session[:name]).to eq me
    end
  end

  describe 'destroy' do
    it 'leaves session[:name] nil if it was not set' do
      post :destroy
      expect(@request.session[:name]).to be nil
    end

    it 'clears session[:name] if it was set' do
      post :create, params: { name: 'Trinity' }
      expect(@request.session[:name]).to_not be nil
      post :destroy
      expect(@request.session[:name]).to be nil
    end
  end
end

Broken application controller tests

The tests would fail and keep saying the current_user method was undefined for the application controller, even though I had written it in. I did some research and prepended the current_user method with the subject keyword in the spec file and that seemed to get it to register. I'm not fully sure if that's the right solution or how it works, but without it, it didn't seem current_user knew what class to be invoked upon.

tests aren't strict enough

When typing learn on the vanilla lab the green light on Learn comes on for passing local tests with no code written.

Instructions are pretty thin...

Just an observation... if I hadn't peeked at the solution, I would have had no idea how to structure the pages for this lab.

hello.html.erb in views/application?

INSTRUCTIONS
Build out the SessionsController. - including what actions??
Write a current_user method. - where?
Write a SecretsController using a before_action to prevent any route from being accessed without logging in. - okay this is more like it! :)

I LOVE nearly every lab I come across, so I feel the need to share my concerns when something seems really slapped together. I get that we need to take the training wheels off but a little more instruction on what's expected doesn't seem so out of line.

In the real world I assume we're either getting detailed instruction from a boss or have free reign to build something with prewritten tests. Just my two cents :) Keep up the great work Team Learn!

Learn IDE is getting stuck on Bundler

When trying to open the lesson through Learn IDE, I am getting this error:
Bundler could not find compatible versions for gem "bundler":
In Gemfile:
rails (> 4.2.0) was resolved to 4.2.11, which depends on
bundler (>= 1.3.0, < 2.0)
Current Bundler version:
bundler (2.0.0)
This Gemfile requires a different version of Bundler.
Perhaps you need to update Bundler by running gem install bundler?
Could not find gem 'bundler (>= 1.3.0, < 2.0)', which is required by gem 'rails
(
> 4.2.0)', in any of the sources.

Have tried running 'gem install bundler', but it did not help.

Solution branch?

Hi! Will someone please post a solution branch for this lab? Thank you!

No solution link?

When solving this lab, there was no solution link provided after passing the tests.

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.