From 6192edb3eb6dd4db47db91c22e9b6788733b93a1 Mon Sep 17 00:00:00 2001 From: Daniel Suarez Date: Sat, 19 Jun 2021 01:04:37 -0400 Subject: [PATCH 1/4] Done. --- app/controllers/application_controller.rb | 16 +++++++++++++++- app/models/user.rb | 1 + db/migrate/20210619044238_create_users.rb | 8 ++++++++ db/schema.rb | 20 ++++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20210619044238_create_users.rb create mode 100644 db/schema.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9429109e3..a3565c756 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -18,7 +18,13 @@ class ApplicationController < Sinatra::Base post "/signup" do #your code here - + user = User.new(:username => params[:username], :password => params[:password]) + + if user.save + redirect "/login" + else + redirect "/failure" + end end get '/account' do @@ -33,6 +39,14 @@ class ApplicationController < Sinatra::Base 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 "/account" + else + redirect "/failure" + end end get "/failure" do diff --git a/app/models/user.rb b/app/models/user.rb index 4a57cf079..40f070579 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,2 +1,3 @@ class User < ActiveRecord::Base + has_secure_password end diff --git a/db/migrate/20210619044238_create_users.rb b/db/migrate/20210619044238_create_users.rb new file mode 100644 index 000000000..3ae321009 --- /dev/null +++ b/db/migrate/20210619044238_create_users.rb @@ -0,0 +1,8 @@ +class CreateUsers < ActiveRecord::Migration[5.1] + def change + create_table :users do |t| + t.string :username + t.string :password_digest + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 000000000..3a6a28b85 --- /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: 20210619044238) do + + create_table "users", force: :cascade do |t| + t.string "username" + t.string "password_digest" + end + +end From db28ab3d009a6374e9d3db767c0e1776f4b03da7 Mon Sep 17 00:00:00 2001 From: Daniel Suarez Date: Sat, 19 Jun 2021 01:39:00 -0400 Subject: [PATCH 2/4] Done. --- app/controllers/application_controller.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a3565c756..277caf705 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -27,12 +27,6 @@ class ApplicationController < Sinatra::Base end end - get '/account' do - @user = User.find(session[:user_id]) - erb :account - end - - get "/login" do erb :login end @@ -49,6 +43,15 @@ class ApplicationController < Sinatra::Base end end + get "/account" do + # @user = User.find(session[:user_id]) + if logged_in? + erb :account + else + redirect "/login" + end + end + get "/failure" do erb :failure end From 87bddd69542d538b86b792ebab661b210b0c62c2 Mon Sep 17 00:00:00 2001 From: Daniel Suarez Date: Sat, 19 Jun 2021 01:59:23 -0400 Subject: [PATCH 3/4] Done. --- app/controllers/application_controller.rb | 2 +- config.ru | 1 + config/environment.rb | 4 +++- db/development.sqlite | 0 4 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 db/development.sqlite diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 277caf705..07691b564 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -53,7 +53,7 @@ class ApplicationController < Sinatra::Base end get "/failure" do - erb :failure + erb :failure end get "/logout" do diff --git a/config.ru b/config.ru index 25e640c70..5b98a3927 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,4 @@ require './app/controllers/application_controller' + run ApplicationController \ No newline at end of file diff --git a/config/environment.rb b/config/environment.rb index a11542813..7ce0f288d 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -5,4 +5,6 @@ set :database, {adapter: "sqlite3", database: "db/database.sqlite3"} end require_relative '../app/controllers/application_controller.rb' -require_all 'app/models' \ No newline at end of file +require_all 'app/models' + + diff --git a/db/development.sqlite b/db/development.sqlite new file mode 100644 index 000000000..e69de29bb From 17ca294dbdf336b728d5243f60a194a597366db8 Mon Sep 17 00:00:00 2001 From: Daniel Suarez Date: Sat, 19 Jun 2021 20:00:29 -0400 Subject: [PATCH 4/4] Done. --- Rakefile | 8 ++++++-- app/controllers/application_controller.rb | 6 +++--- config.ru | 13 ++++++++++++- config/environment.rb | 11 +++++++++++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/Rakefile b/Rakefile index dfe158548..3535fc871 100644 --- a/Rakefile +++ b/Rakefile @@ -1,2 +1,6 @@ -require "./config/environment" -require "sinatra/activerecord/rake" \ No newline at end of file +require_relative "./config/environment" +require "sinatra/activerecord/rake" + +task :console do + Pry.start +end \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 07691b564..d028e8e06 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -18,12 +18,12 @@ class ApplicationController < Sinatra::Base post "/signup" do #your code here - user = User.new(:username => params[:username], :password => params[:password]) + @user = User.new(:username => params[:username], :password => params[:password]) - if user.save + if @user.save redirect "/login" else - redirect "/failure" + redirect "/failure" end end diff --git a/config.ru b/config.ru index 5b98a3927..7e00049b7 100644 --- a/config.ru +++ b/config.ru @@ -1,4 +1,15 @@ require './app/controllers/application_controller' -run ApplicationController \ No newline at end of file +run ApplicationController + + + +# require_relative './config/environment' + +# if ActiveRecord::Migrator.need_migration? +# raise 'Migrations are pending. Run 'rake db:migrate' to resolve the issue.' +# end + +# use Rack::MethodOverride +# run ApplicationController \ No newline at end of file diff --git a/config/environment.rb b/config/environment.rb index 7ce0f288d..dc3ce2fb7 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -1,6 +1,7 @@ require 'bundler' Bundler.require + configure :development do set :database, {adapter: "sqlite3", database: "db/database.sqlite3"} end @@ -8,3 +9,13 @@ require_all 'app/models' + +# require 'bundler' +# Bundler.require + +# ActiveRecord::Base.establish_connection( +# :adapter => "sqlite3", +# :database => "db/database.sqlite3" +# ) + +# require_all 'app' \ No newline at end of file