gem 'bcrypt'
User.rb:
validates :email, presence: true, uniqueness: { case_sensitive: false }
validates :password, presence: true
validates_length_of :password, :minimum => 8
validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i,:if => lambda{ |object| object.email.present? }
require 'bcrypt'
# before_create :encrypt_password
before_save :encrypt_password, if: :password_changed? #will_save_change_to_password?
routes:
resources :users do
resources :comments
collection do
get :sign_in
post :session_create
delete :session_destroy
end
end
User_controller.rb:
def session_destroy
session[:user_id] = nil
redirect_to root_url, :notice => "Logged out"
end
def session_create
require 'bcrypt'
user = User.find_by_email(params[:user][:email])
password = params[:user][:password]
if user.present?
my_password = BCrypt::Password.new(user.password)
if my_password == password #=> true
session[:user_id] = user.id
redirect_to root_url, :notice => "Logged in!"
else
flash.now.alert = "Invalid email or password"
render "new"
end
else
redirect_to :back, :notice => "Email Not found, please enter correct email."
end
end
def sign_in
current_user = User.find_by_id(session[:user_id])
if current_user.present?
flash[:notice]= "you allready login"
redirect_to root_url
else
@user = User.new
end
end
User.rb:
validates :email, presence: true, uniqueness: { case_sensitive: false }
validates :password, presence: true
validates_length_of :password, :minimum => 8
validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i,:if => lambda{ |object| object.email.present? }
require 'bcrypt'
# before_create :encrypt_password
before_save :encrypt_password, if: :password_changed? #will_save_change_to_password?
private
def encrypt_password
self.password = BCrypt::Password.create(self.password)
end
def password_changed?
# will_save_change_to_password?
will_save_change_to_attribute?(:password)
end
# for This is after save
# after_save :encrypt_password
# def encrypt_password
# if saved_change_to_password?
# password = BCrypt::Password.create(self.password)
# self.update_column(:password, password)
# end
# end
routes:
resources :users do
resources :comments
collection do
get :sign_in
post :session_create
delete :session_destroy
end
end
User_controller.rb:
def session_destroy
session[:user_id] = nil
redirect_to root_url, :notice => "Logged out"
end
def session_create
require 'bcrypt'
user = User.find_by_email(params[:user][:email])
password = params[:user][:password]
if user.present?
my_password = BCrypt::Password.new(user.password)
if my_password == password #=> true
session[:user_id] = user.id
redirect_to root_url, :notice => "Logged in!"
else
flash.now.alert = "Invalid email or password"
render "new"
end
else
redirect_to :back, :notice => "Email Not found, please enter correct email."
end
end
def sign_in
current_user = User.find_by_id(session[:user_id])
if current_user.present?
flash[:notice]= "you allready login"
redirect_to root_url
else
@user = User.new
end
end