mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-11-09 13:44:53 +08:00
Store autogenerated user id seprate from identity data (#2879)
This commit is contained in:
parent
712c7d6cc1
commit
ffd6b99bd9
3 changed files with 13 additions and 13 deletions
|
|
@ -4,16 +4,16 @@ defmodule LivebookWeb.UserHook do
|
||||||
|
|
||||||
alias Livebook.Users.User
|
alias Livebook.Users.User
|
||||||
|
|
||||||
def on_mount(:default, _params, %{"identity_data" => identity_data} = session, socket) do
|
def on_mount(:default, _params, session, socket) do
|
||||||
if connected?(socket) do
|
|
||||||
Livebook.Users.subscribe(identity_data.id)
|
|
||||||
end
|
|
||||||
|
|
||||||
socket =
|
socket =
|
||||||
socket
|
socket
|
||||||
|> assign_new(:current_user, fn -> build_current_user(session, socket) end)
|
|> assign_new(:current_user, fn -> build_current_user(session, socket) end)
|
||||||
|> attach_hook(:current_user_subscription, :handle_info, &info/2)
|
|> attach_hook(:current_user_subscription, :handle_info, &info/2)
|
||||||
|
|
||||||
|
if connected?(socket) do
|
||||||
|
Livebook.Users.subscribe(socket.assigns.current_user.id)
|
||||||
|
end
|
||||||
|
|
||||||
{:cont, socket}
|
{:cont, socket}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -43,7 +43,7 @@ defmodule LivebookWeb.UserHook do
|
||||||
attrs -> attrs
|
attrs -> attrs
|
||||||
end
|
end
|
||||||
|
|
||||||
user = User.new(attrs["id"])
|
user = User.new(session["user_id"])
|
||||||
|
|
||||||
case Livebook.Users.update_user(user, attrs) do
|
case Livebook.Users.update_user(user, attrs) do
|
||||||
{:ok, user} -> user
|
{:ok, user} -> user
|
||||||
|
|
|
||||||
|
|
@ -37,11 +37,11 @@ defmodule LivebookWeb.UserPlug do
|
||||||
|
|
||||||
identity_data ->
|
identity_data ->
|
||||||
# Ensure we have a unique ID to identify this user/session.
|
# Ensure we have a unique ID to identify this user/session.
|
||||||
id =
|
id = identity_data[:id] || get_session(conn, :user_id) || Livebook.Utils.random_long_id()
|
||||||
identity_data[:id] || get_session(conn, :identity_data)[:id] ||
|
|
||||||
Livebook.Utils.random_long_id()
|
|
||||||
|
|
||||||
put_session(conn, :identity_data, Map.put(identity_data, :id, id))
|
conn
|
||||||
|
|> put_session(:identity_data, identity_data)
|
||||||
|
|> put_session(:user_id, id)
|
||||||
|
|
||||||
true ->
|
true ->
|
||||||
conn
|
conn
|
||||||
|
|
|
||||||
|
|
@ -13,17 +13,17 @@ defmodule LivebookWeb.UserPlugTest do
|
||||||
|> fetch_cookies()
|
|> fetch_cookies()
|
||||||
|> call()
|
|> call()
|
||||||
|
|
||||||
assert get_session(conn, :identity_data)[:id] != nil
|
assert get_session(conn, :user_id) != nil
|
||||||
end
|
end
|
||||||
|
|
||||||
test "keeps user id in the session if present" do
|
test "keeps user id in the session if present" do
|
||||||
conn =
|
conn =
|
||||||
conn(:get, "/")
|
conn(:get, "/")
|
||||||
|> init_test_session(%{identity_data: %{id: "valid_user_id"}})
|
|> init_test_session(%{user_id: "valid_user_id"})
|
||||||
|> fetch_cookies()
|
|> fetch_cookies()
|
||||||
|> call()
|
|> call()
|
||||||
|
|
||||||
assert get_session(conn, :identity_data)[:id] != nil
|
assert get_session(conn, :user_id) != nil
|
||||||
end
|
end
|
||||||
|
|
||||||
test "given no user_data cookie, generates and stores new data" do
|
test "given no user_data cookie, generates and stores new data" do
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue