首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在我更改此用户密码并重新登录后,Devise将current_user设置为nil

在我更改此用户密码并重新登录后,Devise将current_user设置为nil
EN

Stack Overflow用户
提问于 2019-05-29 01:52:52
回答 2查看 431关注 0票数 1

下面是我尝试在控制器中使用的代码:

profiles_controller.rb:

class ProfilesController < ApplicationController
  ...


  def update

    respond_to do |format|
      # assume valid data sent (I've already tested for this)
      if @user.update(user_params)
        # password_reset? check's parameter passed to action that a check box was
        # checked (which enables/disables password/confirmation fields. If unchecked,
        # fields are disabled and no password parameters are sent to this action.
        # @user was set to current_user in a before_action already
        # inspecting @user at this point returns the same thing as current_user here
        sign_in(:user, @user) if password_reset?
        # current_user is still set to @user and is valid

        # after redirection current_user becomes nil
        format.html {
          redirect_to home_path, notice: 'Your profile was successfully updated.'
        }
      else
        format.html { render :edit }
      end
    end
  end
  ...
private
  # Never trust parameters from the scary internet, only allow the white list through.
  def user_params
    @user_params ||= params.require(:user).permit(:first_name, :last_name, :email, :phone, :password, :password_confirmation, :reset_password)
  end

  def password_reset?
    @user_params["reset_password"] == "1"
  end
end

application_controller.rb:

class ApplicationController < ActionController::Base
...

private
...
  def require_user
    logger.debug "IN REQUIRE_USER, CURRENT_USER IS: #{current_user.inspect}"
    unless current_user
      store_location
      redirect_to new_user_session_url, notice: "That url doesn't exist."
      return false
    end
  end

  def require_admin
    # this line will actually log a user in
    #sign_in(:user, User.first) unless current_user
    logger.debug "IN REQUIRE_ADMIN, CURRENT_USER IS: #{current_user.inspect}"
    unless current_user && current_user.is_admin?
      redirect_to(home_path, notice: "That url doesn't exist.") and return false
    end
  end
...
end

development.log:

Started PATCH "/profile" for 127.0.0.1 at 2019-05-28 10:38:45 -0700
Processing by ProfilesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"....", "user"=>{....}, "commit"=>"Update Profile"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/application_controller.rb:47
IN REQUIRE_USER, CURRENT_USER IS: #<User id: 1 ....>
   (0.1ms)  begin transaction
  ↳ app/controllers/profiles_controller.rb:16
  User Exists (0.4ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER(?) AND "users"."id" != ? LIMIT ?  [["email", "...."], ["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/profiles_controller.rb:16
  User Update (0.3ms)  UPDATE "users" SET "encrypted_password" = ?, "updated_at" = ? WHERE "users"."id" = ?  [["encrypted_password", "$2a$11...."], ["updated_at", "2019-05-28 17:38:45.346414"], ["id", 1]]
  ↳ app/controllers/profiles_controller.rb:16
   (2.3ms)  commit transaction
  ↳ app/controllers/profiles_controller.rb:16
PASSWORDS PASSED IN SO USER PASSWORD CHANGE OCCURRED
REDIRECTING TO HOME PATH
Redirected to http://localhost:3000/admin
Completed 302 Found in 121ms (ActiveRecord: 3.2ms)


Started GET "/admin" for 127.0.0.1 at 2019-05-28 10:38:45 -0700
Processing by Admin::PagesController#index as HTML
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/application_controller.rb:65
IN REQUIRE_ADMIN, CURRENT_USER IS: nil
Redirected to http://localhost:3000/
Filter chain halted as :require_admin rendered or redirected
Completed 302 Found in 2ms (ActiveRecord: 0.2ms)


Started GET "/" for 127.0.0.1 at 2019-05-28 10:38:45 -0700
Processing by PagesController#index as HTML
  Rendering pages/index.html.erb within layouts/application
  Rendered pages/index.html.erb within layouts/application (0.7ms)
  Rendered application/_navigation.html.erb (1.7ms)
  Rendered application/_alert.html.erb (0.3ms)
Completed 200 OK in 1152ms (Views: 1151.2ms | ActiveRecord: 0.0ms)

我到处搜索,看到by_pass: true被传递给sign_in,但这没有帮助。一旦我让用户登录(@current_user是Devise控制器btw的直接实例变量),我也尝试过@current_user = @user,但这也没有帮助。

有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-29 03:21:03

Devise忽略登录如果用户已经登录,请尝试:

if @user.saved_change_to_encrypted_password? # for rails 5+, for previous - .encrypted_password_changed?
  sign_in @user, force: true
end
票数 2
EN

Stack Overflow用户

发布于 2019-10-07 13:16:54

如果用户已经登录,则可以登录新会话。

Devise说

# Sign in a user bypassing the warden callbacks and stores the user
# straight in session. This option is useful in cases the user is 
# signed in, but we want to refresh the credentials in session.

请像下面这样使用。

bypass_sign_in(@user)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56347661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档