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>
|
||||
|
||||
<% if @error %>
|
||||
<p class="error"><%= @error %></p>
|
||||
<% end %>
|
||||
|
||||
<form action="/login" method="POST">
|
||||
<p>
|
||||
<label for="email"><%= _("Email") %></label>
|
||||
|
|
|
|||
|
|
@ -1,13 +1,32 @@
|
|||
<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">
|
||||
<p>
|
||||
<label for="email"><%= _("Email") %></label>
|
||||
<input type="text" name="email">
|
||||
<input type="text" name="email" value="<%= params[:email] %>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="password"><%= _("Password") %></label>
|
||||
<input type="password" name="password">
|
||||
<input type="password" name="password" value="<%= params[:password] %>">
|
||||
</p>
|
||||
<button type="submit"><%= _("Create account") %></button>
|
||||
</form>
|
||||
|
|
|
|||
20
vote.rb
20
vote.rb
|
|
@ -28,6 +28,8 @@ class User < ActiveRecord::Base
|
|||
has_many :ratings
|
||||
has_many :organizers
|
||||
has_many :votes, through: :organizers
|
||||
validates :email, uniqueness: true
|
||||
validates :email, format: URI::MailTo::EMAIL_REGEXP
|
||||
end
|
||||
|
||||
class Organizer < ActiveRecord::Base
|
||||
|
|
@ -81,9 +83,21 @@ get '/signup' do
|
|||
end
|
||||
|
||||
post '/signup' do
|
||||
@user = User.create(email: params[:email],
|
||||
password: hash_password(params[:password]))
|
||||
@user = User.create(email: params[:email])
|
||||
@errors = []
|
||||
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
|
||||
|
||||
get '/login' do
|
||||
|
|
@ -97,7 +111,7 @@ post '/login' do
|
|||
session[:user_id] = user.id
|
||||
redirect '/'
|
||||
else
|
||||
@error = 'Username or password was incorrect'
|
||||
@error = _("Incorrect email or password.")
|
||||
erb :login
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue