Hello am still new to Elixir and phoenix i want to use nimble_topt to generate and send it to a user while registering
defp generate_otp(user) do
secret = NimbleTOTP.secret()
otp_code = NimbleTOTP.verification_code(secret)
case Auth.create_opt(%{"user_id" => user.id, "otp_code" => otp_code}) do
{:ok, _created_opt} ->
IO.puts("OTP generated and saved successfully #{otp_code}")
{:error, reason} ->
IO.puts("Error generating OTP: #{reason}")
end
end
then call to store it in the database
def create_opt(attrs) do
Otp.changeset(%Otp{}, attrs)
|> Repo.insert()
end
then I want to verify as follows
case Auth.get_user_recent_otp(user.id) do
nil ->
{:noreply,
socket
|> put_flash(:error, "Something went wrong")}
otp_data when otp_data.otp_status ->
{:noreply,
socket
|> put_flash(:error, "OTP already used or invalid")}
otp_data ->
IO.inspect(otp_data)
IO.inspect(otp_code)
if NimbleTOTP.valid?(otp_data, otp_code) do
Accounts.update_phone_confirmed(user)
{
:noreply,
socket
|> assign(user_id: user.id)
|> put_flash(:info, "successfully verified phone number")
|> push_navigate(to: ~p"/register/account-manager?user_id=#{user.id}")
}
else
{:noreply,
socket
|> put_flash(:error, "Invalid OTP")}
end
end
However, in the docs I see that NimbleTOTP.valid? checks with the secret and not the code sent
How will I convert the string code from my db back to binary which matches the
Kindly assist