Created
February 9, 2016 14:26
-
-
Save deadkff01/b3e31b30dd1bef2713b0 to your computer and use it in GitHub Desktop.
AngularJS - FacebookAuth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = angular.module('myApp', ['ngRoute', 'ngCookies']); | |
window.fbAsyncInit = function() { | |
FB.init({ | |
appId : 'the id of your application', | |
xfbml : true, | |
version : 'v2.5' | |
}); | |
}; | |
(function(d, s, id){ | |
var js, fjs = d.getElementsByTagName(s)[0]; | |
if (d.getElementById(id)) {return;} | |
js = d.createElement(s); js.id = id; | |
js.src = "https://connect.facebook.net/en_US/sdk.js"; | |
fjs.parentNode.insertBefore(js, fjs); | |
}(document, 'script', 'facebook-jssdk')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.factory('authToken',['$cookieStore', '$location', function($cookieStore, $location){ | |
var token = {}; | |
token.start = null; | |
token.setAccessToken = function(accessToken) { | |
$cookieStore.put('accessToken', accessToken); | |
} | |
token.getAccessToken = function() { | |
token.authToken = $cookieStore.get('accessToken'); | |
return token.authToken; | |
} | |
token.getUserinfo = function() { | |
token.userInfo = $cookieStore.get('facebookUser'); | |
if(token.userInfo) | |
return token.userInfo; | |
} | |
token.logout = function(){ | |
$cookieStore.remove("accessToken"); | |
$cookieStore.remove('facebookUser'); | |
$location.path('/'); | |
} | |
return token; | |
}]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.controller('LoginController', ['$scope','$http', '$rootScope','$location', '$cookieStore', 'authToken', | |
function($scope, $http, $rootScope, $location, $cookieStore, authToken) { | |
//send an req to server side | |
$scope.checkFacebookUser = function(facebook){ | |
$http.post('/checkFacebookUser', facebook).success(function(response){ | |
$location.path('/dashboard'); | |
}).error(function(response){ | |
console.log('Application unavailable... Try later.'); | |
}); | |
} | |
$scope.facebookLogin = function() { | |
FB.login(function(response){ | |
if (response.authResponse) { | |
FB.api('/me', function(response) { | |
$cookieStore.put('facebookUser', response); | |
var accessToken = FB.getAuthResponse().accessToken; | |
authToken.setAccessToken(accessToken); | |
$scope.checkFacebookUser(response); | |
$scope.$applyAsync(); | |
}); | |
} else { | |
$location.path('/'); | |
} | |
}); | |
}; | |
}]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.config(['$routeProvider', '$locationProvider', | |
function($routeProvider, $locationProvider) { | |
$routeProvider | |
.when("/", { | |
templateUrl : '/views/login.html', | |
controller : 'LoginController' | |
}) | |
.when("/dashboard", { | |
templateUrl : '/views/dashboard.html', | |
controller : 'DashboardController', | |
authenticated: true | |
}) | |
.otherwise('/', { | |
templateUrl : '/views/login.html', | |
controller : 'LoginController' | |
}); | |
$locationProvider.html5Mode({ | |
enabled: true, | |
requireBase: false | |
}); | |
}]); | |
app.run(['$rootScope', '$location', 'authToken', | |
function($rootScope, $location, authToken){ | |
$rootScope.$on('$routeChangeStart', function(event, next, cur) { | |
/*Check if user are authenticated*/ | |
var userAuth = authToken.getAccessToken(); | |
var facebookUser = authToken.getUserinfo(); | |
authToken.start = true; | |
if(!userAuth) { | |
$location.path('/'); | |
}else { | |
$location.path('/dashboard'); | |
} | |
}); | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment