function AuthService($firebaseAuth) {
    var authorisation = $firebaseAuth();
    var playerData = null;
    var storeAuth = function(response) {
        playerData = response;
        return playerData;
    }
    var signIn = function(player) {
        playerData = player;
        return authorisation.$requireSignIn();
    }
    var clearPlayerData = function() {
        playerData = null;
    }
    this.login = function(player) {
        return authorisation
            .$signInWithEmailAndPassword(player.email, player.password)
            .then(storeAuth);
    }
    this.register = function(player) {
        return authorisation
            .$createUserWithEmailAndPassword(player.email, player.password)
            .then(storeAuth);
    }
    this.needsAuth = function() {
        return authorisation
            .$waitForSignIn().then(signIn);
    }
    this.playerAuthenticated = function() {
        return !!playerData;
    }
    this.getPlayer = function() {
        if(playerData) {
            return playerData;
        }
    }
    this.getPlayers = function() {
        return authorisation.child('user');
    }
    this.logout = function() {
        return authorisation
            .$signOut()
            .then(clearPlayerData);
    }
}

angular
    .module(DRONES.modules.components.AUTH)
    .service('AuthService', AuthService);