Skip to content

Commit

Permalink
Merge pull request #14 from lao9/user-menu
Browse files Browse the repository at this point in the history
User Menu and Authentication
  • Loading branch information
lao9 authored Aug 1, 2017
2 parents f638b01 + ab23c31 commit b3215ed
Show file tree
Hide file tree
Showing 20 changed files with 477 additions and 33 deletions.
14 changes: 7 additions & 7 deletions app/assets/javascripts/geolocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function geolocator() {

function addWaitingMessage() {
$(".container:nth-child(2)").prepend(
`<div class='alert alert-warning'>
`<div class='alert waiting alert-warning'>
<button type='button' class='close' data-dismiss='alert'>x</button>
Waiting to obtain your location...
</div>
Expand All @@ -22,17 +22,17 @@ function grabUserLocation() {
}

function failureMessage() {
$(".alert").removeClass("alert-warning")
$(".alert").addClass("alert-danger")
$(".waiting").removeClass("alert-warning")
$(".waiting").addClass("alert-danger")
var errorMessage = "Geolocation is not supported by this browser."
$(".alert").html(`<button type="button" class="close" data-dismiss="alert">x</button>\n${errorMessage}`)
$(".waiting").html(`<button type="button" class="close" data-dismiss="alert">x</button>\n${errorMessage}`)
BrackMap.drawMap({latitude: 39.749598000000006, longitude: -105.0004297})
}

function showPosition(position) {
$(".alert").removeClass("alert-warning")
$(".alert").addClass("alert-success")
$(".alert").html('<button type="button" class="close" data-dismiss="alert">x</button>\nSuccess!')
$(".waiting").removeClass("alert-warning")
$(".waiting").addClass("alert-success")
$(".waiting").html('<button type="button" class="close" data-dismiss="alert">x</button>\nSuccess!')
BrackMap.drawMap(position.coords)
}

Expand Down
7 changes: 5 additions & 2 deletions app/assets/javascripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ var brackMarkers = [];
var currentCenter = [];

$(document).ready(function(){
geolocator()
expandRackOptions()
var path = window.location.pathname
if (path === "/" || path === "/bracks") {
geolocator()
expandRackOptions()
}
})
14 changes: 11 additions & 3 deletions app/assets/javascripts/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,17 @@ function searchBoxListener(searchBox) {
}

function setMapZoom(distance) {
var dynaZoom = Math.round((-7 * distance) + 19)
if (dynaZoom < 10) { dynaZoom = 10 }
map.setZoom(dynaZoom)
if (distance < 0.2) {
map.setZoom(18)
} else if (distance < 0.5) {
map.setZoom(17)
} else if (distance < 1) {
map.setZoom(15)
} else if (distance < 2) {
map.setZoom(12)
} else {
map.setZoom(10)
}
}

function addMarkers(latLng) {
Expand Down
14 changes: 14 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

helper_method :current_user

def authenticate_user
unless current_user
flash[:alert] = "Please login to continue!"
redirect_to login_path
end
end

def current_user
@user ||= User.find(session[:user_id]) if session[:user_id]
end

end
24 changes: 24 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class SessionsController < ApplicationController

def new
end

def create
user = User.find_by(email: params[:session][:email])
if user && user.authenticate(params[:session][:password])
session[:user_id] = user.id
flash[:notice] = "Welcome!"
redirect_to bracks_path
else
flash[:error] = "Invalid Username or Password!"
redirect_to login_path
end
end

def destroy
session.clear
flash[:notice] = "Goodbye!"
redirect_to bracks_path
end

end
31 changes: 31 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class UsersController < ApplicationController

def new
@user = User.new
end

def create
@user = User.new(user_params)
error_messages = @user.check_user_errors(check_user_params)
if error_messages.empty?
@user.save
session[:user_id] = @user.id
flash[:notice] = "Welcome!"
redirect_to bracks_path
else
flash[:error] = error_messages
redirect_to new_user_path
end
end

private

def check_user_params
user_params.merge!({pass_confirm: params[:user][:password_confirmation]})
end

def user_params
params.require(:user).permit(:email, :password)
end

end
10 changes: 10 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@ class User < ApplicationRecord
has_secure_password

validates :email, presence: true, uniqueness: true

def check_user_errors(params)
messages = []
messages << "Email account already exists!" if User.find_by(email: params[:email])
messages << "Please fill out email field." if params[:email].empty?
messages << "Please fill out password field." if params[:password].empty?
messages << "Please fill out password confirmation field." if params[:pass_confirm].empty?
messages << "Password confirmation does not match." if params[:password] != params[:pass_confirm]
return messages.join("\n")
end
end
14 changes: 12 additions & 2 deletions app/views/layouts/_navbar.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
<div class="navbar navbar-inverse bg-inverse navbar-static-top">
<div class="container">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".bs-example-navbar-collapse-9">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Brackr</a>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li>Menu</li>
<% unless current_user %>
<li><%= link_to "Log In", login_path %></li>
<li><%= link_to "Sign Up", new_user_path %></li>
<% end %>
<li><%= link_to "Bike Rack Map", bracks_path, method: :get %></li>
<li><a href="#">Add New Bike</a></li>
<% if current_user %>
<li><%= link_to logout_path, method: :delete do %>
Log Out</a>
</li><% end %>
<% end %>
</ul>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
Expand All @@ -27,7 +28,7 @@
<%= flash[:alert] %>
</div>
<% end %>

<%= yield %>
</div>
</body>
Expand Down
20 changes: 20 additions & 0 deletions app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<h1>Login</h1>

<div class="well">
<%= form_for :session, url: login_path do |f| %>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control' %>
</div>

<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
</div>

<div class="form-group">
<%= f.submit "Log In", class: 'btn btn-primary' %>
</div>
<% end %>
<h5>Don't have an account? <%= link_to "Sign up by clicking here!", new_user_path %></h5>
</div>
41 changes: 41 additions & 0 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<h1>Sign Up</h1>

<div class="well">
<%= form_for @user do |f| %>

<div class="form-group">
<%= f.label :first_name %>
<%= f.text_field :first_name, class: 'form-control' %>
</div>

<div class="form-group">
<%= f.label :last_name %>
<%= f.text_field :last_name, class: 'form-control' %>
</div>

<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control' %>
</div>

<div class="form-group">
<%= f.label :organization %>
<%= f.text_field :organization, class: 'form-control' %>
</div>

<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
</div>

<div class="form-group">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
</div>

<div class="form-group">
<%= f.submit "Sign Up", class: 'btn btn-primary' %>
</div>
<% end %>
<h5>Already have an account? <%= link_to "Log in by clicking here!", login_path %></h5>
</div>
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
Rails.application.routes.draw do
root 'bracks#index'

get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'

resources :bracks, only: [:index]
resources :users, only: [:new, :create]

namespace :api do
namespace :v1 do
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"files":{"b-icon-7f9780781c9d08b1b924f1036e54c13fb38c190fbf27dd58a55e502d699481be.png":{"logical_path":"b-icon.png","mtime":"2017-07-31T10:36:11-06:00","size":4314,"digest":"7f9780781c9d08b1b924f1036e54c13fb38c190fbf27dd58a55e502d699481be","integrity":"sha256-f5eAeBydCLG5JPEDblTBP7OMGQ+/J91YpV5QLWmUgb4="},"spinner-47c716a105894b5888f62cfa3108a66830f958e41247c4396d70a57821464ffa.gif":{"logical_path":"spinner.gif","mtime":"2017-07-26T21:37:28-06:00","size":265881,"digest":"47c716a105894b5888f62cfa3108a66830f958e41247c4396d70a57821464ffa","integrity":"sha256-R8cWoQWJS1iI9iz6MQimaDD5WOQSR8Q5bXCleCFGT/o="},"application-cebee81d3f4145cd6b4ebaf87d6053343053e68cec726ac7f1429d61ea40ad0b.js":{"logical_path":"application.js","mtime":"2017-08-01T09:00:27-06:00","size":447267,"digest":"cebee81d3f4145cd6b4ebaf87d6053343053e68cec726ac7f1429d61ea40ad0b","integrity":"sha256-zr7oHT9BRc1rTrr4fWBTNDBT5ozscmrH8UKdYepArQs="},"application-02c40a133c96dd469be010cef633fe8413d58baaf4d673acd91f08ec92241677.css":{"logical_path":"application.css","mtime":"2017-08-01T08:59:11-06:00","size":121316,"digest":"02c40a133c96dd469be010cef633fe8413d58baaf4d673acd91f08ec92241677","integrity":"sha256-AsQKEzyW3Uab4BDO9jP+hBPVi6r01nOs2R8I7JIkFnc="},"glyphicons-halflings-regular-13634da87d9e23f8c3ed9108ce1724d183a39ad072e73e1b3d8cbf646d2d0407.eot":{"logical_path":"glyphicons-halflings-regular.eot","mtime":"2017-05-09T13:33:22-06:00","size":20127,"digest":"13634da87d9e23f8c3ed9108ce1724d183a39ad072e73e1b3d8cbf646d2d0407","integrity":"sha256-E2NNqH2eI/jD7ZEIzhck0YOjmtBy5z4bPYy/ZG0tBAc="},"glyphicons-halflings-regular-a26394f7ede100ca118eff2eda08596275a9839b959c226e15439557a5a80742.woff":{"logical_path":"glyphicons-halflings-regular.woff","mtime":"2017-05-09T13:33:22-06:00","size":23424,"digest":"a26394f7ede100ca118eff2eda08596275a9839b959c226e15439557a5a80742","integrity":"sha256-omOU9+3hAMoRjv8u2ghZYnWpg5uVnCJuFUOVV6WoB0I="},"glyphicons-halflings-regular-e395044093757d82afcb138957d06a1ea9361bdcf0b442d06a18a8051af57456.ttf":{"logical_path":"glyphicons-halflings-regular.ttf","mtime":"2017-05-09T13:33:22-06:00","size":45404,"digest":"e395044093757d82afcb138957d06a1ea9361bdcf0b442d06a18a8051af57456","integrity":"sha256-45UEQJN1fYKvyxOJV9BqHqk2G9zwtELQahioBRr1dFY="},"glyphicons-halflings-regular-42f60659d265c1a3c30f9fa42abcbb56bd4a53af4d83d316d6dd7a36903c43e5.svg":{"logical_path":"glyphicons-halflings-regular.svg","mtime":"2017-05-09T13:33:22-06:00","size":108738,"digest":"42f60659d265c1a3c30f9fa42abcbb56bd4a53af4d83d316d6dd7a36903c43e5","integrity":"sha256-QvYGWdJlwaPDD5+kKry7Vr1KU69Ng9MW1t16NpA8Q+U="}},"assets":{"b-icon.png":"b-icon-7f9780781c9d08b1b924f1036e54c13fb38c190fbf27dd58a55e502d699481be.png","spinner.gif":"spinner-47c716a105894b5888f62cfa3108a66830f958e41247c4396d70a57821464ffa.gif","application.js":"application-cebee81d3f4145cd6b4ebaf87d6053343053e68cec726ac7f1429d61ea40ad0b.js","application.css":"application-02c40a133c96dd469be010cef633fe8413d58baaf4d673acd91f08ec92241677.css","glyphicons-halflings-regular.eot":"glyphicons-halflings-regular-13634da87d9e23f8c3ed9108ce1724d183a39ad072e73e1b3d8cbf646d2d0407.eot","glyphicons-halflings-regular.woff":"glyphicons-halflings-regular-a26394f7ede100ca118eff2eda08596275a9839b959c226e15439557a5a80742.woff","glyphicons-halflings-regular.ttf":"glyphicons-halflings-regular-e395044093757d82afcb138957d06a1ea9361bdcf0b442d06a18a8051af57456.ttf","glyphicons-halflings-regular.svg":"glyphicons-halflings-regular-42f60659d265c1a3c30f9fa42abcbb56bd4a53af4d83d316d6dd7a36903c43e5.svg"}}
{"files":{"b-icon-7f9780781c9d08b1b924f1036e54c13fb38c190fbf27dd58a55e502d699481be.png":{"logical_path":"b-icon.png","mtime":"2017-07-31T10:36:11-06:00","size":4314,"digest":"7f9780781c9d08b1b924f1036e54c13fb38c190fbf27dd58a55e502d699481be","integrity":"sha256-f5eAeBydCLG5JPEDblTBP7OMGQ+/J91YpV5QLWmUgb4="},"spinner-47c716a105894b5888f62cfa3108a66830f958e41247c4396d70a57821464ffa.gif":{"logical_path":"spinner.gif","mtime":"2017-07-26T21:37:28-06:00","size":265881,"digest":"47c716a105894b5888f62cfa3108a66830f958e41247c4396d70a57821464ffa","integrity":"sha256-R8cWoQWJS1iI9iz6MQimaDD5WOQSR8Q5bXCleCFGT/o="},"application-de129d5c3cf0bdda56c0144f00d4cbfe4870cedd9237b588cfd06c45cea319ab.js":{"logical_path":"application.js","mtime":"2017-08-01T12:01:25-06:00","size":447492,"digest":"de129d5c3cf0bdda56c0144f00d4cbfe4870cedd9237b588cfd06c45cea319ab","integrity":"sha256-3hKdXDzwvdpWwBRPANTL/khwzt2SN7WIz9BsRc6jGas="},"application-02c40a133c96dd469be010cef633fe8413d58baaf4d673acd91f08ec92241677.css":{"logical_path":"application.css","mtime":"2017-08-01T08:59:11-06:00","size":121316,"digest":"02c40a133c96dd469be010cef633fe8413d58baaf4d673acd91f08ec92241677","integrity":"sha256-AsQKEzyW3Uab4BDO9jP+hBPVi6r01nOs2R8I7JIkFnc="},"glyphicons-halflings-regular-13634da87d9e23f8c3ed9108ce1724d183a39ad072e73e1b3d8cbf646d2d0407.eot":{"logical_path":"glyphicons-halflings-regular.eot","mtime":"2017-05-09T13:33:22-06:00","size":20127,"digest":"13634da87d9e23f8c3ed9108ce1724d183a39ad072e73e1b3d8cbf646d2d0407","integrity":"sha256-E2NNqH2eI/jD7ZEIzhck0YOjmtBy5z4bPYy/ZG0tBAc="},"glyphicons-halflings-regular-a26394f7ede100ca118eff2eda08596275a9839b959c226e15439557a5a80742.woff":{"logical_path":"glyphicons-halflings-regular.woff","mtime":"2017-05-09T13:33:22-06:00","size":23424,"digest":"a26394f7ede100ca118eff2eda08596275a9839b959c226e15439557a5a80742","integrity":"sha256-omOU9+3hAMoRjv8u2ghZYnWpg5uVnCJuFUOVV6WoB0I="},"glyphicons-halflings-regular-e395044093757d82afcb138957d06a1ea9361bdcf0b442d06a18a8051af57456.ttf":{"logical_path":"glyphicons-halflings-regular.ttf","mtime":"2017-05-09T13:33:22-06:00","size":45404,"digest":"e395044093757d82afcb138957d06a1ea9361bdcf0b442d06a18a8051af57456","integrity":"sha256-45UEQJN1fYKvyxOJV9BqHqk2G9zwtELQahioBRr1dFY="},"glyphicons-halflings-regular-42f60659d265c1a3c30f9fa42abcbb56bd4a53af4d83d316d6dd7a36903c43e5.svg":{"logical_path":"glyphicons-halflings-regular.svg","mtime":"2017-05-09T13:33:22-06:00","size":108738,"digest":"42f60659d265c1a3c30f9fa42abcbb56bd4a53af4d83d316d6dd7a36903c43e5","integrity":"sha256-QvYGWdJlwaPDD5+kKry7Vr1KU69Ng9MW1t16NpA8Q+U="}},"assets":{"b-icon.png":"b-icon-7f9780781c9d08b1b924f1036e54c13fb38c190fbf27dd58a55e502d699481be.png","spinner.gif":"spinner-47c716a105894b5888f62cfa3108a66830f958e41247c4396d70a57821464ffa.gif","application.js":"application-de129d5c3cf0bdda56c0144f00d4cbfe4870cedd9237b588cfd06c45cea319ab.js","application.css":"application-02c40a133c96dd469be010cef633fe8413d58baaf4d673acd91f08ec92241677.css","glyphicons-halflings-regular.eot":"glyphicons-halflings-regular-13634da87d9e23f8c3ed9108ce1724d183a39ad072e73e1b3d8cbf646d2d0407.eot","glyphicons-halflings-regular.woff":"glyphicons-halflings-regular-a26394f7ede100ca118eff2eda08596275a9839b959c226e15439557a5a80742.woff","glyphicons-halflings-regular.ttf":"glyphicons-halflings-regular-e395044093757d82afcb138957d06a1ea9361bdcf0b442d06a18a8051af57456.ttf","glyphicons-halflings-regular.svg":"glyphicons-halflings-regular-42f60659d265c1a3c30f9fa42abcbb56bd4a53af4d83d316d6dd7a36903c43e5.svg"}}
Original file line number Diff line number Diff line change
Expand Up @@ -14560,7 +14560,7 @@ function geolocator() {

function addWaitingMessage() {
$(".container:nth-child(2)").prepend(
`<div class='alert alert-warning'>
`<div class='alert waiting alert-warning'>
<button type='button' class='close' data-dismiss='alert'>x</button>
Waiting to obtain your location...
</div>
Expand All @@ -14576,17 +14576,17 @@ function grabUserLocation() {
}

function failureMessage() {
$(".alert").removeClass("alert-warning")
$(".alert").addClass("alert-danger")
$(".waiting").removeClass("alert-warning")
$(".waiting").addClass("alert-danger")
var errorMessage = "Geolocation is not supported by this browser."
$(".alert").html(`<button type="button" class="close" data-dismiss="alert">x</button>\n${errorMessage}`)
$(".waiting").html(`<button type="button" class="close" data-dismiss="alert">x</button>\n${errorMessage}`)
BrackMap.drawMap({latitude: 39.749598000000006, longitude: -105.0004297})
}

function showPosition(position) {
$(".alert").removeClass("alert-warning")
$(".alert").addClass("alert-success")
$(".alert").html('<button type="button" class="close" data-dismiss="alert">x</button>\nSuccess!')
$(".waiting").removeClass("alert-warning")
$(".waiting").addClass("alert-success")
$(".waiting").html('<button type="button" class="close" data-dismiss="alert">x</button>\nSuccess!')
BrackMap.drawMap(position.coords)
}

Expand All @@ -14603,8 +14603,11 @@ var brackMarkers = [];
var currentCenter = [];

$(document).ready(function(){
geolocator()
expandRackOptions()
var path = window.location.pathname
if (path === "/" || path === "/bracks") {
geolocator()
expandRackOptions()
}
})
;
function BrackMap(coords) {
Expand Down Expand Up @@ -14681,9 +14684,17 @@ function searchBoxListener(searchBox) {
}

function setMapZoom(distance) {
var dynaZoom = Math.round((-7 * distance) + 19)
if (dynaZoom < 10) { dynaZoom = 10 }
map.setZoom(dynaZoom)
if (distance < 0.2) {
map.setZoom(18)
} else if (distance < 0.5) {
map.setZoom(17)
} else if (distance < 1) {
map.setZoom(15)
} else if (distance < 2) {
map.setZoom(12)
} else {
map.setZoom(10)
}
}

function addMarkers(latLng) {
Expand Down
Binary file not shown.
12 changes: 7 additions & 5 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
FactoryGirl.define do
factory :user do
first_name "MyString"
last_name "MyString"
email "MyString"
password_digest "MyString"
organization "MyString"
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
sequence :email do |n|
Faker::Internet.email("lauren#{n}")
end
password "password"
organization "Turing School"
end
end
Loading

0 comments on commit b3215ed

Please sign in to comment.