wedding-planner/app/controllers/passwords_controller.rb

31 lines
859 B
Ruby
Raw Normal View History

2024-11-18 23:27:50 +00:00
# Copyright (C) 2024 Manuel Bustillo
class PasswordsController < ApplicationController
allow_unauthenticated_access
2024-11-19 00:32:24 +01:00
before_action :set_user_by_token, only: :update
def create
if user = User.find_by(email_address: params[:email_address])
PasswordsMailer.reset(user).deliver_later
end
2024-11-19 08:56:51 +01:00
render json: {}, status: :created
end
def update
if @user.update(params.permit(:password, :password_confirmation))
2024-11-19 00:32:24 +01:00
render json: {}, status: :ok
else
2024-11-19 00:32:24 +01:00
render json: { errors: @user.errors.full_messages }, status: :unprocessable_entity
end
end
private
2024-11-19 00:32:24 +01:00
def set_user_by_token
@user = User.find_by_password_reset_token!(params[:token])
rescue ActiveSupport::MessageVerifier::InvalidSignature
2024-11-19 08:56:51 +01:00
render json: { errors: ['Password reset link is invalid or has expired.'] }, status: :unprocessable_entity
2024-11-19 00:32:24 +01:00
end
end