Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protocol String.Chars not implemented for %{"email_verified" => "false"} of type Map #20

Closed
nelsonic opened this issue Dec 5, 2019 · 3 comments
Labels
bug Something isn't working chore help wanted Extra attention is needed

Comments

@nelsonic
Copy link
Member

nelsonic commented Dec 5, 2019

Part of documenting #10 this package is attempting to create an app that uses the package from scratch. I am creating the absolute minimum steps example in: https://github.com/dwyl/auth-demo

When I attempt to access the "email_verified" property of the profile (returned by Google)
in a template e.g:

<section class="phx-hero">
  <%= @profile.email_verified != "true" do %>

    <h1>Welcome to Awesome App!</h1>
    <p>To get started, login to your Google Account: <p>
    <a href="<%= @oauth_google_url %>">
      <img src="https://i.imgur.com/Kagbzkq.png" alt="Sign in with Google" />
    </a>
  <% else %>
    <h1> Welcome <%= @profile["given_name"] %> </h1>
  <% end %>
</section>

I see the following error:

image

protocol String.Chars not implemented for %{"email_verified" => "false"} of type Map

This is very unfriendly to end users (the beginners who are attempting to consume the package).
This is partly Elixir being unfriendly and partly our fault for not making the profile map easier to use.

I feel we could improve this by transforming the keys of the profile map from strings to atoms.
https://stackoverflow.com/questions/31990134/convert-map-keys-from-strings-to-atoms-in-elixir

Specifically in the parse_body_response currently:

defp parse_body_response({:ok, response}) do
body = Map.get(response, :body)
if body == nil do
{:error, :no_body}
else
Poison.decode(body)
end
end

Could be:

  def parse_body_response({:ok, response}) do
    body = Map.get(response, :body)
    if body == nil do
      {:error, :no_body}
    else # make keys of map atoms for easier access in templates
      {:ok, str_key_map} = Poison.decode(body)
      atom_key_map = for {key, val} <- str_key_map, into: %{},
        do: {String.to_atom(key), val}
      {:ok, atom_key_map}
    end # https://stackoverflow.com/questions/31990134
  end

So that instead of returning a Map where they keys are strings:

%{
  "email" => "nelson@gmail.com",
  "email_verified" => true,
  "family_name" => "Correia",
  "given_name" => "Nelson",
  "locale" => "en",
  "name" => "Nelson Correia",
  "picture" => "https://lh3.googleusercontent.com/a-/AAuE7mApnYb260YC1JY7",
  "sub" => "940732358705212133793"
}

The parse_body_response would return a Map where the keys are atoms:

%{
  email: "nelson@gmail.com",
  email_verified: true,
  family_name: "Correia",
  given_name: "Nelson",
  locale: "en",
  name: "Nelson Correia",
  picture: "https://lh3.googleusercontent.com/a-/AAuE7mApnYb260YC1JY7",
  sub: "940732358705212133793"
}
@nelsonic nelsonic added bug Something isn't working help wanted Extra attention is needed chore labels Dec 5, 2019
nelsonic added a commit that referenced this issue Dec 5, 2019
SimonLab added a commit that referenced this issue Dec 5, 2019
@nelsonic
Copy link
Member Author

nelsonic commented Dec 5, 2019

@SimonLab thanks for sense checking this suggestion and merging my PR #22 👍

@nelsonic nelsonic closed this as completed Dec 5, 2019
@jasmo2
Copy link

jasmo2 commented Jan 10, 2020

It still have the error even with the atoms

The way are been call is like this:
ElixirAuthGoogle.get_user_profile(token)

protocol String.Chars not implemented for %{email: "em@mail.com", email_verified: true, family_name: "OKAY", given_name: "OKAY", locale: "en-GB", name: "OK OK"} of type Map

nelsonic added a commit to dwyl/elixir-auth-github that referenced this issue Feb 1, 2020
@nelsonic
Copy link
Member Author

nelsonic commented Feb 1, 2020

GOTO: #28 (issue for discussing this)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working chore help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants