问题
Alright, this is my first question on SO so I'll try to make it a good one sorry ahead of time.
I've been using ember-cli to work on a product. I am using Firebase simple login for authentication and have created an initializer that adds an auth object to all my controllers and routes.
This works, but... There seems to be a delay for the redirect checking. example I go to /secret and this should redirect me back to the /login route and it does but there is a slight delay where I can see the template
I've tried to create a gist with all the necessary information. Let me know if there is anything else I can provide to help out
https://gist.github.com/mbertino/060e96e532f8ce05d2d0
回答1:
You could provide a flag on your auth object, which you use in your route to conditionally display the content:
import Ember from 'ember';
var firebase = new window.Firebase( window.MyAppENV.APP.firebaseURL );
export default Ember.Object.extend({
authed: false,
init: function(route) {
var self = this;
self.authClient = new window.FirebaseSimpleLogin( firebase ), function(error, user) {
self.set( 'authed', false );
if (error) {
alert('Authentication failed: ' + error);
} else if (user) {
self.set( 'authed', true );
// if already a user redirect to the secret resource
route.transitionTo('secret')
} else if (route.routeName !== 'login'){
// if the route is anything but the login route and there is not
// a user then redirect to the login resource
route.transitionTo('login')
}
}.bind(this));
You then can use {{#if auth.authed}}
in your templates to reveal private data, or display different content.
Instead of setting the flag above, I use a watcher on the authenticated status, this is provided by the API (documented here: https://www.firebase.com/docs/web/guide/user-auth.html#section-monitoring-authentication):
// also in init()
self.authRef = new window.Firebase( firebase + '/.info/authenticated' );
self.authRef.on( 'value', function( snap ) {
var oldAuthed = self.get('authed');
if ( snap.val() === true ) {
self.set( 'authed', true );
if ( ! oldAuthed ) {
// Status switched form unauthenticated to authenticated
// go to your route here
);
}
} else {
self.set( 'authed', false );
if ( oldAuthed ) {
// User was logged in, is now logged out
}
}
});
Keep in mind that it's JavaScript, and nothing you can do will be fool-proof. In other words, you can try to hide anything, but someone who really wants to can always hack into your data structures and reveal a hidden page that way. If you don't want people to see something, make sure they can't read it in the first place (by locking down access through the Firebase privilege system).
来源:https://stackoverflow.com/questions/25852290/short-delay-when-trying-to-run-redirect-with-ember-route-with-firebase-authentic