使用 Timex 设置验证超时 link

Set timeout for verification link using Timex

我在将验证保存到我的数据库后通过电子邮件发送验证 link。我希望 link 有一个超时,即它会在一段时间后失效。我如何使用 Timex 这样做?我不想使用 Coherence 等其他身份验证包。

您可能根本不需要 Timex,现在假设您的数据库使用的是原始时区,您可以使用

# 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