Created
January 7, 2013 02:54
-
-
Save sirbrillig/4471930 to your computer and use it in GitHub Desktop.
Adding Users to a rails app using has_secure_password.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
protect_from_forgery | |
private | |
def current_user | |
@current_user ||= User.find(session[:user_id]) if session[:user_id] | |
end | |
helper_method :current_user | |
def authenticate_user | |
return redirect_to root_url unless current_user | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateUsers < ActiveRecord::Migration | |
def change | |
create_table :users do |t| | |
t.string :email | |
t.string :password_digest | |
t.timestamps | |
add_column :items, :user_id, :integer | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ItemsController < ApplicationController | |
before_filter :authenticate_user | |
... | |
def edit | |
@item = Item.where(id: params[:id], user_id: current_user.id).first | |
end | |
... | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
resources :users do | |
collection do | |
get 'login' | |
post 'login' | |
get 'logout' | |
end | |
end | |
end | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
attr_accessible :email, :password, :password_confirmation | |
has_secure_password | |
validates :password, :presence => { :on => :create } | |
validates :email, :presence => { :on => :create }, :uniqueness => { :case_sensitive => false } | |
has_many :items, :dependent => :destroy | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersController < ApplicationController | |
before_filter :authenticate_user, except: [:new, :create, :login] | |
def new | |
@user = User.new | |
respond_to do |format| | |
format.html | |
format.json { render json: @user } | |
end | |
end | |
def create | |
@user = User.new(params[:user]) | |
respond_to do |format| | |
if @user.save | |
session[:user_id] = @user.id | |
format.html { redirect_to root_url, notice: 'User was successfully created.' } | |
format.json { render json: @user, status: :created } | |
else | |
format.html { render action: "new" } | |
format.json { render json: @user.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
def edit | |
@user = User.find(params[:id]) | |
end | |
def update | |
@user = User.find(params[:id]) | |
respond_to do |format| | |
if @user.update_attributes(params[:user]) | |
format.html { redirect_to root_url, notice: 'User was successfully updated.' } | |
format.json { head :no_content } | |
else | |
format.html { render action: "edit" } | |
format.json { render json: @user.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
def login | |
if request.post? | |
user = User.find_by_email(params[:user][:email]) | |
if user && user.authenticate(params[:user][:password]) | |
session[:user_id] = user.id | |
redirect_to root_url, :notice => "You are now logged in!" | |
else | |
flash.now[:error] = "Invalid email or password." | |
@user = User.new | |
respond_to do |format| | |
format.html | |
end | |
end | |
else | |
@user = User.new | |
respond_to do |format| | |
format.html | |
end | |
end | |
end | |
def logout | |
session[:user_id] = nil | |
redirect_to root_url, :notice => "You are now logged out." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment