ruby on rails - Having Log in & Session Errors -
first i'd let know can create new user , start session new user. cannot log in new user.
not distract answering think error within authenticating user.
currently if in sessions controller. if have below.
user = user.find_by(params[:email]) if user && user.authenticate(params[:session][:password])
i redirected login page because password doesn't match. can login if have below. not start session correct user.
user = user.find_by(params[:email]) if user
again can create new users, user count increase, cannot log in user once end session.
class sessionscontroller < applicationcontroller def new end def create user = user.find_by(params[:email]) if user && user.authenticate(params[:session]) session[:user_id] = user.id case user.admin_level when 1 session[:site_admin] = true session[:can_create_post] = true when 2 session[:creator] = true session[:can_create_post] = true when 3 session[:friend] = true session[:can_comment_on_post] = true end if params[:remember_me] cookies.permanent[:auth_token] = user.auth_token else cookies[:auth_token] = user.auth_token end log_in user redirect_to user, :notice => "logged in!" else flash.now[:danger] = 'invalid email/password combination' # not quite right! redirect_to root_url end end def loggedin? @user = session[:user_id] redirect_to page_path(current_user) end def destroy log_out if logged_in? cookies.delete(:auth_token) session.clear redirect_to root_url, :notice => "logged out!" end def session_params params.require(:session).permit(:user_id) end end
my user model looks below
class user < activerecord::base attr_accessor :remember_token, :image before_create { generate_token(:auth_token) } mount_uploader :image, imageuploader mount_uploader :avatar, avataruploader validates :name, presence: true, length: { maximum: 50 } before_save { email.downcase! } before_save { name.capitalize! } valid_email_regex = /\a[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :email, presence: true , length: { maximum: 255 }, format: {with: valid_email_regex }, uniqueness: { case_sensitive: false } has_secure_password validates :password, length: { minimum: 6 }, allow_blank: true def user.digest(string) cost = activemodel::securepassword.min_cost ? bcrypt::engine::min_cost : bcrypt::engine.cost bcrypt::password.create(string, cost: cost) end def user.new_token securerandom.urlsafe_base64 end def remember self.remember_token = user.new_token update_attribute(:remember_digest, user.digest(remember_token)) end def forget update_attribute(:remember_digest, nil) end def authenticated?(remember_token) bcrypt::password.new(remember_digest).is_password?(remember_token) end def generate_token(column) begin self[column] = securerandom.urlsafe_base64 end while user.exists?(column => self[column]) end def authenticated?(remember_token) bcrypt::password.new(remember_digest).is_password?(remember_token) end end
Comments
Post a Comment