From 47ce08bb202fddc74b377d07906cae250e73a0a5 Mon Sep 17 00:00:00 2001 From: Ken Johnson Date: Thu, 25 Apr 2013 00:19:00 -0400 Subject: [PATCH] working login, signup, and logout --- app/controllers/application_controller.rb | 2 +- app/controllers/dashboard_controller.rb | 6 ++++++ app/controllers/sessions_controller.rb | 13 +++++++++++++ app/controllers/tutorials_controller.rb | 0 app/controllers/users_controller.rb | 9 +++++++++ app/helpers/dashboard_helper.rb | 2 ++ app/helpers/tutorials_helper.rb | 0 app/models/user.rb | 17 ++++++++++++++++- app/views/dashboard/home.html.erb | 0 app/views/layouts/shared/_header.html.erb | 7 +++---- app/views/layouts/tutorial/_header.html.erb | 0 app/views/layouts/tutorial/_sidebar.html.erb | 0 app/views/users/new.html.erb | 16 ++++++++-------- config/routes.rb | 7 +++++-- db/migrate/20130424220355_create_users.rb | 1 - db/schema.rb | 0 test/functional/dashboard_controller_test.rb | 7 +++++++ test/functional/tutorials_controller_test.rb | 0 test/unit/helpers/dashboard_helper_test.rb | 4 ++++ test/unit/helpers/tutorials_helper_test.rb | 0 20 files changed, 74 insertions(+), 17 deletions(-) create mode 100755 app/controllers/dashboard_controller.rb mode change 100644 => 100755 app/controllers/tutorials_controller.rb create mode 100755 app/helpers/dashboard_helper.rb mode change 100644 => 100755 app/helpers/tutorials_helper.rb create mode 100644 app/views/dashboard/home.html.erb mode change 100644 => 100755 app/views/layouts/tutorial/_header.html.erb mode change 100644 => 100755 app/views/layouts/tutorial/_sidebar.html.erb mode change 100644 => 100755 db/schema.rb create mode 100755 test/functional/dashboard_controller_test.rb mode change 100644 => 100755 test/functional/tutorials_controller_test.rb create mode 100755 test/unit/helpers/dashboard_helper_test.rb mode change 100644 => 100755 test/unit/helpers/tutorials_helper_test.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0730591..c0a10b8 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -9,7 +9,7 @@ class ApplicationController < ActionController::Base private def current_user - @current_user ||= User.find_by_user_id(session[:user_id].to_s) + @current_user ||= User.find_by_id(session[:id].to_s) end def authenticated diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb new file mode 100755 index 0000000..8d5138f --- /dev/null +++ b/app/controllers/dashboard_controller.rb @@ -0,0 +1,6 @@ +class DashboardController < ApplicationController + + def home + end + +end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 4e35018..2ec2920 100755 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -3,9 +3,22 @@ class SessionsController < ApplicationController skip_before_filter :authenticated, :only => [:new, :create] def new + redirect_to dashboard_home_path(:dashboard_id => current_user.id) if current_user && current_user.id end def create + user = User.authenticate(params[:email], params[:password]) + if user + session[:id] = user.id if User.where(:id => user.id).exists? + redirect_to dashboard_home_path(:dashboard_id => user.id) + else + render "new" + end + end + + def destroy + reset_session + redirect_to root_path end end diff --git a/app/controllers/tutorials_controller.rb b/app/controllers/tutorials_controller.rb old mode 100644 new mode 100755 diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index c60dfea..1686101 100755 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -3,9 +3,18 @@ class UsersController < ApplicationController skip_before_filter :authenticated, :only => [:new, :create] def new + @user = User.new end def create + user = User.new(params[:user]) + if user.save + session[:id] = user.id + redirect_to dashboard_home_path(:dashboard_id => user.id) + else + @user = User.new + render :new + end end end diff --git a/app/helpers/dashboard_helper.rb b/app/helpers/dashboard_helper.rb new file mode 100755 index 0000000..a94ddfc --- /dev/null +++ b/app/helpers/dashboard_helper.rb @@ -0,0 +1,2 @@ +module DashboardHelper +end diff --git a/app/helpers/tutorials_helper.rb b/app/helpers/tutorials_helper.rb old mode 100644 new mode 100755 diff --git a/app/models/user.rb b/app/models/user.rb index 3778979..5b29eac 100755 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,3 +1,18 @@ class User < ActiveRecord::Base - attr_accessible :email, :password, :user_id, :admin + attr_accessible :email, :password, :user_id, :admin, :password_confirmation + validates_confirmation_of :password, :password_confirmation + validates_presence_of :password, :on => :create + validates_presence_of :email + validates_uniqueness_of :email + + def self.authenticate(email, password) + auth = nil + user = find_by_email(email) + # I heard something about hashing, dunno, why bother really. Nobody will get access to my stuff! + if user && user.password == password + auth = user + end + return auth + end + end diff --git a/app/views/dashboard/home.html.erb b/app/views/dashboard/home.html.erb new file mode 100644 index 0000000..e69de29 diff --git a/app/views/layouts/shared/_header.html.erb b/app/views/layouts/shared/_header.html.erb index 99f6265..36a90ce 100755 --- a/app/views/layouts/shared/_header.html.erb +++ b/app/views/layouts/shared/_header.html.erb @@ -4,7 +4,8 @@
- profile + profile +
diff --git a/app/views/layouts/tutorial/_header.html.erb b/app/views/layouts/tutorial/_header.html.erb old mode 100644 new mode 100755 diff --git a/app/views/layouts/tutorial/_sidebar.html.erb b/app/views/layouts/tutorial/_sidebar.html.erb old mode 100644 new mode 100755 diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index a8abddc..0188314 100755 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb @@ -5,7 +5,7 @@