将Phoenix.Token传入和传出浏览器

时间:2016-12-09 06:10:32

标签: authentication elixir phoenix-framework

我正在尝试使用Openmaize进行用户身份验证,并且在用户登录时难以让phoenix传递令牌。似乎没有分配令牌并传递给客户端,因此Phoenix.Token.verify失败。

IO.inspect(socket)中的

UserSocket.connect会返回此信息。

Phoenix.Socket{assigns: %{}, channel: nil, channel_pid: nil,
     endpoint: SeatSaver.Endpoint, handler: SeatSaver.UserSocket, id: nil,
     joined: false, pubsub_server: SeatSaver.PubSub, ref: nil,
     serializer: Phoenix.Transports.WebSocketSerializer, topic: nil,
     transport: Phoenix.Transports.WebSocket, transport_name: :websocket,
     transport_pid: #PID<0.2098.0>}

我在authenticate.ex控制器中定义了set_current_user(user, conn)函数,看起来像

  defp set_current_user(user, conn) do 
    token = Phoenix.Token.sign(conn, "user socket", user.id)
    conn
    |> assign(:current_user, user)
    |> assign(:user_token, token)

在app.html.eex中,添加了以下内容。

<script> window.userToken = “<%= assigns[:user_token] %>” </script>
<script src = “<%= static_path(@conn, “/js/app.js”) %>”></script>
<{1>}中的

app.js

最后,user_socket.ex有

let socket = new Socket(”/socket”, {
   params: {token: window.userToken},
   …
})

1 个答案:

答案 0 :(得分:0)

首先,您需要在config / config.exs中添加secret_key_base。

<!DOCTYPE html> <html lang="en"> <head> <title>Pure CSS Horizontal Slider</title> <meta name="viewport" content="width=device-width, initial-scale=" 1 "/> <link rel="stylesheet " type="text/css " href="main1.css " /> </head> <body> <div class="wrap "> <header> <label for="slide-1-trigger ">Slide One</label> <label for="slide-2-trigger ">Slide Two</label> <label for="slide-3-trigger ">Slide Three</label> <label for="slide-4-trigger ">Slide Four</label> </header> <input id="slide-1-trigger " type="radio " name="slides " checked/> <section class="slide slide-one "> <h1>Headline One</h1> <video width="250 " height="170 " controls> <source src="jupitergif.webm " type="video/webm "> </source> </video> </section> <input id="slide-2-trigger " type="radio " name="slides " /> <section class="slide slide-two "> <h1>Headline Two</h1> </section> <input id="slide-3-trigger " type="radio " name="slides " /> <section class="slide slide-three "> <h1>Headline Three</h1> </section> <input id="slide-4-trigger " type="radio " name="slides " /> <section class="slide slide-four "> <h1>Headline Four</h1> </section> </div> </body> </html>