So I'm building a login/signup page in Ruby/Sinatra, and I'm trying to add some logic so that if someone tries to sign up with an email that is in use, it will tell them so, and not allow them to sign up
require 'rubygems'
require 'sinatra'
require 'mysql'
get "/" do
erb :form
end
post "/" do
begin
con = Mysql.new('localhost', 'tut', 'tut', 'recruited_users')
auth = con.query('SELECT school FROM users WHERE email = "#{params[:email]}" AND password = "#{params[:password]}"')
auth.fetch_row
ensure
con.close if con
end
end
get '/signup' do
erb :signup
end
post '/signup' do
begin
con = Mysql.new('localhost', 'tut', 'tut', 'recruited_users')
check_for_user = con.query("SELECT email FROM users WHERE email = '#{params[:email]}'")
if check_for_user == ''
"Sorry, but there is already an account for this user. The ID is '#{params[:check_for_user]}', please try again"
else
auth = con.query("INSERT INTO users (email, password, school) VALUES('#{params[:email]}', '#{params[:password]}', '#{params[:school]}')")
"Succesfully created user #{params[:email]}"
end
ensure
con.close if con
end
end
The problem is that the variable check_for_user is not receiving any value, at least not one that I can work with. I need to be able to set up the if statement so that they can only create a new user if the email does not already exist in the database.