diff --git a/Rakefile b/Rakefile index dfe158548..4bf93d0fe 100644 --- a/Rakefile +++ b/Rakefile @@ -1,2 +1,6 @@ require "./config/environment" -require "sinatra/activerecord/rake" \ No newline at end of file +require "sinatra/activerecord/rake" + +task :console do + Pry.start +end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9429109e3..ff025029b 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -17,7 +17,15 @@ class ApplicationController < Sinatra::Base end post "/signup" do + puts params.inspect #your code here + user = User.new(:username => params[:username], :password => params[:password]) + #binding.pry + if user.username != "" && user.save + redirect "/login" + else + redirect "/failure" + end end @@ -33,6 +41,13 @@ 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/app/views/account.erb b/app/views/account.erb index 445620b2f..874a0dcab 100644 --- a/app/views/account.erb +++ b/app/views/account.erb @@ -2,4 +2,10 @@

We are currently working on your account.

+

Your account details

+

Session <%= session[:user_id] %>

+

Current User: <%= current_user.username %>

+

Current Balance $: <%= current_user.balance %>

+ +

Log Out

diff --git a/db/migrate/20200623020721_create_users.rb b/db/migrate/20200623020721_create_users.rb new file mode 100644 index 000000000..3ae321009 --- /dev/null +++ b/db/migrate/20200623020721_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/migrate/20200623042228_add_balance_to_users.rb b/db/migrate/20200623042228_add_balance_to_users.rb new file mode 100644 index 000000000..b86d8ba55 --- /dev/null +++ b/db/migrate/20200623042228_add_balance_to_users.rb @@ -0,0 +1,5 @@ +class AddBalanceToUsers < ActiveRecord::Migration[5.1] + def change + add_column :users, :balance, :decimal, :default => 0 + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 000000000..0760dd021 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,21 @@ +# 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: 20200623042228) do + + create_table "users", force: :cascade do |t| + t.string "username" + t.string "password_digest" + t.decimal "balance", default: "0.0" + end + +end