var app = angular.module('plunker', ['firebase']);
app.constant('FIREBASE_URL', 'https://newname.firebaseio.com/');
app.run(['$rootScope', 'authService', function($rootScope, authService){
authService();
$rootScope.$watch('auth.authenticated', function() {
isAuthenticated = $rootScope.auth.authenticated;
});
}]);
app.factory('authService', [
'$rootScope',
'$timeout',
'angularFireAuth',
'FIREBASE_URL',
function($rootScope, $timeout, angularFireAuth, FIREBASE_URL) {
return function() {
angularFireAuth.initialize(new Firebase(FIREBASE_URL), {
scope: $rootScope,
name: 'user'
});
$rootScope.auth = {
authenticated: false,
user: null,
name: null
};
$rootScope.$on('angularFireAuth:login', _set);
$rootScope.$on('angularFireAuth:error', _unset);
$rootScope.$on('angularFireAuth:logout', _unset);
function _set(evt, user) {
$timeout(function() {
$rootScope.auth = {
authenticated: true,
user: user.id,
provider: user.provider
};
});
}
function _unset() {
$timeout(function() {
$rootScope.auth = {
authenticated: false,
user: null,
provider: $rootScope.auth && $rootScope.auth.provider
};
});
}
}
}
]);
app.controller('MainCtrl', function($scope, authService, angularFireAuth) {
$scope.name = 'World';
$scope.login = function(){
angularFireAuth.login('facebook');
}
$scope.logout = function(){
angularFireAuth.logout();
}
console.log($scope.auth);
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.0.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js" data-semver="1.0.7"></script>
<script src='https://cdn.firebase.com/v0/firebase.js'></script>
<script src='https://cdn.firebase.com/v0/firebase-simple-login.js'></script>
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/angularFire/0.2.0/angularfire.min.js"></script>-->
<script src="https://cdn.firebase.com/libs/angularfire/0.3.0/angularfire.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<div ng-show="auth.authenticated">
<button ng-click="logout()">logout</button>
</div>
<div ng-show="!auth.authenticated">
<button ng-click="login()">login</button>
</div>
Check it in console. $scope.auth in controller always return null datas
</body>
</html>
/* Put your css in here */