2024-11-18 23:27:50 +00:00
|
|
|
# Copyright (C) 2024 Manuel Bustillo
|
|
|
|
|
2024-11-19 00:26:44 +01:00
|
|
|
class PasswordsController < ApplicationController
|
|
|
|
allow_unauthenticated_access
|
2024-11-19 00:32:24 +01:00
|
|
|
before_action :set_user_by_token, only: :update
|
2024-11-19 00:26:44 +01:00
|
|
|
|
|
|
|
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
|
2024-11-19 00:26:44 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
if @user.update(params.permit(:password, :password_confirmation))
|
2024-11-19 00:32:24 +01:00
|
|
|
render json: {}, status: :ok
|
2024-11-19 00:26:44 +01:00
|
|
|
else
|
2024-11-19 00:32:24 +01:00
|
|
|
render json: { errors: @user.errors.full_messages }, status: :unprocessable_entity
|
2024-11-19 00:26:44 +01:00
|
|
|
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
|
2024-11-19 00:26:44 +01:00
|
|
|
end
|