Prevent user information from being exposed in session tokens
defmodule MyApp.GuardianSerializer do
@behaviour Guardian.Serializer
def for_token(user = %User{}), do: { :ok, "User:#{user.id}:#{user.email}:#{user.name}" }
def from_token("User:" <> id <> ":" <> email <> ":" <> name), do: { :ok, %User{id: id, email: email, name: name} }
end
This code is including the user's email and name in the session token. If an attacker can decode this token, they can obtain the user's email and name.
defmodule MyApp.GuardianSerializer do
@behaviour Guardian.Serializer
def for_token(user = %User{}), do: { :ok, "User:#{user.id}" }
def from_token("User:" <> id), do: { :ok, %User{id: id} }
end
The secure code does not include the user's email and name in the session token, thus protecting sensitive data.