diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9429109e3..f15402a33 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -17,12 +17,29 @@ class ApplicationController < Sinatra::Base end post "/signup" do - #your code here + + if params[:username] == "" || params[:password] == "" + redirect to "/failure" + else + user = User.create(:username => params[:username], + :password => params[:password]) - end + redirect to "/login" + end +end + + #or + #post "/signup" do + #if params[:username] == "" || params[:password] == "" + #redirect '/failure' + #else + #User.create(username: params[:username], password: params[#:password]) + #redirect '/login' + #end + #end get '/account' do - @user = User.find(session[:user_id]) + @user = User.find(session[:user_id]) erb :account end @@ -31,9 +48,18 @@ class ApplicationController < Sinatra::Base erb :login end - post "/login" do - ##your code here + post "/login" do + @user = User.find_by(username: params[:username]) + if @user && @user.authenticate(params[:password]) + session[:user_id] = @user.id + + redirect to "/account" + else + + redirect to "/failure" + end end + get "/failure" do erb :failure @@ -52,6 +78,7 @@ def logged_in? def current_user User.find(session[:user_id]) end - end + end +end + -end diff --git a/app/models/user.rb b/app/models/user.rb index 4a57cf079..c5139792a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,2 +1,4 @@ class User < ActiveRecord::Base + has_secure_password + end diff --git a/app/views/failure.erb b/app/views/failure.erb index 1faa52448..87c105c64 100644 --- a/app/views/failure.erb +++ b/app/views/failure.erb @@ -1,2 +1,3 @@ +
It seems there was a problem. Please return to the home page and try again.
diff --git a/db/migrate/001_users.rb b/db/migrate/001_users.rb new file mode 100644 index 000000000..4aff7e587 --- /dev/null +++ b/db/migrate/001_users.rb @@ -0,0 +1,9 @@ +class Users < ActiveRecord::Migration[4.2] + + def change + create_table :users do |t| + t.string :username + t.string :password_digest + end + end +end \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 000000000..7a28f7fc0 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,20 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 1) do + + create_table "users", force: :cascade do |t| + t.string "username" + t.string "password_digest" + end + +end