提问人:raja Abdullah 提问时间:5/17/2023 最后编辑:Arctodusraja Abdullah 更新时间:5/17/2023 访问量:54
Rails 重置密码链接问题
Rails reset password link issue
问:
我创建了一个忘记密码功能,我只使用rails api模式,我的rails版本是7.0.4.问题是当我在邮件中得到重置密码链接时,如果我点击它,我看不到任何页面没有显示任何内容,这应该显示密码重置的形式。
我的控制器
class PasswordResetsController < ApplicationController
def create
user = User.find_by(email: params[:email])
if user.present?
# user.send_reset_password_instructions
UserMailer.with(user: user).reset_instructions.deliver_now
render json: { message: 'Password reset instructions sent' }
else
render json: { message: 'User not found' }, status: :not_found
end
end
def update
@user = User.find_by(reset_password_token: params[:user][:reset_password_token])
if @user && @user.reset_password_sent_at > 2.hours.ago
if @user.update(password_params)
@user.update(reset_password_token: nil, reset_password_sent_at: nil)
redirect_to login_path, notice: 'Your password has been reset successfully'
else
render :edit
end
else
# Invalid reset password token
render json: { error: 'Invalid reset password token or token has expired' }, status: :unprocessable_entity
end
end
def edit
@user = User.find_by(reset_password_token: params[:reset_password_token])
if @user
# The reset password token is valid, render the password reset form
@reset_password_token = params[:reset_password_token]
render :edit
else
# Invalid reset password token
render json: { error: 'Invalid reset password token' }, status: :unprocessable_entity
end
end
private
def password_params
params.require(:user).permit(:password, :password_confirmation)
end
end
routes.rb 文件
get 'password/reset', to: 'password_resets#new', as: 'new_password_reset'
post 'password/reset', to: 'password_resets#create', as: 'create_password_reset'
get 'password/reset/edit/:reset_password_token', to: 'password_resets#edit', as: :edit_password_reset
patch 'password/reset/edit', to: 'password_resets#update', as: :password_reset
视图
<h1>Reset Password</h1>
<%= form_with(url: password_reset_path, method: :patch) do |form| %>
<%= form.hidden_field :reset_password_token, value: @reset_password_token %>
<div>
<%= form.label :password %>
<%= form.password_field :password %>
</div>
<div>
<%= form.label :password_confirmation %>
<%= form.password_field :password_confirmation %>
</div>
<%= form.submit 'Reset Password' %>
<% end %>
用户邮件
class UserMailer < ApplicationMailer
def reset_instructions
@user = params[:user]
@reset_password_link = edit_password_reset_url(@user.reset_password_token)
mail(to: @user.email, subject: 'Password reset instructions')
end
end
我尝试了很多解决方案,但没有这不起作用,我已经看到了我的控制台,但我没有错误,并且在服务器日志上它显示一切正常
Started GET "/password/reset/edit/848f365b993d2e6aef3cafcf42fdaf0234c09fe82a3026a828696112c06f2fb0" for 127.0.0.1 at 2023-05-17 12:13:28 +0500
Processing by PasswordResetsController#edit as HTML
Parameters: {"reset_password_token"=>"[FILTERED]"}
User Load (15.3ms) SELECT "users".* FROM "users" WHERE "users"."reset_password_token" = $1 LIMIT $2 [["reset_password_token", "[FILTERED]"], ["L
IMIT", 1]]
↳ app/controllers/password_resets_controller.rb:29:in `edit'
Completed 200 OK in 241ms (Views: 10.4ms | ActiveRecord: 15.3ms | Allocations: 610)
答: 暂无答案
评论