From 46854b2f2fdd3f68b8b124037bb7b9df20ff2e3f Mon Sep 17 00:00:00 2001 From: Jonathan Fernandez Date: Sun, 23 Feb 2020 06:09:04 +0000 Subject: [PATCH 1/6] Done. --- app/controllers/application_controller.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9429109e3..babaf911d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -17,9 +17,15 @@ class ApplicationController < Sinatra::Base end post "/signup" do - #your code here - - end + user = User.new(:username => params[:username], :password => params[:password]) + + if user.save + redirect to "/login" + else + redirect to "/failure" + end + end +end get '/account' do @user = User.find(session[:user_id]) From a9e458245f5174a14697cfc44bf36bf4e202c57e Mon Sep 17 00:00:00 2001 From: Jonathan Fernandez Date: Sun, 23 Feb 2020 06:36:00 +0000 Subject: [PATCH 2/6] Done. --- app/controllers/application_controller.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index babaf911d..56ba75d85 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -20,10 +20,10 @@ class ApplicationController < Sinatra::Base user = User.new(:username => params[:username], :password => params[:password]) if user.save - redirect to "/login" + redirect "/login" else - redirect to "/failure" - end + redirect "/failure" + end end end @@ -38,7 +38,13 @@ class ApplicationController < Sinatra::Base end post "/login" do - ##your code here + user = User.find_by(:username => params[:username]) + if user && user.authenticate(params[:password]) + session[user.id] = user.id + redirect "/success" + else + redirect "/failure" + end end get "/failure" do @@ -60,4 +66,4 @@ def current_user end end -end + From 5b85a550e5f8c2cedecd7c4ee6bbeb2dda08b772 Mon Sep 17 00:00:00 2001 From: Jonathan Fernandez Date: Sun, 23 Feb 2020 07:19:35 +0000 Subject: [PATCH 3/6] Done. --- db/migrate/001_users.rb | 9 +++++++++ db/schema.rb | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 db/migrate/001_users.rb create mode 100644 db/schema.rb diff --git a/db/migrate/001_users.rb b/db/migrate/001_users.rb new file mode 100644 index 000000000..44a4d76d6 --- /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 + 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..d895352a9 --- /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" + end + +end From b4704dc9fe13661cb8fefb93769bf0fe800671ec Mon Sep 17 00:00:00 2001 From: Jonathan Fernandez Date: Sun, 23 Feb 2020 08:40:57 +0000 Subject: [PATCH 4/6] Done. --- app/controllers/application_controller.rb | 9 +++++---- app/models/user.rb | 2 ++ app/views/failure.erb | 1 + 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 56ba75d85..4ac570767 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -37,14 +37,15 @@ class ApplicationController < Sinatra::Base erb :login end - post "/login" do + post "/login" do user = User.find_by(:username => params[:username]) + if user && user.authenticate(params[:password]) - session[user.id] = user.id + session[:user_id] = user.id redirect "/success" - else + else redirect "/failure" - end + end end get "/failure" do 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 @@ +

Flatiron Bank Error

It seems there was a problem. Please return to the home page and try again.

From 11120c5275a6d5a8bd26cc3cad1952d81fc9789f Mon Sep 17 00:00:00 2001 From: Jonathan Fernandez Date: Tue, 25 Feb 2020 14:13:40 +0000 Subject: [PATCH 5/6] Done. --- app/controllers/application_controller.rb | 46 ++++++++++++++--------- db/migrate/001_users.rb | 2 +- db/schema.rb | 2 +- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4ac570767..de587c4d6 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -17,18 +17,18 @@ class ApplicationController < Sinatra::Base end post "/signup" do - user = User.new(:username => params[:username], :password => params[:password]) - - if user.save - redirect "/login" - else - redirect "/failure" - end - end + + if params[:username] == "" || params[:password] == "" + redirect to "/failure" + else + user = User.create(:username => params[:username], :password => params[:password]) + + redirect to "/login" + end end get '/account' do - @user = User.find(session[:user_id]) + @user = User.find(session[:user_id]) erb :account end @@ -38,14 +38,23 @@ class ApplicationController < Sinatra::Base end post "/login" do - user = User.find_by(:username => params[:username]) - - if user && user.authenticate(params[:password]) - session[:user_id] = user.id - redirect "/success" - else - redirect "/failure" - end + @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 "/success" do + if logged_in? + erb :success + else + redirect "/login" + end end get "/failure" do @@ -65,6 +74,7 @@ def logged_in? def current_user User.find(session[:user_id]) end - end + end +end diff --git a/db/migrate/001_users.rb b/db/migrate/001_users.rb index 44a4d76d6..4aff7e587 100644 --- a/db/migrate/001_users.rb +++ b/db/migrate/001_users.rb @@ -3,7 +3,7 @@ class Users < ActiveRecord::Migration[4.2] def change create_table :users do |t| t.string :username - t.string :password + t.string :password_digest end end end \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index d895352a9..7a28f7fc0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -14,7 +14,7 @@ create_table "users", force: :cascade do |t| t.string "username" - t.string "password" + t.string "password_digest" end end From 5eeb08b4afd45dde22bfcebf6bb5ef8805980bc6 Mon Sep 17 00:00:00 2001 From: Jonathan Fernandez Date: Tue, 25 Feb 2020 15:35:59 +0000 Subject: [PATCH 6/6] Done. --- app/controllers/application_controller.rb | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index de587c4d6..f15402a33 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -21,12 +21,23 @@ class ApplicationController < Sinatra::Base if params[:username] == "" || params[:password] == "" redirect to "/failure" else - user = User.create(:username => params[:username], :password => params[:password]) + user = User.create(:username => params[:username], + :password => params[:password]) 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]) erb :account @@ -49,13 +60,6 @@ class ApplicationController < Sinatra::Base end end - get "/success" do - if logged_in? - erb :success - else - redirect "/login" - end - end get "/failure" do erb :failure