Validate email and password
This commit is contained in:
parent
069d565ba9
commit
4faa4fd555
3 changed files with 41 additions and 6 deletions
|
|
@ -1,7 +1,9 @@
|
||||||
<h1><%= _("Login") %></h1>
|
<h1><%= _("Login") %></h1>
|
||||||
|
|
||||||
<% if @error %>
|
<% if @error %>
|
||||||
<p class="error"><%= @error %></p>
|
<p class="error"><%= @error %></p>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<form action="/login" method="POST">
|
<form action="/login" method="POST">
|
||||||
<p>
|
<p>
|
||||||
<label for="email"><%= _("Email") %></label>
|
<label for="email"><%= _("Email") %></label>
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,32 @@
|
||||||
<h1><%= _("Create account") %></h1>
|
<h1><%= _("Create account") %></h1>
|
||||||
|
|
||||||
|
<% if @errors %>
|
||||||
|
<% @errors.each do |error| %>
|
||||||
|
<% if error.attribute == :password and error.type == :blank %>
|
||||||
|
<p class="error"><%= _("Specify a password.") %></p>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
|
<% if @user and @user.errors.any? %>
|
||||||
|
<% @user.errors.each do |error| %>
|
||||||
|
<% if error.attribute == :email and error.type == :invalid %>
|
||||||
|
<p class="error"><%= _("Email is not a valid email address.") %></p>
|
||||||
|
<% end %>
|
||||||
|
<% if error.attribute == :email and error.type == :taken %>
|
||||||
|
<p class="error"><%= _("An account already exists for %{email}.") % { email: @user.email } %></p>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
<form action="/signup" method="post">
|
<form action="/signup" method="post">
|
||||||
<p>
|
<p>
|
||||||
<label for="email"><%= _("Email") %></label>
|
<label for="email"><%= _("Email") %></label>
|
||||||
<input type="text" name="email">
|
<input type="text" name="email" value="<%= params[:email] %>">
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<label for="password"><%= _("Password") %></label>
|
<label for="password"><%= _("Password") %></label>
|
||||||
<input type="password" name="password">
|
<input type="password" name="password" value="<%= params[:password] %>">
|
||||||
</p>
|
</p>
|
||||||
<button type="submit"><%= _("Create account") %></button>
|
<button type="submit"><%= _("Create account") %></button>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
||||||
22
vote.rb
22
vote.rb
|
|
@ -28,6 +28,8 @@ class User < ActiveRecord::Base
|
||||||
has_many :ratings
|
has_many :ratings
|
||||||
has_many :organizers
|
has_many :organizers
|
||||||
has_many :votes, through: :organizers
|
has_many :votes, through: :organizers
|
||||||
|
validates :email, uniqueness: true
|
||||||
|
validates :email, format: URI::MailTo::EMAIL_REGEXP
|
||||||
end
|
end
|
||||||
|
|
||||||
class Organizer < ActiveRecord::Base
|
class Organizer < ActiveRecord::Base
|
||||||
|
|
@ -81,9 +83,21 @@ get '/signup' do
|
||||||
end
|
end
|
||||||
|
|
||||||
post '/signup' do
|
post '/signup' do
|
||||||
@user = User.create(email: params[:email],
|
@user = User.create(email: params[:email])
|
||||||
password: hash_password(params[:password]))
|
@errors = []
|
||||||
redirect '/'
|
if params[:password].empty?
|
||||||
|
@errors << OpenStruct.new(:attribute => :password, :type => :blank)
|
||||||
|
else
|
||||||
|
@user.password = hash_password(params[:password])
|
||||||
|
end
|
||||||
|
if @errors.empty? and @user.valid?
|
||||||
|
@user.save
|
||||||
|
session.clear
|
||||||
|
session[:user_id] = @user.id
|
||||||
|
redirect '/'
|
||||||
|
else
|
||||||
|
erb :signup
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/login' do
|
get '/login' do
|
||||||
|
|
@ -97,7 +111,7 @@ post '/login' do
|
||||||
session[:user_id] = user.id
|
session[:user_id] = user.id
|
||||||
redirect '/'
|
redirect '/'
|
||||||
else
|
else
|
||||||
@error = 'Username or password was incorrect'
|
@error = _("Incorrect email or password.")
|
||||||
erb :login
|
erb :login
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue