- view
- index.html
- display.html
- js
- main.js
- controllers
- mainController.js
- config
- moduleConfig.js
- factory
- facebookFactory.js
- service
- serviceLocalStorage.js
This file contains 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
<!DOCTYPE html> | |
<html ng-app="facebookAngularjs"> | |
<head> | |
<title>Facebook authentification with angularJS By Samark Chaisanguan</title> | |
<script type="text/javascript" src="js/angular.min.js"></script> | |
<script type="text/javascript" src="js/angular-route.min.js"></script> | |
<script type="text/javascript" src="js/main.js"></script> | |
<script type="text/javascript" src="js/config/moduleConfig.js"></script> | |
<script type="text/javascript" src="js/factory/facebookFactory.js"></script> | |
<script type="text/javascript" src="js/controllers/mainController.js"></script> | |
<script type="text/javascript" src="js/services/serviceLocalStorage.js"></script> | |
<script type="text/javascript" src="//connect.facebook.net/en_US/all.js"></script> | |
</head> | |
<body> | |
<div class="container" ng-view> | |
</div> | |
</body> | |
</html> |
This file contains 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
<div id="fb-root"></div> | |
<fb:login-button show-faces="true" max-rows="1" size="large"></fb:login-button> | |
{{ name }} | |
<br> | |
name: {{ list.item.name}}<br> | |
lastName: {{ list.item.lastName}} | |
<pre> | |
{{ list | json}} | |
</pre> | |
<pre> | |
{{ profile |json}} | |
</pre> | |
</div> |
This file contains 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
(function() { | |
"use strict"; | |
var facebookAngularjs = angular.module('facebookAngularjs', [ | |
'ngRoute', | |
'mainController', | |
'moduleConfig', | |
]); | |
facebookAngularjs.config(function($routeProvider) { | |
$routeProvider.when('/index', { | |
templateUrl: 'display.html', | |
controller: 'mainController' | |
}).otherwise({ | |
redirectTo: '/index' | |
}); | |
}); | |
} |
This file contains 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
/** main controller */ | |
(function(){ | |
var mainController = angular.module('mainController', ['moduleConfig','facebookFactory','serviceLocalStorage']); | |
mainController.controller('mainController', ['$scope', '$window', 'config','facebook','localStorage', | |
function($scope, $window ,config,facebook,localStorage) { | |
console.log(config); | |
facebook.checkConnect().then(function(status){ | |
/** case already authen */ | |
console.log(status); | |
console.log(facebook.getProfile()); | |
facebook.getProfile().then(function(data){ | |
$scope.profile = data; | |
localStorage.set('profile',angular.toJson(data)); | |
}); | |
},function(err){ | |
console.log(error); | |
}); | |
}]); | |
}()); |
This file contains 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
/** config constant */ | |
(function() { | |
var moduleConfig = angular.module('moduleConfig', []); | |
moduleConfig.constant('config', { | |
local: { | |
baseUrl: 'http://localhost', | |
getprofile: '/get-profile', | |
register: '/register', | |
removeProfile: '/remove-profile', | |
getall: 'get-all-profile', | |
}, | |
alpha: { | |
baseUrl: 'http://localhost', | |
getprofile: '/get-profile', | |
register: '/register', | |
removeProfile: '/remove-profile', | |
getall: 'get-all-profile', | |
}, | |
staging: { | |
baseUrl: 'http://localhost', | |
getprofile: '/get-profile', | |
register: '/register', | |
removeProfile: '/remove-profile', | |
getall: 'get-all-profile', | |
}, | |
production: { | |
baseUrl: 'http://localhost', | |
getprofile: '/get-profile', | |
register: '/register', | |
removeProfile: '/remove-profile', | |
getall: 'get-all-profile', | |
}, | |
}); | |
/** config constant language */ | |
moduleConfig.constant('lang', { | |
th: { | |
message: { | |
success: 'ทำรายการสำเร็จ', | |
fail: 'ไม่สามารถทำรายการได้', | |
}, | |
error: { | |
404: 'ไม่พบหน้าที่คุณเรียก', | |
500: 'เกิดข้อผิดพลาด', | |
}, | |
display: {}, | |
alert: {}, | |
alert: {}, | |
}, | |
en: { | |
message: { | |
success: 'ทำรายการสำเร็จ', | |
fail: 'ไม่สามารถทำรายการได้', | |
}, | |
error: { | |
404: 'ไม่พบหน้าที่คุณเรียก', | |
500: 'เกิดข้อผิดพลาด', | |
}, | |
display: {}, | |
alert: {}, | |
alert: {}, | |
}, | |
}); | |
/** config facebook constant */ | |
moduleConfig.constant('facebookConfig', { | |
appId: '295136370824606', | |
status: true, | |
cookie: true, | |
xfbml: true, | |
version: 'v2.4' | |
}); | |
}()); |
This file contains 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
/** facebook factory */ | |
var facebookFactory = angular.module('facebookFactory', ['moduleConfig']); | |
facebookFactory.service('facebook', ['$window','facebookConfig', 'connect','getProfile', | |
function($window,facebookConfig,connect,getProfile) { | |
var init = function() { | |
$window.fbAsyncInit = function() { | |
FB.init({ | |
appId: facebookConfig.appId, | |
status: facebookConfig.status, | |
cookie: facebookConfig.cookie, | |
xfbml: facebookConfig.xfbml, | |
version: facebookConfig.version | |
}); | |
}; | |
}; | |
init(); | |
this.checkConnect = function(){ | |
return connect.status(); | |
}; | |
this.getProfile = function() { | |
return getProfile.profile(); | |
} | |
}]); | |
/** check status connect*/ | |
facebookFactory.factory('connect', function($q) { | |
return { | |
status: function() { | |
var deferred = $q.defer(); | |
FB.Event.subscribe('auth.authResponseChange', function(res) { | |
if (res.status === 'connected') { | |
deferred.resolve(res); | |
} else { | |
deferred.reject('error'); | |
} | |
}); | |
return deferred.promise; | |
} | |
}; | |
}); | |
/** get profile */ | |
facebookFactory.factory('getProfile', function($q) { | |
return { | |
profile: function() { | |
var deferred = $q.defer(); | |
FB.api('/me', { | |
fields: 'name,last_name,email' | |
}, function(response) { | |
console.log(response); | |
if (!response || response.error) { | |
deferred.reject('Error occured'); | |
} else { | |
deferred.resolve(response); | |
} | |
}); | |
return deferred.promise; | |
} | |
}; | |
}); |
This file contains 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
/** service local storage */ | |
(function() { | |
var serviceLocalStorage = angular.module('serviceLocalStorage', []); | |
serviceLocalStorage.service('localStorage', function() { | |
this.set = function(key, value) { | |
return localStorage.setItem(key, value); | |
}; | |
this.get = function(key) { | |
return localStorage.getItem(key); | |
}; | |
this.del = function(key) { | |
return localStorage.removeItem(key); | |
} | |
this.cls = function() { | |
return localStorage.clear(); | |
} | |
}); | |
}()); |
0 ความคิดเห็น:
แสดงความคิดเห็น