My app has 2 pages: main.html
and login.html
.
When not authenticated users go to /main
they should be redirected to /login
.
The problem is that main.html
is rendered first, and after a second or so, when user authentication fails, login.html
is rendered.
How could I prevent from main.html
to be rendered until authentication succeeds?
Here is the relevant code (CoffeeScript):
angular.module('myApp', [...])
.config(['$routeProvider', ($routeProvider) ->
$routeProvider.when '/login',
templateUrl: 'html/login.html'
controller: LoginController
$routeProvider.otherwise
templateUrl: 'html/main.html'
controller: MainController
])
.run(['$rootScope', '$location', 'appService', ($rootScope, $location, app) ->
$rootScope.$on '$locationChangeStart', (event, newValue, oldValue) ->
return if newValue == '/login'
$.when(app.authenticate()).fail ->
$location.path '/login'
$rootScope.$apply()
])
angular.module('myApp.services').factory 'appService' , () ->
rootRef = new Firebase('https://myapp.firebaseio.com')
user: null
authenticate: ->
deferred = $.Deferred()
authClient = new FirebaseAuthClient rootRef, (error, user) =>
if error
# An error occurred while attempting login
@user = null
deferred.reject()
else if user
# User authenticated with Firebase
@user = user
deferred.resolve()
else
# User is logged out
@user = null
deferred.reject()
deferred.promise()