summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2015-02-21 12:31:50 -0700
committermo khan <mo@mokhan.ca>2015-02-21 12:31:50 -0700
commit6ada62adfd2b60e3af4f721ed8e09a5658920fa6 (patch)
tree1f65086944ce3be9b12b48bda16761fde6a6b965
parentd3f91de6fcc6d27bc2d7adb8f1e14d73b71e7b96 (diff)
add user model and login page.
-rw-r--r--Gemfile2
-rw-r--r--app/assets/javascripts/sessions.coffee3
-rw-r--r--app/assets/stylesheets/sessions.scss3
-rw-r--r--app/controllers/sessions_controller.rb17
-rw-r--r--app/helpers/sessions_helper.rb2
-rw-r--r--app/models/event.rb1
-rw-r--r--app/models/user.rb3
-rw-r--r--app/views/sessions/new.html.erb19
-rw-r--r--config/routes.rb1
-rw-r--r--db/migrate/20150221192553_create_users.rb10
-rw-r--r--db/schema.rb9
11 files changed, 68 insertions, 2 deletions
diff --git a/Gemfile b/Gemfile
index 812b7f0..856be98 100644
--- a/Gemfile
+++ b/Gemfile
@@ -26,7 +26,7 @@ gem 'typhoeus'
gem 'listen'
# Use ActiveModel has_secure_password
-# gem 'bcrypt', '~> 3.1.7'
+gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
diff --git a/app/assets/javascripts/sessions.coffee b/app/assets/javascripts/sessions.coffee
new file mode 100644
index 0000000..24f83d1
--- /dev/null
+++ b/app/assets/javascripts/sessions.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://coffeescript.org/
diff --git a/app/assets/stylesheets/sessions.scss b/app/assets/stylesheets/sessions.scss
new file mode 100644
index 0000000..7bef9cf
--- /dev/null
+++ b/app/assets/stylesheets/sessions.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the sessions controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb
new file mode 100644
index 0000000..baf8679
--- /dev/null
+++ b/app/controllers/sessions_controller.rb
@@ -0,0 +1,17 @@
+class SessionsController < ApplicationController
+ def new
+ @user = User.new
+ end
+
+ def create
+ user = User.find_by(username: user_params[:username])
+ if user && user.authenticate(user_params[:password])
+ session[:x] = user.id
+ redirect_to agents_path
+ end
+ redirect_to new_session_path
+ end
+
+ def destroy
+ end
+end
diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb
new file mode 100644
index 0000000..309f8b2
--- /dev/null
+++ b/app/helpers/sessions_helper.rb
@@ -0,0 +1,2 @@
+module SessionsHelper
+end
diff --git a/app/models/event.rb b/app/models/event.rb
index 3e55383..8124b3f 100644
--- a/app/models/event.rb
+++ b/app/models/event.rb
@@ -1,3 +1,4 @@
class Event < ActiveRecord::Base
belongs_to :agent
+ has_secure_password
end
diff --git a/app/models/user.rb b/app/models/user.rb
new file mode 100644
index 0000000..40f0705
--- /dev/null
+++ b/app/models/user.rb
@@ -0,0 +1,3 @@
+class User < ActiveRecord::Base
+ has_secure_password
+end
diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb
new file mode 100644
index 0000000..7e99bf1
--- /dev/null
+++ b/app/views/sessions/new.html.erb
@@ -0,0 +1,19 @@
+<div class="row">
+<div class="small-12 columns">
+ <h1>Login</h1>
+
+ <%= form_for @user, url: sessions_path do |f| %>
+ <div class="field">
+ <%= label_tag :username %><br>
+ <%= text_field_tag :username %>
+ </div>
+ <div class="field">
+ <%= label_tag :password %><br>
+ <%= password_field_tag :password %>
+ </div>
+ <div class="actions">
+ <%= submit_tag %>
+ </div>
+ <% end %>
+</div>
+</div>
diff --git a/config/routes.rb b/config/routes.rb
index ae0a4cb..3d664d0 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
+ resources :sessions, only: [:new, :create, :destroy]
resources :agents do
resources :events, only: [:index, :new, :create, :destroy], controller: 'agents/events'
resources :files, only: [:index, :show], controller: 'agents/files'
diff --git a/db/migrate/20150221192553_create_users.rb b/db/migrate/20150221192553_create_users.rb
new file mode 100644
index 0000000..72a9f38
--- /dev/null
+++ b/db/migrate/20150221192553_create_users.rb
@@ -0,0 +1,10 @@
+class CreateUsers < ActiveRecord::Migration
+ def change
+ create_table :users do |t|
+ t.string :username
+ t.string :password_digest
+
+ t.timestamps null: false
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index d62cc41..d7219a5 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20150207194524) do
+ActiveRecord::Schema.define(version: 20150221192553) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -47,4 +47,11 @@ ActiveRecord::Schema.define(version: 20150207194524) do
t.datetime "updated_at", null: false
end
+ create_table "users", force: :cascade do |t|
+ t.string "username"
+ t.string "password_digest"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ end
+
end