Phoenix:授权经过身份验证的用户加入频道

Phoenix: Authorizing an authenticated user to join a channel

在我的新 HelloPhoenix 应用程序中,我使用 register/login 表单构建了一个标准的基本 http 用户身份验证。

接下来,我尝试了频道。它很好用。然后,我希望通过身份验证的用户(并且只有那些已经通过身份验证的用户)能够加入某些频道。根据文档:

Clients must join a channel to send and receive PubSub events on that channel. Your channels must implement a join/3 callback that authorizes the socket for the given topic. For example, you could check if the user is allowed to join that particular room.

这听起来不错,但我想知道最有效的方法是什么。我想在身份验证时返回一个令牌(与用户的模型相关联),因此令牌被传回 WebSocket。 join/3 回调可以验证此标记是否与模型中的标记匹配。

这是最有效的方法吗?

我想我找到了答案。 Phoenix.Token 非常适合我想要实现的目标。