Handling expired session in Phoenix

I want to know how to prevent this error which I get for requesting an expired session. What could be a good way to do that? I checked Custom Errors and Error Handling but not sure how helpful they are. Error message below.

ArgumentError at GET /svr/email/verify
cannot perform Monitor.Repo.get/2 because the given value is nil

lib/ecto/repo/queryable.ex
defp query_for_get(repo, _queryable, nil) do
  raise ArgumentError, "cannot perform #{inspect repo}.get/2 because the given value is nil"
end

Ecto.Repo.Queryable.query_for_get/3
  • First, a UUID is created and set in the session.

    uuid = SecureRandom.uuid
    SessionStore.set uuid, id, ttl: 20
    
  • Then below URL link is sent to the user and when the user clicks, it should check if the UUID is expired in session or not.

    <% verify_url = MyAppWeb.Router.Helpers.email_url(MyAppWeb.Endpoint, :verify, uuid: @uuid) %>
    
  • UUID is checked

    def verify(conn, %{"uuid" => uuid}) do
      user_id = SessionStore.get(uuid)
    end
1 Like

That error just means that uuid was nil and that you aren’t allowed to pass in nil values as a query get. Just need to check to make sure it is not nil first. :slight_smile:

1 Like

Right, thanks! Is this a good idea/practice to do like something below?

if SessionStore.get(uuid) == nil do
  IO.puts "uuid expired"
else
  user_id = SessionStore.get(uuid)
  user = Accounts.get_user!(user_id)
end

Well personally I’d use a case. ^.^;

case SessionStore.get(uuid) do
  nil -> ...
  uuid -> ...
end

Or if I were using the exceptional or ok libraries, then I’d just pipe it through those if the error case is as I already want.

3 Likes

Ah I see, neat!