我正在使用一个名为:devise-jwt
当我尝试重置 rails API 项目的密码时,当我成功更改密码时它不会发回 JWT 令牌。
一切正常,但在密码重置/更新功能上,我在成功更新密码后从未获得授权令牌。
更新方法代码:
class Api::V1::Patients::PasswordsController < Devise::PasswordsController
respond_to :json
skip_before_action :verify_authenticity_token
def update
self.resource = resource_class.reset_password_by_token(params)
if resource.errors.empty?
bypass_sign_in(resource, scope: :patient)
render json: resource, root: 'data', each_serializer: PatientSerializer, message: 'Password updated successfully', adapter: :json, status: :ok
else
render json: { error: resource.errors.full_messages }, status: :not_found
end
end
private
def resource_params
params.require(:patient).permit(:email, :password, :password_confirmation)
end
end
路线:
namespace :api, defaults: { format: :json } do
namespace :v1 do
devise_for :patients, controllers: {
sessions: 'api/v1/patients/sessions',
registrations: 'api/v1/patients/registrations',
passwords: 'api/v1/patients/passwords'
}
end
end
配置:
config.jwt do |jwt|
jwt.secret = ENV['DEVISE_JWT_SECRET_KEY']
jwt.expiration_time = 30.day.to_i
end
设计配置:
config.sign_in_after_reset_password = true
日志:
Started PUT "/api/v1/patients/password" for ::1 at 2022-03-05 10:51:14 +0500
Processing by Api::V1::Patients::PasswordsController#update as JSON
Parameters: {"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "reset_password_token"=>"[FILTERED]"}
Patient Load (38.1ms) SELECT "patients".* FROM "patients" WHERE "patients"."reset_password_token" = $1 ORDER BY "patients"."id" ASC LIMIT $2 [["reset_password_token", "32bbaa6ea73b7a49d1abb84c0756b4666f12f95c42c27218d3150b0204e1e909"], ["LIMIT", 1]]
↳ app/controllers/api/v1/patients/passwords_controller.rb:30:in `update'
TRANSACTION (387.7ms) BEGIN
↳ app/controllers/api/v1/patients/passwords_controller.rb:30:in `update'
Patient Update (225.8ms) UPDATE "patients" SET "encrypted_password" = $1, "reset_password_token" = $2, "reset_password_sent_at" = $3, "updated_at" = $4 WHERE "patients"."id" = $5 [["encrypted_password", "$2a$12$XYIP61byGk5XgROhYDP8WuEfn698HTRL/Rv2Y3K/Okt7esT5NyVTW"], ["reset_password_token", nil], ["reset_password_sent_at", nil], ["updated_at", "2022-03-05 05:51:15.618405"], ["id", 2]]
↳ app/controllers/api/v1/patients/passwords_controller.rb:30:in `update'
TRANSACTION (100.8ms) COMMIT
↳ app/controllers/api/v1/patients/passwords_controller.rb:30:in `update'
[active_model_serializers] Address Load (48.1ms) SELECT "addresses".* FROM "addresses" WHERE "addresses"."addressable_id" = $1 AND "addresses"."addressable_type" = $2 LIMIT $3 [["addressable_id", 2], ["addressable_type", "Patient"], ["LIMIT", 1]]
[active_model_serializers] ↳ app/controllers/api/v1/patients/passwords_controller.rb:33:in `update'
[active_model_serializers] Rendered PatientSerializer with ActiveModelSerializers::Adapter::Json (1493.45ms)
Completed 200 OK in 6337ms (Views: 829.0ms | ActiveRecord: 1504.4ms | Allocations: 12243320)