Skip to content

Commit

Permalink
Login functionality added
Browse files Browse the repository at this point in the history
  • Loading branch information
chugh97 committed Dec 11, 2013
1 parent 0c6bb30 commit f0deb1f
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 126 deletions.
9 changes: 9 additions & 0 deletions .idea/railways.cache

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

318 changes: 205 additions & 113 deletions .idea/workspace.xml

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception

before_filter :is_user_logged_in

def is_user_logged_in
if session[:current_user].nil?
@logged_in_user = false
else
@logged_in_user = true
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/carts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def add
end

def products_in_existing_cart
Cart.where("session_id = ? AND product_id = ?", session[:session_id], params[:product][:id])
Cart.where("session_id = ? AND product_id = ? and purchased_at IS NULL", session[:session_id], params[:product][:id])
end

def index
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/invitation_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def new
def confirm
invite = Invitation.find_by_invite_code(params[:id])
user = User.find_by_email(invite.email)
user.is_confirmed = true
user.save
user.update_attribute :is_confirmed, true

flash[:notice] = "Your email is confirmed. Please Sign in....."
redirect_to newuser_path
end
Expand Down
30 changes: 28 additions & 2 deletions app/controllers/user_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require 'digest/sha1'

class UserController < ApplicationController

skip_before_filter :verify_authenticity_token, :only => [:address]

before_filter :redirect_if_logged_in, :only => [:new, :create, :login]

def new

Expand All @@ -23,6 +25,16 @@ def registration
@user = User.find_by(:id => user_id)
end

def login
@user = User.where("email = ? and encrypted_password=? and is_confirmed = true", login_params[:email], Digest::SHA1.hexdigest(login_params[:password]))
if @user.first
session[:current_user] = @user.first.id
redirect_to user_registration_path(:user => @user.first)
else
render :new
end
end

def address
@user = User.find_by(:id => params[:id]);

Expand All @@ -36,14 +48,28 @@ def address
@user.phones << Phone.create!(:phone_type_id => phone_type.id, :user_id => @user.id, :phone_number => phone[:number]);
end

session[:user_id] = @user.id
data = { :success => 'true' }
render :json => data, :status => :ok
end

def logout
reset_session
redirect_to root_path
end

private

def redirect_if_logged_in
if !session[:current_user].nil?
redirect_to user_registration_path(:user => session[:current_user])
end
end

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

def login_params
params.require(:user).permit(:email,:password)
end
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class User < ActiveRecord::Base

private
def hash_password
self.encrypted_password = Digest::SHA1.hexdigest(self.encrypted_password)
self.encrypted_password = Digest::SHA1.hexdigest(self.encrypted_password) if new_record?
end

end
15 changes: 10 additions & 5 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,28 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">e-Shop</a>
<a class="navbar-brand" href="/">e-Shop</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
<li class="active"><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<%if @logged_in_user == true%>
<li><a href="/logout">Logout</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li class="divider"></li>
<li class="dropdown-header">Nav header</li>
<li><a href="#">Separated link</a></li>
<li><a href="#">One more separated link</a></li>
<%elsif%>
<li><a href="/newuser">Login</a></li>
<li><a href="#">Another action</a></li>
<%end%>
</ul>
</li>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion app/views/user/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="notice"><%= flash[:notice] %></div>
<% end %>
<div class="col-md-6">
<%=form_tag('/user/existing', method: "post", :id => "user_existing", :class=> "form-signin") do%>
<%=form_tag('/login', method: "post", :id => "user_existing", :class=> "form-signin") do%>
<h2 class="form-signin-heading">Existing User</h2>

<input type="text" id="user[email]" name="user[email]" class="validate form-control" placeholder="Email"/> <br/>
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
post 'carts/update' => 'carts#update'
post 'carts/deletelineitem' => 'carts#deletelineitem'

get 'newuser' => 'user#new'
get '/newuser' => 'user#new'
post 'user/create' => 'user#create'
get 'user/registration' => 'user#registration'
post 'user/address' => 'user#address'
post '/login' => 'user#login', :as => 'login'
get '/logout' => 'user#logout', :as => 'logout'
get "/auth/paypal/callback", to: "callback#show"

resources :invitation do
Expand Down
7 changes: 7 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@
ProductPrice.create!(product: p2, price: 2.69, effective_start_date: 1.day.ago)
ProductPrice.create!(product: p3, price: 14.99, effective_start_date: 1.day.ago)

AddressType.create!(description: "Delivery")
AddressType.create!(description: "Billing")

PhoneType.create!(description: "Home")
PhoneType.create!(description: "Mobile")



0 comments on commit f0deb1f

Please sign in to comment.