Skip to content

Commit 2712788

Browse files
committed
feat: add locale field to AccountUser, extract from user JWT claims
The app_registry_v2 JWT format nests user info under a "user" key including a "locale" field. AccountUser now carries locale (defaults to nil for backward compat with older tokens that omit it).
1 parent 44a7a68 commit 2712788

4 files changed

Lines changed: 37 additions & 12 deletions

File tree

lib/peek_app_sdk/account_user.ex

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,16 @@ defmodule PeekAppSDK.AccountUser do
22
@moduledoc """
33
When iFrames are loaded, who is the logged in user?
44
"""
5-
@fields [
6-
:email,
7-
:id,
8-
:is_peek_admin,
9-
:name,
10-
:primary_role
11-
]
12-
@enforce_keys @fields
13-
defstruct @fields
5+
@enforce_keys [:email, :id, :is_peek_admin, :name, :primary_role]
6+
defstruct [:email, :id, :is_peek_admin, :name, :primary_role, locale: nil]
147

158
@type t :: %__MODULE__{
169
email: String.t(),
1710
id: String.t(),
1811
is_peek_admin: boolean(),
1912
name: String.t(),
20-
primary_role: String.t()
13+
primary_role: String.t() | nil,
14+
locale: String.t() | nil
2115
}
2216

2317
@doc """

lib/peek_app_sdk/plugs/peek_auth.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,15 @@ defmodule PeekAppSDK.Plugs.PeekAuth do
8585
"email" => current_user_email,
8686
"is_admin" => current_user_is_peek_admin,
8787
"name" => current_user_name
88-
}
88+
} = user
8989
}) do
9090
%PeekAppSDK.AccountUser{
9191
email: current_user_email,
9292
id: current_user_id,
9393
is_peek_admin: current_user_is_peek_admin,
9494
name: current_user_name,
95-
primary_role: nil
95+
primary_role: nil,
96+
locale: user["locale"]
9697
}
9798
end
9899

test/peek_app_sdk/account_user_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ defmodule PeekAppSDK.AccountUserTest do
1313
assert hook.id == nil
1414
assert hook.is_peek_admin == nil
1515
assert hook.primary_role == nil
16+
assert hook.locale == nil
1617
end
1718
end
1819
end

test/peek_app_sdk/plugs/peek_auth_test.exs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,35 @@ defmodule PeekAppSDK.Plugs.PeekAuthTest do
216216
assert conn.assigns.peek_account_user.is_peek_admin == true
217217
assert conn.assigns.peek_account_user.name == "Legacy User"
218218
assert conn.assigns.peek_account_user.primary_role == nil
219+
assert conn.assigns.peek_account_user.locale == nil
220+
end
221+
222+
test "extracts locale from user JWT claims into account_user" do
223+
install_id = "test_install_id"
224+
225+
config = PeekAppSDK.Config.get_config()
226+
shared_secret_key = config.peek_app_secret
227+
signer = Joken.Signer.create("HS256", shared_secret_key)
228+
229+
params = %{
230+
"iss" => "app_registry_v2",
231+
"sub" => install_id,
232+
"exp" => DateTime.utc_now() |> DateTime.add(60) |> DateTime.to_unix(),
233+
"user" => %{
234+
"email" => "user@example.com",
235+
"id" => "user123",
236+
"is_admin" => false,
237+
"name" => "Test User",
238+
"locale" => "fr"
239+
}
240+
}
241+
242+
{:ok, token, _claims} = Token.generate_and_sign(params, signer)
243+
conn = conn(:post, "/", %{"peek-auth" => token})
244+
245+
conn = PeekAuth.set_peek_install_id(conn, %{})
246+
247+
assert conn.assigns.peek_account_user.locale == "fr"
219248
end
220249

221250
test "handles non-keyword list and non-map options" do

0 commit comments

Comments
 (0)