2021-07-01 07:24:35 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2021-06-17 06:59:23 +00:00
|
|
|
class ForgotPasswordController < ApplicationController
|
|
|
|
|
skip_before_action :authenticate_request
|
|
|
|
|
|
|
|
|
|
def forgot
|
|
|
|
|
user = User.find_by(email: params[:_json])
|
|
|
|
|
if user.present?
|
|
|
|
|
user.send_password_reset
|
2021-07-01 07:24:35 +00:00
|
|
|
render json: { message: "We've sent the confirmation code to your email address" }, status: :ok
|
2021-06-17 06:59:23 +00:00
|
|
|
else
|
2021-07-01 07:24:35 +00:00
|
|
|
render json: { error: "Email address is not associated with a ToolJet cloud account." }, status: :not_found
|
2021-06-17 06:59:23 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def reset
|
|
|
|
|
user = User.find_by(forgot_password_token: params[:token])
|
|
|
|
|
if user.present? && user.forgot_password_token_valid?
|
|
|
|
|
if user.reset_password(params[:password])
|
2021-07-01 07:24:35 +00:00
|
|
|
render json: { message: "Your password has been successfuly reset!" }, status: :ok
|
2021-06-17 06:59:23 +00:00
|
|
|
else
|
2021-07-01 07:24:35 +00:00
|
|
|
render json: { error: user.errors.full_messages }, status: :unprocessable_entity
|
2021-06-17 06:59:23 +00:00
|
|
|
end
|
|
|
|
|
else
|
2021-07-01 07:24:35 +00:00
|
|
|
render json: { error: "Link not valid or expired." }, status: :not_found
|
2021-06-17 06:59:23 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|