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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dom-module id="signin-event-handling"> | |
<template> | |
<google-signin | |
client-id="..." | |
scopes="..." | |
on-google-signin-success="_handleSignin" | |
on-google-signed-out="_handleSignout"></google-signin> | |
<div hidden$="[[!isAuthorized]]">Signed in as <span>{{user.name}}</span></div> | |
</template> | |
<script> | |
Polymer({ | |
is: 'signin-event-handling' | |
properties: { | |
user: Object, | |
isAuthorized: Boolean | |
}, | |
_handleSignin: function () { | |
var profile = gapi.auth2.getAuthInstance().currentUser.get().getBasicProfile(); | |
this.isAuthorized = true; | |
this.user = { | |
id: profile.getId(), | |
name: profile.getName(), | |
image: profile.getImageUrl() | |
}; | |
}, | |
_handleSignout: function () { | |
this.user = undefined; | |
this.isAuthorized = false; | |
} | |
}); | |
</script> | |
</dom-module> |
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dom-module id="signin-data-binding"> | |
<template> | |
<google-signin | |
client-id="..." | |
scopes="..." | |
is-authorized="{{isAuthorized}}"></google-signin> | |
<div hidden$="[[!isAuthorized]]">Signed in as <span>{{user.name}}</span></div> | |
</template> | |
<script> | |
Polymer({ | |
is: 'signin-data-binding', | |
properties: { | |
user: { | |
type: Object, | |
computed: '_computeUser(isAuthorized)' | |
}, | |
isAuthorized: Boolean | |
}, | |
_computeUser: function (isAuthorized) { | |
if (!isAuthorized) { return undefined; } | |
var profile = gapi.auth2.getAuthInstance().currentUser.get().getBasicProfile(); | |
return { | |
id: profile.getId(), | |
name: profile.getName(), | |
image: profile.getImageUrl() | |
}; | |
} | |
}); | |
</script> | |
</dom-module> |
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dom-module id="request-event-handling"> | |
<template> | |
<discovery-api-elements | |
name="plus" version="v1"></discovery-api-elements> | |
<google-signin | |
client-id="..." | |
scopes="..." | |
is-authorized="{{isAuthorized}}"></google-signin> | |
<plus-activities-list | |
id="listRequest" | |
user-id="me" | |
collection="public" | |
on-success="_handleResponse"></plus-activities-list> | |
<template is="dom-repeat" items="[[activities]]"> | |
<span>[[item.published]]</span> <a href$="[[item.url]]">[[item.title]]</a><br> | |
</template> | |
</template> | |
<script> | |
(function () { | |
Polymer({ | |
is: "request-event-handling", | |
properties: { | |
isAuthorized: { | |
type: Boolean, | |
observer: "_authorizedChanged" | |
}, | |
activities: Array | |
}, | |
_authorizedChanged: function (isAuthorized) { | |
if (!isAuthorized) { | |
this.activities = []; | |
return; | |
} | |
// Trigger the API request | |
this.$.listRequest.go(); | |
}, | |
_handleResponse: function (e) { | |
this.activities = e.detail.items; | |
} | |
}); | |
}()); | |
</script> | |
</dom-module> |
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dom-module id="request-data-binding1"> | |
<template> | |
<discovery-api-elements | |
name="plus" version="v1"></discovery-api-elements> | |
<google-signin | |
client-id="..." | |
scopes="..." | |
is-authorized="{{isAuthorized}}"></google-signin> | |
<plus-activities-list | |
user-id="me" | |
collection="public" | |
on-success="_handleResponse" | |
auto$="[[isAuthorized]]"></plus-activities-list> | |
<template is="dom-repeat" items="[[activities]]"> | |
<span>[[item.published]]</span> <a href$="[[item.url]]">[[item.title]]</a><br> | |
</template> | |
</template> | |
<script> | |
(function () { | |
Polymer({ | |
is: "request-data-binding1", | |
properties: { | |
isAuthorized: Boolean, | |
activities: Array | |
}, | |
_handleResponse: function (e) { | |
this.activities = e.detail.items; | |
} | |
}); | |
}()); | |
</script> | |
</dom-module> |
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dom-module id="request-data-binding2"> | |
<template> | |
<discovery-api-elements | |
name="plus" version="v1"></discovery-api-elements> | |
<google-signin | |
client-id="..." | |
scopes="..." | |
is-authorized="{{isAuthorized}}"></google-signin> | |
<plus-activities-list | |
user-id="me" | |
collection="public" | |
response="{{response}}" | |
auto$="[[isAuthorized]]"></plus-activities-list> | |
<template is="dom-repeat" items="[[activities]]"> | |
<span>[[item.published]]</span> <a href$="[[item.url]]">[[item.title]]</a><br> | |
</template> | |
</template> | |
<script> | |
(function () { | |
Polymer({ | |
is: "request-data-binding2", | |
properties: { | |
isAuthorized: Boolean, | |
response: Object, | |
activities: { | |
type: Array, | |
computed: '_parseActivities(response, isAuthorized)' | |
} | |
}, | |
_parseActivities: function (response, isAuthorized) { | |
if (!isAuthorized || !response) { return []; } | |
// you could do any preprocessing of the data here | |
return response.items; | |
} | |
}); | |
}()); | |
</script> | |
</dom-module> |
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.