From 08db8ec3dec9b19712e84e0ffafba01b77aeb6d9 Mon Sep 17 00:00:00 2001 From: parisdolfman Date: Sat, 27 Feb 2021 13:40:43 -0500 Subject: [PATCH] Done. --- Gemfile.lock | 3 ++- app/controllers/application_controller.rb | 19 +++++++++++++++---- app/models/user.rb | 1 + app/views/account.erb | 2 +- db/migrate/20210227183923_create_users.rb | 8 ++++++++ db/schema.rb | 20 ++++++++++++++++++++ 6 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20210227183923_create_users.rb create mode 100644 db/schema.rb diff --git a/Gemfile.lock b/Gemfile.lock index da877f8b5..b3966f17c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -99,6 +99,7 @@ GEM PLATFORMS ruby + x86_64-linux DEPENDENCIES activerecord (<= 5.1) @@ -117,4 +118,4 @@ DEPENDENCIES tux BUNDLED WITH - 2.0.1 + 2.2.2 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9429109e3..e15338bcf 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -17,7 +17,12 @@ class ApplicationController < Sinatra::Base end post "/signup" do - #your code here + if params[:username] == "" || params[:password] == "" + redirect '/failure' + else + User.create(username: params[:username], password: params[:password]) + redirect '/login' + end end @@ -32,9 +37,15 @@ 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 to "/account" + else + redirect to "/failure" + end end - + get "/failure" do erb :failure end @@ -54,4 +65,4 @@ def current_user end end -end +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index 4a57cf079..2b616d26a 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..0aff0df24 100644 --- a/app/views/account.erb +++ b/app/views/account.erb @@ -1,4 +1,4 @@ -

Welcome

+

Welcome <%=@user.username %>

We are currently working on your account.

diff --git a/db/migrate/20210227183923_create_users.rb b/db/migrate/20210227183923_create_users.rb new file mode 100644 index 000000000..3ae321009 --- /dev/null +++ b/db/migrate/20210227183923_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..10d13a7a3 --- /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: 20210227183923) do + + create_table "users", force: :cascade do |t| + t.string "username" + t.string "password_digest" + end + +end