简体   繁体   English

Rails:输入正确的用户名/密码后,用户身份验证未重定向

[英]Rails: User Authentification not redirecting after correct username/password input

I have a rails app, for some reason my login action does not work. 我有一个Rails应用程序,由于某种原因,我的登录操作不起作用。 I put in a correct username/password in, however it does not redirect to the desired 'menu' action. 我输入了正确的用户名/密码,但是它没有重定向到所需的“菜单”操作。 It just redirects me to the login action everytime (I have set that to happen when the login is unsucessful). 它只是每次将我重定向到登录操作(我将其设置为在登录失败时发生)。 I state unless session[:user_id] . 我声明unless session[:user_id] When I input the wrong password on purpose, the flash message is correct, it says "Invalid Username/Password", when the correct one is input, it doesn't which means it recognises it, somehow the session is not being created. 当我故意输入错误的密码时,闪存消息是正确的,它说“ Invalid Username / Password”,当输入正确的密码时,它并不意味着它可以识别该会话,并且某种程度上也没有在创建会话。 Below is my code 下面是我的代码

Application Controller 应用控制器

protected
def confirm_logged_in
    unless session[:user_id]
        flash[:notice] = "Please Log In"
        redirect_to(:controller => 'access', :action => 'login')
        return false
    else
        return true
    end
end

Access Controller (Where the magic should be happening) 访问控制器(发生魔术的地方)

Class AccessController < ApplicationController

layout 'admin'

before_filter :confirm_logged_in, :except => [:login, :attempt_login, :logout]

def index
    menu
    render('menu')
end

def menu
    #display text & links
end

def login
    #login form
end

def attempt_login
    authorised_user = AdminUser.authenticate(params[:username], params[:password])
    if authorised_user
        flash[:notice] = "You are now logged in"
        redirect_to(:action => 'menu')
    else
        flash[:notice] = "Invalid username/password"
        redirect_to(:action => 'login')
    end
end

def logout
    session[:user_id] = nil
    session[:username] = nil
    flash[:notice] = "You have been logged out"
    redirect_to(:action => 'login')
end

end

AdminUser Model AdminUser模型

require 'digest/sha1'

class AdminUser < ActiveRecord::Base

# because we created a migration to change the name of the users tabe to admin_users we have   to specify
# set_table_name("admin_users")
# or we can change the class name and file name like we did
attr_accessible :first_name, :last_name, :username, :email 
attr_accessor :password
attr_protected :hashed_password, :salt

scope :named, lambda {|first,last| where(:first_name => first, :last_name => last)}

has_and_belongs_to_many :pages
has_many :section_edits
has_many :sections, :through => :section_edits

EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z)0-9.-]+\.[A-Z]{2,4}$/i

validates_presence_of :first_name
validates_presence_of :last_name
validates_presence_of :username

validates_length_of :first_name, :maximum => 25
validates_length_of :last_name, :maximum => 50
validates_length_of :username, :within => 3..25

validates_length_of :password, :within => 8..25, :on => :create

validates_uniqueness_of :username

validates :email, :presence => true, :length => {:maximum => 100}, :format => EMAIL_REGEX, :confirmation => true

before_save :create_hashed_password
after_save :clear_password

def self.authenticate(username="", password="")
user = AdminUser.find_by_username(username)
if user && user.password_match?(password)
  return user
else
  return false
end
end

def password_match?(password="")
hashed_password == AdminUser.hash_with_salt(password,salt) 
end

def self.make_salt(username="")
Digest::SHA1.hexdigest("User #{username} with #{Time.now} to make salt")
end

def self.hash_with_salt(password="", salt="")
Digest::SHA1.hexdigest("Put #{salt} on the #{password}")
end

private

def create_hashed_password
unless password.blank?
  self.salt = AdminUser.make_salt(username) if salt.blank?
  self.hashed_password = AdminUser.hash_with_salt(password,salt)
end
end

def clear_password
self.password = nil
end

end

I found the solution. 我找到了解决方案。 It was pretty simple. 这很简单。 The problem was that I did not create the sessions when the login was made, this is why the login did not recognise the sessions because they were not initialised. 问题是登录时我没有创建会话,这就是为什么登录因为未初始化而无法识别会话的原因。 In the Access Controller I simply changed it to this : 在访问控制器中,我只是将其更改为:

def attempt_login
    authorised_user = AdminUser.authenticate(params[:username], params[:password])
    if authorised_user
        session[:user_id] = authorised_user.id
        session[:username] = authorised_user.username
        flash[:notice] = "You are now logged in"
        redirect_to(:action => 'menu')
    else
        flash[:notice] = "Invalid username/password"
        redirect_to(:action => 'login')
    end
end

The amendments are the two session lines added to the code 修正是添加到代码中的两个会话行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on rails fatal error (failed password authentication for user ****) - Ruby on rails fatal error (failed password authentification for user ****) 登录后将用户重定向到同一页面(Rails) - Redirecting the user to the same page after login (Rails) Devise + Angular + Rails 4:更改已登录用户不验证用户名/密码 - Devise + Angular + Rails 4 : Changing Logged-in user not validating username/password 用户编辑密码后,Rails Devise重定向 - Rails Devise redirect after User edits their password Ruby on rails - 在创建 session 并重定向主页后用户为零 - Ruby on rails - user is nil after creating a session and redirecting home Rails SMTPAuthenticationError - 不接受用户名和密码 - Rails SMTPAuthenticationError - Username and Password not accepted 如何检查用户名和密码是否在数据库中可用或未从 Ruby on rails 中的邮递员获取输入 - how to check that if a username and password is available in database or not taking input from postman in Ruby on rails 在 Rails 上使用 Ruby - Watir 不会输入用户名、密码或单击登录按钮 - Using Ruby on Rails - Watir won't input username, password, or click the log in button 使用Oauth无需单击按钮即可自动为用户登录提供密码和用户名? (路轨) - Signin a user automatically given password and username without clicking on a button using Oauth? (Rails) “使用Capistrano部署Rails时,访问被拒绝用户&#39;用户名&#39;@&#39;localhost&#39;(使用密码:YES)” - “Access denied for user 'username'@'localhost' (using password: YES)” when deploying Rails with Capistrano
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM