简体   繁体   English

elixir phoenix liveview - 通过套接字传递用户 ID

[英]elixir phoenix liveview - passing user id through socket

In liveview , how can I pass the user data from leex to the context ?liveview中,如何将用户数据从leex传递到context I have phx.gen.live a profiles context, and I want to add user_id to the profile every time user create the new profile.我有phx.gen.live profiles上下文,并且我想在每次用户创建新配置文件时将 user_id 添加到配置文件中。 I change the create_profile code to:我将 create_profile 代码更改为:

**profiles.ex (context)**
  def create_profile(attrs \\ %{}, userid) do
    attrs = Map.put(attrs, "user_id", userid)
    %Profile{}
    |> Profile.changeset(attrs)
    |> Repo.insert()
  end

I am using pow , so in normal phoenix case, I would just do this:我正在使用pow ,所以在正常的 phoenix 情况下,我会这样做:

user = Pow.Plug.current_user(conn) #<-- this is conn
Profiles.create_profile(profile_params, user.id)

but in liveview , instead of conn , it use socket .但在liveview中,它使用的是socket而不是conn So I am not sure how to go about it.所以我不确定如何 go 关于它。

There are lots of different ways to do this but I will describe a straightforward approach.有很多不同的方法可以做到这一点,但我将描述一个简单的方法。

  1. In the function where you "log in" a user, I assume you have access to the conn .在您“登录”用户的 function 中,我假设您可以访问conn I also assume you have a user_token , if you are using Pow.如果您使用 Pow,我还假设您有一个user_token If so, do this:如果是这样,请执行以下操作:
conn
|> put_session(:user_token, user_token)
  1. Now go to your live_helpers.ex file (or create it if you don't have one) and make a function like this:现在 go 到你的live_helpers.ex文件(或者如果你没有的话创建它)并像这样制作一个 function :
  def assign_defaults(session, socket) do
    socket =
      socket
      |> assign_new(:current_user, fn ->
        find_current_user(session)
      end)

    socket
  end
  1. Also, in live_helpers , write this function:另外,在live_helpers中,写这个 function:
  defp find_current_user(session) do
    with user_token when not is_nil(user_token) <- session["user_token"],
         %User{} = user <- Accounts.get_user_by_session_token(user_token),
         do: user
  end
  1. Now in any LiveView modules where you need to access the current_user, just put this in your mount function:现在在您需要访问 current_user 的任何 LiveView 模块中,只需将其放入您的mount function 中:
  def mount(_params, session, socket) do
    socket =
      assign_defaults(session, socket)

    {:ok, socket}
  end

And then you will have access to the session.assigns.current_user .然后您将可以访问session.assigns.current_user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Phoenix LiveView heex 模板调用 Elixir function 并传递来自套接字分配的数据 arguments? - How do I call an Elixir function from a Phoenix LiveView heex template passing arguments with data from socket assigns? Elixir Phoenix LiveView Echart 刚刚消失(消失) - Elixir Phoenix LiveView Echart just vanishes (disappears) Elixir phoenix LiveView 可折叠在更新时折叠 - Elixir phoenix LiveView collapsible collapses on update Elixir / Phoenix LiveView:如何向Rollbar报告异常? - Elixir / Phoenix LiveView: How can I report exceptions to Rollbar? 如何将 LiveView 添加到现有的 Elixir/Phoenix 应用程序? - How can I add LiveView to an existing Elixir/Phoenix app? Elixir / Phoenix通过hidden_​​input传递当前日期 - Elixir/Phoenix passing current date through hidden_input 查询记录上的ID-Phoenix和Elixir - Query for an ID on a record - Phoenix and Elixir 在挂载期间使用 LiveView 将值从客户端传递到 Phoenix 服务器 - Passing a value from client to Phoenix server using LiveView during mount 如何在我的 Elixir Phoenix LiveView 应用程序 header 中混合实时和“死”内容? - How do I mix live and "dead" content in my Elixir Phoenix LiveView app header? 从“凤凰”功能导入{Socket,Presence}不起作用。 凤凰/药剂 - import {Socket, Presence} from “phoenix” function not working. Phoenix/Elixir
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM