Let's take a look at a simple sample of a login. Using the
google-signin
element you could wait for the google-signin-success
event to trigger and then retrieve/display information about the authenticated user and toggle the UI accordingly. Of course then you also have to handle the reverse case if a user signs out by listening for the google-signed-out
event.But the
google-signin
element also offers an is-authorized
attribute / isAuthorized
property that you can bind to and observe. Toggling the UI based on this property is as simple as adding hidden$="[[!isAuthorized]]"
and hidden$="[[isAuthorized]]"
to elements you want to show/hide. No extra JS necessary for this, as opposed to before where you had to set isAuthorized
in the event handlers.To retrieve user information once authorization has been granted you could add an observer to
isAuthorized
, but I think the much nicer solution is to make user
a computed property that depends on isAuthorized
. Whenever the value of isAuthorized
changes this will re-evaluate the function and set the user
property accordingly.
Let's take this sample a bit further. In many cases you will have to retrieve some more information from your server or elsewhere about the authenticated user. So you would need to trigger some request once the user is signed in and handle the response once it is available. In this sample I'm using my
discovery-api-elements
to fetch information about the user from Google+, but you can do something similar using iron-ajax
or any other data-fetching element.Instead of triggering the request manually, what you can do is binding the
auto
property (at least for discovery-api-elements or iron-ajax) to the isAuthorized
property. Once isAuthorized
and with it auto
becomes true the request will be triggered automatically and you just have to handle the response.But this won't remove the data in case the user signs out. To achieve this we make the data that is displayed (
activities
) a computed property that depends on both the response
from the data-fetching element and the isAuthorized
property.Here's what happens now, when a user signs in:
google-signin
setsisAuthorized
to true.- This sets the
auto
property on the data element which triggers the request. - Once the request completes
plus-activities-list
sets theresponse
property accordingly. - This change triggers recomputing
activities
with the_parseActivities
function. - Once there are items in the
activities
array, they will be displayed by thedom-repeat
.
google-signin
setsisAuthorized
to false.- This triggers recomputing
activities
which will be set to an empty list.