4

I'm using Google's "platform" sign-in for websites. On the signin page, I only display the sign-in button when the user is unauthenticated. Once they sign in, the button is removed from the page.

This causes Google to throw an error:

Uncaught TypeError: Cannot read property 'style' of null

This comes from this line of their minified code:

window.document.getElementById((c ? "not_signed_in" : "connected") + a.El).style.display = "none";

Which is clearly assuming that the button is still on the page.

Ideally, I'd like to fix this silly code and make it deal with this more gracefully. Is it open source? Can I contribute? Where can I find it?

If it's proprietary, I guess I'd be ok with a workaround. My thoughts for a workaround: if I could tell this Google thing to unrender the button, I think I'd be ok.

That is, I'm using the render method as described in their docs. When I remove the button container from the page, I would like to unrender. I don't see any way to do that.

Any ideas?

chadoh
  • 4,343
  • 6
  • 39
  • 64
  • do you remove the button? if so, stop doing that, hide it instead. if not, you can make a hidden element of the same id to stop the exception. – dandavis Jan 09 '16 at 01:48
  • 1
    @dandavis yes, I remove the button. I will not stop! :-p Seriously, though, I should not have to stop that. The button is on the sign-in page of a single-page app, and the user is redirected to a different page at sign-in. The not-sign-in page doesn't (and shouldn't) have a sign in button (even if it's hidden; that's dumb). Additionally, the id in question is some hashed gobbledegook that's internal to Google's sign-in button logic. I can't easily create a pointless new hidden element with the same id. Did I mention I hate workarounds? haha – but thank you! A stupid workaround beats an error. – chadoh Jan 09 '16 at 13:35
  • @dandavis it does work, btw—you can see how I've implemented the (totally unacceptable, but functional) workaround at https://github.com/chadoh/entire_life/commit/ba926510cb5a0181c6d21dfd286db5f58facfa54 – chadoh Jan 09 '16 at 14:01
  • @chadoh I have same problem.. Can you help me on http://stackoverflow.com/questions/41328556/angular-2-google-api-login-issue – Java Curious ღ Dec 26 '16 at 09:42
  • Possible duplicate of [Cannot read property 'style' of null - Google Sign-In Button](http://stackoverflow.com/questions/37472453/cannot-read-property-style-of-null-google-sign-in-button) – Curly Brace Feb 27 '17 at 21:05

1 Answers1

2

Whenever the user is signed in, you can just hide the box so that it gets rendered but its just not visible.

<div id="g-signin2" style="display: none;"></div>
amey91
  • 542
  • 6
  • 17
  • 1
    Helped me solve it in Angular context: I had to use `ng-show`/`ng-hide` instead of `ng-if` – Grimace of Despair Jun 17 '16 at 04:28
  • hello I have same problem, can you please check http://stackoverflow.com/questions/41328556/angular-2-google-api-login-issue please help me – Java Curious ღ Dec 26 '16 at 09:43
  • @GrimaceofDespair Same problem [link](http://stackoverflow.com/questions/42299875/angular2-google-signin-button-cannot-read-property-style-of-null). Did you solve it. I post an issue on [GitHub](https://github.com/google/google-api-javascript-client/issues/281). – Makla Feb 22 '17 at 15:53
  • @Makla my solution was as mentioned: keep the button alive, but somehow hide it. Make sure it exists in the dom though, which will not be the case when using ng-if, so make sure to use ng-hide or some css trickery. – Grimace of Despair Feb 22 '17 at 22:24
  • I do not use ngIf I use router.navigate (from login to home page). For me this is clearly a bug and I think Google should fix it. We will see what will be response on GitHub. – Makla Feb 23 '17 at 11:56
  • This is just plain old stupid on Google's part to have to leave a button for reference and hide it. I'm sure there are smarter ways to accomplish a reference. I guess we have to hide it for now and wait for Google to fix this at some point. – kevado Mar 18 '17 at 01:58