Module: Google::IdentityToolkit::Helpers

Defined in:
lib/google/identity_toolkit/helpers.rb

Instance Method Summary collapse

Instance Method Details

#account_chooser(element_id, options = {}) ⇒ String

Generate javascript for configuring the identity toolkit button

Parameters:

  • element_id (String)

    Id of element that will contain the login button

  • options (Hash) (defaults to: {})

    Configuration options

Options Hash (options):

  • :api_key (String)

    APIKey from developer console

  • :company_name (String)

    Name of site to display on login pages

  • :base_url (String)

Returns:

  • (String)

    Block of javascript (must be wrapped in <script> tag)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/google/identity_toolkit/helpers.rb', line 22

def (element_id, options = {})
  callback_url = Addressable::URI.parse(options[:callback_url] || Google::IdentityToolkit.callback_url)
  params = callback_url.query_values || {}
  callback_url.query_values = params.merge("mobile" => "1") if options[:mobile]
  js = <<-EOF
    window.google.identitytoolkit.setConfig({
      developerKey: "#{options[:api_key] || Google::IdentityToolkit.api_key}",
      companyName: "#{options[:site_name]}",
      callbackUrl: "#{callback_url}",
      userStatusUrl: "#{callback_url}",
      loginUrl: "#{callback_url}",
      federatedSignupUrl: "#{options[:] || options[:] || '/signup' }",
      signupUrl: "#{options[:] || '/signup'}",
      homeUrl: "#{options[:home_url] || '/' }",
      logoutUrl: "#{options[:logout_url] || '/logout'}",
      realm: "#{options[:realm]}",
      language: "#{options[:language] || 'en'}",
      idps: ["Gmail", "AOL", "Hotmail", "Yahoo"],
      tryFederatedFirst: #{options[:try_federated_first] || false},
      useCachedUserStatus: #{options[:use_cached_status] || false}
    });
    $('#{element_id}').accountChooser();
  EOF
end

#show_current_user(options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/google/identity_toolkit/helpers.rb', line 47

def show_current_user(options={})
  js = ""
  user = options[:user] || Google::IdentityToolkit.current_user
  if user
    js = <<-EOF
    window.google.identitytoolkit.updateSavedAccount({
      email: '#{user[:email]}',
      displayName: '#{user[:display_name]}',
      photoUrl: '#{user[:photo_url]}'
    });
    window.google.identitytoolkit.showSavedAccount('#{user[:email]}');
    EOF
  end
  js
end