elixirphoenix-frameworkectotimex

Set timeout for verification link using Timex


I am sending a verification link via email after saving it to my database. I want the link to have a timeout, i.e., it would be rendered invalid after a certain duration. How do I do so using Timex? I don't want to use other authentication packages like Coherence.


Solution

  • You may not need Timex at all, now assuming your DB is using naive time zone, you can use

    # Assuming the link will expire in an hour (3600 seconds)
    valid_till = NaiveDateTime.add(NaiveDateTime.utc_now(), 3600)
    
    # Sends the verification mail
    ...
    
    # Save the valid_till somewhere in the database and when user tries to use the link to verify themselves compare the current timestamp against the stored valid_till
    if NaiveDateTime.utc_now > stored_valid_till, do: false