Commit 6ada62a

mo khan <mo@mokhan.ca>
2015-02-21 19:31:50
add user model and login page.
1 parent d3f91de
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/
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/
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
app/helpers/sessions_helper.rb
@@ -0,0 +1,2 @@
+module SessionsHelper
+end
app/models/event.rb
@@ -1,3 +1,4 @@
 class Event < ActiveRecord::Base
   belongs_to :agent
+  has_secure_password
 end
app/models/user.rb
@@ -0,0 +1,3 @@
+class User < ActiveRecord::Base
+  has_secure_password
+end
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>
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'
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
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
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'