I’m exploring using Cachex
to implement a simple one time password (OTP). In its simplest form, there is a custom Plug that looks like the following:
defmodule MyAppWeb.OTP do
import Plug.Conn
def init(options), do: options
def call(%Plug.Conn{path_params: path_params} = conn, opts) do
access_code = path_params["code"]
case Cachex.get(:passcode, :"#{access_code}") do
{:ok, %{user_id: user_id}} -> ...
_ ->
conn |> send_resp(:unauthorized, "") |> halt()
end
end
end
The issue here is that Cachex.get(:passcode, :"#{access_code}"
will always give me nil
. Similarly, if I use Cachex.keys(:passcode)
, it will return an empty list while I’m sure there are keys that I had added via iex
.
Here’s how Cachex
is initialised in application.ex
def start(_type, _args) do
children = [
# Start the Telemetry supervisor
MyAppWeb.Telemetry,
...
%{id: :cachex_passcode, start: {Cachex, :start_link, [[name: :passcode]]}},
MyAppWeb.Endpoint
]
...
I have a feeling there is something missing in the setup?