Create model for users with email and password

This commit is contained in:
ricola 2025-04-06 17:04:31 -06:00
parent d6500e013d
commit 6682c8c300
6 changed files with 72 additions and 14 deletions

View file

@ -0,0 +1,9 @@
class CreateUsers < ActiveRecord::Migration[7.2]
def change
create_table :users do |t|
t.string :email
t.string :password
t.timestamps
end
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.2].define(version: 2025_03_21_224552) do ActiveRecord::Schema[7.2].define(version: 2025_03_21_234644) do
create_table "candidates", force: :cascade do |t| create_table "candidates", force: :cascade do |t|
t.integer "vote_id" t.integer "vote_id"
t.string "name" t.string "name"
@ -20,6 +20,13 @@ ActiveRecord::Schema[7.2].define(version: 2025_03_21_224552) do
t.index ["vote_id"], name: "index_candidates_on_vote_id" t.index ["vote_id"], name: "index_candidates_on_vote_id"
end end
create_table "users", force: :cascade do |t|
t.string "email"
t.string "password"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "votes", force: :cascade do |t| create_table "votes", force: :cascade do |t|
t.string "secure_id" t.string "secure_id"
t.string "title" t.string "title"

View file

@ -3,7 +3,14 @@
<p class="error"><%= @error %></p> <p class="error"><%= @error %></p>
<% end %> <% end %>
<form action="/login" method="POST"> <form action="/login" method="POST">
<input name="email" placeholder="Email" /> <p>
<input name="password" type="password" placeholder="Password" /> <label for="email">Email</label>
<input type="submit" value="Log in" /> <input type="text" name="email">
</p>
<p>
<label for="password">Password</label>
<input type="password" name="password">
</p>
<button type="submit">Log in</button>
</form> </form>
<a href="/signup">Create account</a>

13
views/signup.erb Normal file
View file

@ -0,0 +1,13 @@
<h1>Create account</h1>
<form action="/signup" method="post">
<p>
<label for="email">Email</label>
<input type="text" name="email">
</p>
<p>
<label for="password">Password</label>
<input type="password" name="password">
</p>
<button type="submit">Create account</button>
</form>

View file

@ -2,6 +2,8 @@
<p><%= @vote.description %></p> <p><%= @vote.description %></p>
<p>Hello, <%= current_user.email %>.</p>
<h2>Candidates</h2> <h2>Candidates</h2>
<ul> <ul>

40
vote.rb
View file

@ -15,6 +15,9 @@ class Candidate < ActiveRecord::Base
belongs_to :vote belongs_to :vote
end end
class User < ActiveRecord::Base
end
def hash_password(password) def hash_password(password)
BCrypt::Password.create(password).to_s BCrypt::Password.create(password).to_s
end end
@ -23,13 +26,6 @@ def verify_password(password, hash)
BCrypt::Password.new(hash) == password BCrypt::Password.new(hash) == password
end end
User = Struct.new(:id, :email, :password_hash)
USERS = [
User.new(1, 'P1', hash_password('P1')),
User.new(2, 'P2', hash_password('P2')),
User.new(3, 'P3', hash_password('P3')),
]
enable :sessions enable :sessions
get '/' do get '/' do
@ -38,13 +34,23 @@ get '/' do
erb :home erb :home
end end
get '/signup' do
erb :signup
end
post '/signup' do
@user = User.create(email: params[:email],
password: hash_password(params[:password]))
redirect '/'
end
get '/login' do get '/login' do
erb :login erb :login
end end
post '/login' do post '/login' do
user = USERS.find { |u| u.email == params[:email] } user = User.find_by(email: params[:email])
if user && verify_password(params[:password], user.password_hash) if user && verify_password(params[:password], user.password)
session.clear session.clear
session[:user_id] = user.id session[:user_id] = user.id
redirect '/' redirect '/'
@ -64,6 +70,7 @@ get '/votes/new' do
end end
get '/votes/:id' do get '/votes/:id' do
redirect '/login' unless current_user
@vote = Vote.find(params[:id]) @vote = Vote.find(params[:id])
erb :votes_show erb :votes_show
end end
@ -84,10 +91,23 @@ post '/votes/:id/candidates' do
redirect '/votes/' + @vote.secure_id redirect '/votes/' + @vote.secure_id
end end
post '/votes/:id/ratings' do
redirect '/login' unless current_user
vote = Vote.find(params[:id])
vote.candidates.each do |candidate|
rating = Rating.find_by(user: current_user)
rating = Rating.find_by(candidate: candidate)
rating = Rating.find_or_initialize_by(user: current_user, candidate: candidate)
rating.value = params[candidate.id.to_s]
rating.save
end
redirect '/votes/' + vote.secure_id
end
helpers do helpers do
def current_user def current_user
if session[:user_id] if session[:user_id]
USERS.find { |u| u.id == session[:user_id] } User.find(session[:user_id])
else else
nil nil
end end