-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Place all the behaviors and hooks related to the matching controller here. | ||
// All this logic will automatically be available in application.js. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Place all the styles related to the Sessions controller here. | ||
// They will automatically be included in application.css. | ||
// You can use Sass (SCSS) here: https://sass-lang.com/ | ||
.remember-checkbox{ | ||
margin-left: 10px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
class ApplicationController < ActionController::Base | ||
protect_from_forgery with: :exception | ||
include SessionsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class SessionsController < ApplicationController | ||
def new | ||
end | ||
|
||
def create | ||
user = User.find_by(email: params[:session][:email].downcase) | ||
|
||
if user && user.authenticate(params[:session][:password]) | ||
create_session(user) | ||
flash[:notice] = "Welcome, #{user.name}!" | ||
redirect_to root_path | ||
else | ||
flash.now[:alert] = 'Invalid email/password combination' | ||
render :new | ||
end | ||
end | ||
|
||
def destroy | ||
destroy_session(current_user) | ||
flash[:notice] = "You've been signed out, come back soon!" | ||
redirect_to root_path | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module SessionsHelper | ||
def create_session(user) | ||
session[:user_id] = user.id | ||
end | ||
|
||
def destroy_session(user) | ||
session[:user_id] = nil | ||
end | ||
|
||
def current_user | ||
User.find_by(id: session[:user_id]) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<h2>Sign in</h2> | ||
|
||
<div class="row"> | ||
<div class="col-md-8"> | ||
<%= form_for :session, url: sessions_path do |f| %> | ||
<div class="form-group"> | ||
<%= f.label :email %> | ||
<%= f.email_field :email, autofocus: true, class: 'form-control', placeholder: "Enter email" %> | ||
</div> | ||
<div class="form-group"> | ||
<%= f.label :password %> | ||
<%= f.password_field :password, class: 'form-control', placeholder: "Enter password" %> | ||
</div> | ||
<div class="form-group"> | ||
<%= f.label :remember_me, class: 'checkbox' do %> | ||
Remember Me <%= f.check_box :remember_me, class: "remember-checkbox" %> | ||
<% end %> | ||
<%= f.submit "Sign in", class: 'btn btn-success' %> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe SessionsController, type: :controller do | ||
let(:my_user) { User.create!(name: "Blochead", email: "[email protected]", password: "password") } | ||
|
||
describe "GET new" do | ||
it "returns http success" do | ||
get :new | ||
expect(response).to have_http_status(:success) | ||
end | ||
end | ||
|
||
describe "POST sessions" do | ||
it "returns http success" do | ||
post :create, params: { session: { email: my_user.email } } | ||
expect(response).to have_http_status(:success) | ||
end | ||
|
||
it "initializes a session" do | ||
post :create, params: { session: { email: my_user.email, password: my_user.password } } | ||
expect(session[:user_id]).to eq my_user.id | ||
end | ||
|
||
it "does not add a user id to session due to missing password" do | ||
post :create, params: { session: { email: my_user.email } } | ||
expect(session[:user_id]).to be_nil | ||
end | ||
|
||
it "flashes #error with bad email address" do | ||
post :create, params: { session: { email: "does not exist" } } | ||
expect(flash.now[:alert]).to be_present | ||
end | ||
|
||
it "renders #new with bad email address" do | ||
post :create, params: { session: { email: "does not exist" } } | ||
expect(response).to render_template :new | ||
end | ||
|
||
it "redirects to the root view" do | ||
post :create, params: { session: { email: my_user.email, password: my_user.password } } | ||
expect(response).to redirect_to(root_path) | ||
end | ||
end | ||
describe "DELETE sessions/id" do | ||
it "render the #welcome view" do | ||
delete :destroy, params: { id: my_user.id } | ||
expect(response).to redirect_to root_path | ||
end | ||
|
||
it "deletes the user's session" do | ||
delete :destroy, params: { id: my_user.id } | ||
expect(assigns(:session)).to be_nil | ||
end | ||
|
||
it "flashes #notice" do | ||
delete :destroy, params: { id: my_user.id } | ||
expect(flash[:notice]).to be_present | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'rails_helper' | ||
|
||
# Specs in this file have access to a helper object that includes | ||
# the SessionsHelper. For example: | ||
# | ||
# describe SessionsHelper do | ||
# describe "string concat" do | ||
# it "concats two strings with spaces" do | ||
# expect(helper.concat_strings("this","that")).to eq("this that") | ||
# end | ||
# end | ||
# end | ||
RSpec.describe SessionsHelper, type: :helper do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |