【问题标题】:Handling expired session in phoenix处理phoenix中的过期会话
【发布时间】:2018-06-06 02:00:39
【问题描述】:

我想知道如何防止在请求过期会话时出现此错误。有什么好方法可以做到这一点?我检查了Custom Errors,但不确定它有多大帮助。

  • 首先,在会话中创建并设置一个 UUID。

    uuid = SecureRandom.uuid
    SessionStore.set uuid, id, ttl: 20
    
  • 然后将下面的 URL 链接发送给用户,当用户点击时,它应该检查 UUID 是否在会话中过期。

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

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

【问题讨论】:

    标签: session phoenix-framework


    【解决方案1】:

    我只是先检查 nil 值。我也可以使用here 中建议的案例。

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

    【讨论】:

      猜你喜欢
      • 2012-11-30
      • 2014-08-30
      • 2018-04-29
      • 1970-01-01
      • 2011-01-20
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      • 1970-01-01
      相关资源
      最近更新 更多