Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
require "./config/environment"
require "sinatra/activerecord/rake"
require "sinatra/activerecord/rake"

task :console do
Pry.start
end
15 changes: 15 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class User < ActiveRecord::Base
has_secure_password
end
6 changes: 6 additions & 0 deletions app/views/account.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@

<p>We are currently working on your account.</p>

<h2>Your account details</h2>
<h2>Session <%= session[:user_id] %></h2>
<h2>Current User: <%= current_user.username %></h2>
<h2>Current Balance $: <%= current_user.balance %></h2>


<p><a href="/logout">Log Out</a></p>
8 changes: 8 additions & 0 deletions db/migrate/20200623020721_create_users.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions db/migrate/20200623042228_add_balance_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddBalanceToUsers < ActiveRecord::Migration[5.1]
def change
add_column :users, :balance, :decimal, :default => 0
end
end
21 changes: 21 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -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