Commit 4f2740a
Changed files (6)
app
controllers
models
views
registrations
sessions
config
locales
spec
controllers
app/controllers/sessions_controller.rb
@@ -3,10 +3,15 @@ class SessionsController < ApplicationController
layout "public"
def create
- session[:user_id] = User.authenticate(params[:email], params[:password]).id
+ session[:user_id] = User.authenticate(params[:username], params[:password]).id
render :nothing => true
end
+ def new
+ @user = User.new
+
+ end
+
def destroy
reset_session()
redirect_to root_path
app/models/user.rb
@@ -6,8 +6,8 @@ class User < ActiveRecord::Base
validates :email, presence: true, email: true, uniqueness: true
validates_acceptance_of :terms_and_conditions
- def self.authenticate(email,password)
- if user = User.find_by(email: email)
+ def self.authenticate(username,password)
+ if user = User.find_by(email: username) || User.find_by(username: username)
user.authenticate(password)
end
end
app/views/registrations/new.html.erb
@@ -23,6 +23,7 @@
</div> <!-- /.small-12 -->
<div class="small-12 columns">
<%= f.submit t('.register_button'), class: "button" %>
+ <p><%= link_to t('.login_link'), new_session_path, class: "button secondary" %></p>
</div> <!-- /.small-12 -->
<% end %>
</div> <!-- /.row -->
app/views/sessions/new.html.erb
@@ -1,1 +1,18 @@
-<%= link_to t('.register_button'), new_registration_path, class: "button secondary" %>
\ No newline at end of file
+<div class="row">
+<%= form_for @user, url: sessions_path do |f| %>
+ <div class="small-12 columns">
+ <label><%= t('.username') %>
+ <%= f.text_field :username, placeholder: t('.username') %>
+ </label>
+ </div> <!-- /.small-12 -->
+ <div class="small-12 columns">
+ <label><%= t('.password') %>
+ <%= f.password_field :password, placeholder: t('.password') %>
+ </label>
+ </div> <!-- /.small-12 -->
+ <div class="small-12 columns">
+ <%= f.submit t('.login_button'), class: "button" %>
+ <%= link_to t('.register_link'), new_registration_path, class: "button secondary" %>
+ </div> <!-- /.small-12 -->
+<% end %>
+</div> <!-- /.row -->
\ No newline at end of file
config/locales/en.yml
@@ -28,6 +28,10 @@ en:
terms_and_conditions: "I agree to the"
terms_and_conditions_link: "Terms and Conditions of Use"
register_button: "Register"
+ login_link: "Already have an account?"
sessions:
new:
- register_button: "Register"
+ username: "Username (or email)"
+ password: "Password"
+ login_button: "Login"
+ register_link: "Create an account"
spec/controllers/sessions_controller_spec.rb
@@ -7,8 +7,13 @@ describe SessionsController do
let(:user) { create(:user, password: "password") }
- it "logs you in" do
- post :create, { email: user.email, password: "password" }
+ it "logs you in with email" do
+ post :create, { username: user.email, password: "password" }
+ expect(session[:user_id]).to eql(user.id)
+ end
+
+ it "logs you in with username" do
+ post :create, { username: user.username, password: "password" }
expect(session[:user_id]).to eql(user.id)
end