简体   繁体   中英

Devise sign_in(:user, @user) 401 Error

When a user creates a password update action is hit.

module Users
  class PasswordController < ApplicationController
    skip_before_action :authenticate_user!
    before_action :set_user

    def setup
      render file: "#{Rails.root}/public/404" unless @user&.sign_in_count&.zero?
    end

    def update
      if @user.update_attributes(password_params)
        sign_in(:user, @user)
        redirect_to root_path, notice: 'Setup complete'
      else
        flash[:alert] = errors(@user)
        render :setup
      end
    end

    private

    def password_params
      params.require(:user).permit(:password, :password_confirmation)
    end

    def set_user
      @user = User.find_by(confirmation_token: params[:token])
    end
  end
end

When the update action is hit.

This is what the console shows

Started PATCH "/users/password/wynEjv-E7uFp44EcwjbS" for ::1 at 2017-02-07 12:33:01 +1000
Processing by Users::PasswordController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"vIyPtidsM1GDNLh3n7gDDB2HzvR0QEEwAAvfMybNRn0/t4em4StiUVDyKwLC9i1OThoYguEae4T+xqLttjt1Pw==", "email"=>"test04@example.com", "user"=>{"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Continue", "token"=>"wynEjv-E7uFp44EcwjbS"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."confirmation_token" = $1 LIMIT $2  [["confirmation_token", "wynEjv-E7uFp44EcwjbS"], ["LIMIT", 1]]
   (0.1ms)  BEGIN
  SQL (0.5ms)  UPDATE "users" SET "encrypted_password" = $1, "updated_at" = $2 WHERE "users"."id" = $3  [["encrypted_password", "$2a$11$o5wmmxnCv.rlPha52GR0IOG4tbhEJYNNF9tctcSLDMS/dvLAU0hXq"], ["updated_at", 2017-02-07 02:33:01 UTC], ["id", 9]]
   (1.0ms)  COMMIT
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 9], ["LIMIT", 1]]
   (0.1ms)  BEGIN
   (0.2ms)  COMMIT
Completed 401 Unauthorized in 129ms (ActiveRecord: 2.5ms)

Does anyone know why the sign_in method isn't working here?

I used it on few apps and it worked then.

Thank you for any help in advance.

You can use it like this

@user.update_with_password(password_params)
sign_in @user, :bypass => true

Hope it will work for you as it worked for me!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM