I'm using the Polymer Starter Kit and polymerfire to create a firebase app with two routes: 'staff' and 'donate'. The donate route is public and the staff route is private. I'd like to protect the staff route such that only logged in users who have an email ending with mycompany.com
can access it. Unauthenticated users will be redirected to /donate
.
My first attempt was to wrap links to and declarations of those routes in dom-if
templates. This works to prevent users from seeing the route, but if a user is logged in, they will not be able to navigate to that route from the address bar - they'll have to click on a link to the route in the app first. This is confusing: the address bar may still say 'staff' even though the 'donate' page is shown.
<iron-selector selected="[[page]]" attr-for-selected="name" class="drawer-list" role="navigation">
<template is="dom-if" if="[[user]]" restamp="true">
<a name="staff" href="/staff">Staff</a>
</template>
<a name="donate" href="/donate">Donate</a>
</iron-selector>
<iron-pages
selected="[[page]]"
attr-for-selected="name"
fallback-selection="donate"
role="main">
<template is="dom-if" if="[[user]]" restamp="true">
<ksybf-staff name="staff"></ksybf-staff>
</template>
<ksybf-donate name="donate" route="[[subroute]]"></ksybf-donate>
</iron-pages>
I've also tried imperatively preventing route loading:
_routePageChanged: function(page) {
if (this.user) {
this.page = page || 'donate';
} else {
this.page = 'donate';
}
},
This produces similar results: initial browsing to /staff
loads the 'donate' route, even though the address bar shows //app/staff
.
How can I protect routes in a way which just works from the user's perspective?
id
to your<iron-pages>
.iron-select
listener to it.It would look like this: For the
<iron-pages>
:Add the listener:
And the function: