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

omniauth_success.html.erb JSON bug #221

Closed
davidtlee opened this issue Apr 18, 2015 · 3 comments · Fixed by #252
Closed

omniauth_success.html.erb JSON bug #221

davidtlee opened this issue Apr 18, 2015 · 3 comments · Fixed by #252

Comments

@davidtlee
Copy link

I realized that, in the current way omniauth_success.html.erb is written (copied below), the JSON returned gives booleans and numbers as strings, e.g. "false", rather than false or "5" rather than 5. This gave me some problems cause I use coffeescript, which only supports the === or !== equalities which means that "5" !== 5. Any way to fix this easily?

<% @resource.as_json.each do |attr, val| %>
  "<%= attr %>": "<%= val %>",
<% end %>

"auth_token": "<%= @token %>",
"message":    "deliverCredentials",
"client_id":  "<%= @client_id %>",
"expiry":     "<%= @expiry %>",
"config":     "<%= @config %>"
@tbloncar
Copy link
Contributor

You could override this view with something like:

<% @resource.as_json.each do |attr, val| %>

    # Check for number/boolean
    <% if val.is_a?(Numeric) || !!val == val %>
        "<%= attr %>": <%= val %>,
    <% else %>
        "<%= attr %>": "<%= val %>",
    <% end %>
<% end %>

"auth_token": "<%= @token %>",
"message":    "deliverCredentials",
"client_id":  "<%= @client_id %>",
"expiry":     "<%= @expiry %>",
"config":     "<%= @config %>"

Then you'd just need to make sure that your override view is rendered in OmniauthCallbacksController#omniauth_success. I had previously written an override for this controller, so I just replaced this line with:

render :layout => "layouts/omniauth_response", :template => "path_to_my_view_overrides/omniauth_success"

Hope this helps.

@davidtlee
Copy link
Author

ahhh, good idea :) thanks!

@nbrustein
Copy link
Contributor

Here is a more general solution, which worked for me with array values as well:

    <% @resource.as_json.each do |attr, val| %>

        "<%= attr %>": <%= val.to_json.html_safe %>,
    <% end %>

    "auth_token": "<%= @token %>",
    "message":    "deliverCredentials",
    "client_id":  "<%= @client_id %>",
    "expiry":     "<%= @expiry %>",
    "config":     "<%= @config %>"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants