1

Guardian を使用してトークンを生成し、Phoenix Channels のソケットに接続する際の認証に使用します。

最近、一部のユーザーが特定のページから離れず、約 1 か月後にトークンが無効になり、Phoenix Channels の接続試行が無効になることがわかりました。

クライアント側でそのようなケースをどのように処理しますか? フロントエンドに理由を知らせるために Phoenix から返される特定のエラーはありますか? connectの関数はuser_socket.ex次のようになります。

def connect(%{"guardian_token" => token}, socket) do
  case Guardian.Phoenix.Socket.authenticate(socket, MyApp.Guardian, token) do
    {:ok, authed_socket} ->
      {:ok, authed_socket}

    {:error, _} ->
      :error
  end
end

Phoenix Channels の Phoenix JS ライブラリを使用してこのエラーをキャプチャする方法はありますか? 私たちの目標は、1) トークンの有効期限が切れた場合に再試行を停止すること、2) ユーザーをログアウトするか、ユーザーがオフラインであるというメッセージを表示することです。Phoenix JS のドキュメントを確認しましたが、適切なものが見つかりませんでした。

4

2 に答える 2

0

You can try to refresh the tokens with each connect or whenever you see fit

Maybe something like this

# Refresh a token and set it on connect
def connect(%{"guardian_token" => token}, socket) do

  case MyApp.Guardian.refresh(token) do

    {:ok, _old_stuff, {new_token, new_claims}} -> 
      case Guardian.Phoenix.Socket.authenticate(socket, MyApp.Guardian, new_token, new_claims) do
        {:ok, authed_socket} ->
          {:ok, authed_socket}
        {:error, _} ->
          :socket_auth_failed
      end

    _ ->
      {:token_refresh_failed, "could not refresh token"}
  end
end

于 2019-02-14T07:31:46.000 に答える