Commit 34e1fb76 authored by Jens-Christian Fischer's avatar Jens-Christian Fischer
Browse files

Added user identification

storing a UUID for each new user in the socket
parent 087d2926
......@@ -38,6 +38,7 @@ defmodule Grains.Mixfile do
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.11"},
{:cowboy, "~> 1.0"},
{ :uuid, "~> 1.1" },
{:logger_file_backend, "~> 0.0.8"},
{:distillery, "~>1.2.2"}]
end
......
......@@ -26,4 +26,5 @@
"postgrex": {:hex, :postgrex, "0.13.2", "2b88168fc6a5456a27bfb54ccf0ba4025d274841a7a3af5e5deb1b755d95154e", [:mix], [{:connection, "~> 1.0", [hex: :connection, optional: false]}, {:db_connection, "~> 1.1", [hex: :db_connection, optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]},
"providers": {:hex, :providers, "1.4.1", "e74176b29d4771544410af5022fe1306b100d0f57c9e0f3656bcff5962f87cd2", [:rebar], [{:getopt, "0.8.2", [hex: :getopt, optional: false]}]},
"ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], []},
"relx": {:hex, :relx, "3.1.0", "71b7e00e83543c92512d785a268db78a970fe5d811171329623784daca7d23dc", [:rebar], [{:bbmustache, "1.0.1", [hex: :bbmustache, optional: false]}, {:erlware_commons, "0.13.0", [hex: :erlware_commons, optional: false]}, {:getopt, "0.8.2", [hex: :getopt, optional: false]}, {:providers, "1.4.1", [hex: :providers, optional: false]}]}}
"relx": {:hex, :relx, "3.1.0", "71b7e00e83543c92512d785a268db78a970fe5d811171329623784daca7d23dc", [:rebar], [{:bbmustache, "1.0.1", [hex: :bbmustache, optional: false]}, {:erlware_commons, "0.13.0", [hex: :erlware_commons, optional: false]}, {:getopt, "0.8.2", [hex: :getopt, optional: false]}, {:providers, "1.4.1", [hex: :providers, optional: false]}]},
"uuid": {:hex, :uuid, "1.1.7", "007afd58273bc0bc7f849c3bdc763e2f8124e83b957e515368c498b641f7ab69", [:mix], []}}
......@@ -12,7 +12,7 @@ defmodule Grains.SliderChannel do
end
def handle_in("slider", params, socket) do
IO.puts socket.assigns.user_uuid
IO.puts "slider: #{params["slider"]} : #{params["value"]}"
slider = params["slider"]
value = params["value"]
......
......@@ -21,10 +21,24 @@ defmodule Grains.UserSocket do
#
# See `Phoenix.Token` documentation for examples in
# performing token verification on connect.
def connect(_params, socket) do
{:ok, socket}
@max_age 2 * 7 * 24 * 60 * 60
def connect(%{"token" => token}, socket) do
case Phoenix.Token.verify(socket, "user socket", token, max_age: @max_age) do
{:ok, uuid} ->
IO.puts "verified : #{uuid}"
{:ok, assign(socket, :user_uuid, uuid) }
{:error, _reason} ->
IO.puts "error: #{_reason}"
:error
end
end
def connect(_params, _socket), do: :error
def id(socket), do: "users_sockets:#{socket.assigns.user_uuid}"
# Socket id's are topics that allow you to identify all sockets for a given user:
#
# def id(socket), do: "users_socket:#{socket.assigns.user_id}"
......@@ -35,5 +49,4 @@ defmodule Grains.UserSocket do
# Grains.Endpoint.broadcast("users_socket:#{user.id}", "disconnect", %{})
#
# Returning `nil` makes this socket anonymous.
def id(_socket), do: nil
end
defmodule Grains.GrainsController do
use Grains.Web, :controller
def show(conn, %{"grain" => grain}) do
render conn, "show.html", grain: grain
end
end
defmodule Grains.Token do
import Plug.Conn
def init(opts) do
opts
end
def call(conn, _opts) do
uuid = get_session(conn, :user_uuid) || UUID.uuid1()
token = Phoenix.Token.sign(conn, "user socket", uuid )
conn
|> assign(:user_token, token)
|> put_session(:user_uuid, uuid)
|> configure_session(renew: true)
end
end
......@@ -7,6 +7,7 @@ defmodule Grains.Router do
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
plug Grains.Token
end
pipeline :api do
......@@ -16,6 +17,7 @@ defmodule Grains.Router do
scope "/", Grains do
pipe_through :browser # Use the default browser stack
get "/grains/:grain", GrainsController, :show
get "/", PageController, :index
end
......
<h1>Grain <%= @grain %></h1>
<div id="#sliderPanel" class="interfacePanel"></div>
......@@ -14,12 +14,7 @@
<body>
<div class="container">
<header class="header">
<nav role="navigation">
<ul class="nav nav-pills pull-right">
<li><a href="http://www.phoenixframework.org/docs">Get Started</a></li>
</ul>
</nav>
<span class="logo"></span>
<h1>Welcome to Grains</h1>
</header>
<p class="alert alert-info" role="alert"><%= get_flash(@conn, :info) %></p>
......@@ -30,6 +25,7 @@
</main>
</div> <!-- /container -->
<script>window.userToken = "<%= assigns[:user_token] %>"</script>
<script src="<%= static_path(@conn, "/js/app.js") %>"></script>
</body>
</html>
......@@ -4,4 +4,16 @@
the mobile web and the cloud.</p>
</div>
<div id="#sliderPanel" class="interfacePanel"></div>
<div>
Choose your sound:
<ul>
<li>
<a href="/grains/rhythm">Rhythm</a>
</li>
<li><a href="/grains/bass">Bass</a></li>
<li><a href="/grains/chords">Chords</a></li>
<li><a href="/grains/melody">Melody</a></li>
</li>
</ul>
</div>
defmodule Grains.GrainsView do
use Grains.Web, :view
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment