Skip to content

Commit

Permalink
added signup page
Browse files Browse the repository at this point in the history
  • Loading branch information
joehannis committed Jun 3, 2023
1 parent a34fc34 commit 21e46d7
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 23 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ gem "sinatra", "~> 3.0"
gem "sinatra-contrib", "~> 3.0"
gem "webrick", "~> 1.8"
gem "rack-test", "~> 2.1"
gem 'bcrypt'
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ GEM
tzinfo (~> 2.0)
ansi (1.5.0)
ast (2.4.2)
bcrypt (3.1.18)
concurrent-ruby (1.2.2)
diff-lcs (1.4.4)
docile (1.4.0)
Expand Down Expand Up @@ -96,6 +97,7 @@ PLATFORMS

DEPENDENCIES
activerecord
bcrypt
pg (~> 1.3)
rack-test (~> 2.1)
rspec
Expand Down
27 changes: 23 additions & 4 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@ class Application < Sinatra::Base
configure do
# Set the default timezone to GMT
ENV['TZ'] = 'GMT'
@@posts = Post.all_peeps.reverse
end

get '/' do
@posts = Post.all_peeps.reverse
erb :index
erb(:index)
end

post '/' do
current_time = Time.now + 1 * 60 * 60 # Get the current time with GMT offset
Post.create(time: current_time, message: params[:message], user_id: 1)

# Append the new post to the class variable
@@posts.unshift("#{current_time} #{User.find(1).name} #{User.find(1).username} #{params[:message]}")


redirect '/'
end

Expand All @@ -37,8 +42,22 @@ class Application < Sinatra::Base
end

post '/signup' do
User.create(username: params[:username], email: params[:email], password: params[:password])
redirect '/success'
database = User.all_records
match_found = false

database.each do |record|
if record.username == params[:username] || record.email == params[:email]
match_found = true
break
end
end

if match_found
redirect '/signup'
else
User.create_user(params[:name], params[:username], params[:email], params[:password])
redirect '/success'
end
end

get '/success' do
Expand Down
2 changes: 1 addition & 1 deletion lib/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Post < ActiveRecord::Base
@posts = []
def self.all_peeps
Post.joins(:user).map do |post|
@posts << "#{post.time} #{post.user.username} #{post.message}"
@posts << "#{post.time} #{post.user.name} #{post.user.username} #{post.message}"
end
@posts
end
Expand Down
18 changes: 8 additions & 10 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'active_record'
require_relative 'database_connection'
require 'bcrypt'
establish_database_connection

class User < ActiveRecord::Base
Expand All @@ -12,17 +13,14 @@ def self.all_records
@users
end

def self.create_user(username, email, password)
user = User.new
user.username = username
user.email = email
user.password = password
def self.create_user(name, username, email, password)
encrypted_password = BCrypt::Password.create(password)
user = User.new(name: name, username: username, email: email, password: encrypted_password)
user.save
user
end
end

# User.create({ :username => 'Jamie', :email => "[email protected]", :password => "passwordjamie" })

# p User.all
# user.all_records
def authenticate(password)
BCrypt::Password.new(self.password) == password
end
end
25 changes: 25 additions & 0 deletions spec/integration/application.spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,30 @@
expect(response.body).to include('username')
end
end
context 'Get /sucess' do
it 'returns successpage' do
response = get('/success')
expect(response.status).to eq(200)
expect(response.body).to include('Success')
end
end
context 'Post /' do
it 'adds peep to chitter' do
response = post('/', message: 'Need to start working', user_id: 2)
Post.create_post(Time.now, 'Need to start working', 2)

expect(response.status).to eq(302)
end
end
context 'Post /signup' do
it 'recognises that a username already exists, redirects to the signup page' do
user = User.new
response = post('/signup', { name: 'Need to start working', username: 'laurenhannis', email: 'jdjdhjsjksd', password: 'jdsjdnsjs' })
User.create_user('Lauren Hannis', 'laurenhannis', 'jdjdhjsjksd', 'jdsjdnsjs')

expect(response.status).to eq(302) # Assuming it redirects
expect(response.headers['Location']).to include('/signup')
end
end
end

11 changes: 6 additions & 5 deletions spec/seeds/user_seeds.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ DROP TABLE IF EXISTS users CASCADE;
-- Table Definition
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name text,
username text,
email text,
password text
);

TRUNCATE TABLE users RESTART IDENTITY;

INSERT INTO users ("username", "email", "password") VALUES
('joehannis', '[email protected]', 'passwordjoe'),
('jakehannis', '[email protected]', 'passwordjake'),
('laurenhannis', '[email protected]', 'passwordlauren'),
('lunahannis', '[email protected]', 'passwordluna');
INSERT INTO users ("name", "username", "email", "password") VALUES
('Joe Hannis', 'joehannis', '[email protected]', 'passwordjoe'),
('Jake Hannis', 'jakehannis', '[email protected]', 'passwordjake'),
('Lauren Hannis', 'laurenhannis', '[email protected]', 'passwordlauren'),
('Luna Hannis', 'lunahannis', '[email protected]', 'passwordluna');
2 changes: 1 addition & 1 deletion spec/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
expect(User.all_records[1][:email]).to eq('[email protected]')
end
it 'creates a new user and returns the table' do
User.create_user('johnsmith', '[email protected]', 'password123')
User.create_user('John Smith', 'johnsmith', '[email protected]', 'password123')
expect(User.all_records[4][:email]).to eq('[email protected]')
end

Expand Down
2 changes: 1 addition & 1 deletion views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<input type="text" name="message">
<input type="submit" value="Post a message!">
</form></div>
<% @posts.each do |post| %>
<% @@posts.each do |post| %>
<%= post %><br />
<% end %>
</body>
Expand Down
4 changes: 3 additions & 1 deletion views/signup.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
<h1>Welcome to Chitter!</h1>
<div>Please register your details below</div>
<div><form action="/signup" method="POST">
<div>Name</div>
<input type="text" name="name">
<div>Username</div>
<input type="text" name="username">
<div>Password</div>
<input type="text" name="password">
<input type="password" name="password">
<div>Email</div>
<input type="text" name="email">
<input type="submit" value="Signup">
Expand Down

0 comments on commit 21e46d7

Please sign in to comment.