Created
August 9, 2013 04:18
-
-
Save clouddueling/6191166 to your computer and use it in GitHub Desktop.
Using facebook with AngularJS
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
| services.factory('Facebook', function ($rootScope) { | |
| return { | |
| getLoginStatus:function () { | |
| FB.getLoginStatus(function (response) { | |
| $rootScope.$broadcast("fb_statusChange", { | |
| 'status': response.status | |
| }); | |
| }, true); | |
| }, | |
| login:function () { | |
| FB.getLoginStatus(function (response) { | |
| switch (response.status) { | |
| case 'connected': | |
| $rootScope.$broadcast('fb_connected', { | |
| facebook_id: response.authResponse.userID | |
| }); | |
| break; | |
| case 'not_authorized' || 'unknown': | |
| // 'not_authorized' || 'unknown': doesn't seem to work | |
| FB.login(function (response) { | |
| if (response.authResponse) { | |
| $rootScope.$broadcast('fb_connected', { | |
| facebook_id: response.authResponse.userID, | |
| userNotAuthorized: true | |
| }); | |
| } else { | |
| $rootScope.$broadcast('fb_login_failed'); | |
| } | |
| }, { | |
| scope:'email' | |
| }); | |
| break; | |
| default: | |
| FB.login(function (response) { | |
| if (response.authResponse) { | |
| $rootScope.$broadcast('fb_connected', {facebook_id:response.authResponse.userID | |
| }); | |
| $rootScope.$broadcast('fb_get_login_status'); | |
| } else { | |
| $rootScope.$broadcast('fb_login_failed'); | |
| } | |
| }); | |
| break; | |
| } | |
| }, true); | |
| }, | |
| logout:function () { | |
| FB.logout(function (response) { | |
| if (response) { | |
| $rootScope.$broadcast('fb_logout_succeded'); | |
| } else { | |
| $rootScope.$broadcast('fb_logout_failed'); | |
| } | |
| }); | |
| }, | |
| unsubscribe:function () { | |
| FB.api("/me/permissions", "DELETE", function (response) { | |
| $rootScope.$broadcast('fb_get_login_status'); | |
| }); | |
| } | |
| }; | |
| }); |
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
| <?php | |
| class Api_V1_Facebook_Controller extends Base_Controller | |
| { | |
| public function post_auth() | |
| { | |
| try { | |
| $s = extend(array( | |
| 'facebook_id' => 0, | |
| 'first_name' => '', | |
| 'last_name' => '', | |
| 'email' => '', | |
| 'picture' => '', | |
| 'serie_id' => 0, | |
| ), sr()); | |
| if ($s['facebook_id'] == 0) | |
| throw new Exception("No Facebook data found."); | |
| $user = User::where_facebook_id($s['facebook_id']) | |
| ->first(); | |
| if (! $user) { | |
| $user = User::create(array( | |
| 'name' => "{$s['first_name']} {$s['last_name']}", | |
| 'type' => 'account', | |
| 'facebook_id' => $s['facebook_id'], | |
| 'facebook_image' => $s['picture'], | |
| 'perm_contacts' => 0, | |
| )); | |
| $user->account_user_id = $user->id; | |
| $user->save(); | |
| } | |
| Auth::login($user->id, 1); | |
| $email_check = User::where_email($s['email']) | |
| ->where_deleted(0) | |
| ->first(); | |
| if (! $email_check) | |
| $user->email = $s['email']; | |
| if (empty($user->name)) | |
| $user->name = "{$s['first_name']} {$s['last_name']}"; | |
| $user->facebook_id = $s['facebook_id']; | |
| $user->save(); | |
| Cookie::forever('facebook', serialize($s)); | |
| $serie = Serie::find($s['serie_id']); | |
| if (! $serie) | |
| throw new Exception("Could not find product."); | |
| if (! $serie->is_member($user->id) && ! $serie->is_expert($user->id)) { | |
| DB::table('serie_user') | |
| ->insert(array( | |
| 'user_id' => $user->id, | |
| 'account_user_id' => $user->account_user_id, | |
| 'serie_id' => $serie->id, | |
| 'type' => 'member', | |
| )); | |
| } | |
| $data = array( | |
| 'facebook' => $s, | |
| ); | |
| return json($data); | |
| } catch(Exception $e) { | |
| return Report::log($e->getMessage()); | |
| } | |
| } | |
| public function get_session() | |
| { | |
| try { | |
| $facebook = Cookie::get('facebook'); | |
| return json(unserialize($facebook)); | |
| } catch(Exception $e) { | |
| return Report::log($e->getMessage()); | |
| } | |
| } | |
| public function get_logout() | |
| { | |
| try { | |
| session_destroy(); | |
| unset($_COOKIE); | |
| } catch(Exception $e) { | |
| return Report::log($e->getMessage()); | |
| } | |
| } | |
| } |
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
| controllers.controller('MainCtrl', function($scope, $location, Facebook, $rootScope, $http, $location, Upload, Auth, User, Question, Category, Serie, Record, Location, Popup, Process, Card, Question) { | |
| $scope.main = { | |
| facebookAppId: '', | |
| user: {}, | |
| serie: {}, | |
| cloudUrl: $('[data-cloud-url]').data('cloud-url'), | |
| serieId: $('[data-serie-id]').data('serie-id'), | |
| active: { | |
| categoryId: 0, | |
| category: {}, | |
| page: 'locations', | |
| locationgroupId: 0, | |
| location: {}, | |
| newAnswer: {}, | |
| newEvent: {}, | |
| newLocation: {}, | |
| newQuestion: {}, | |
| newRsvp: {}, | |
| }, | |
| invite: {}, | |
| values: {}, | |
| }; | |
| /** | |
| * https://github.com/Terumi/AngularJS-Facebook-Login | |
| */ | |
| $scope.info = {}; | |
| $rootScope.$on("fb_statusChange", function (event, args) { | |
| $rootScope.fb_status = args.status; | |
| $rootScope.$apply(); | |
| }); | |
| $rootScope.$on("fb_get_login_status", function () { | |
| Facebook.getLoginStatus(); | |
| }); | |
| $rootScope.$on("fb_login_failed", function () { | |
| console.log("fb_login_failed"); | |
| }); | |
| $rootScope.$on("fb_logout_succeded", function () { | |
| console.log("fb_logout_succeded"); | |
| $rootScope.id = ""; | |
| }); | |
| $rootScope.$on("fb_logout_failed", function () { | |
| console.log("fb_logout_failed!"); | |
| }); | |
| $rootScope.$on("fb_connected", function (event, args) { | |
| var params = {}; | |
| function authenticateViaFacebook(parameters) { | |
| //posts some user data to a page that will check them against some db | |
| $http.post('/api/v1/facebook/auth', parameters).success(function () { | |
| $scope.updateSession(); | |
| $scope.loadAuth(); | |
| }); | |
| } | |
| if (args.userNotAuthorized === true) { | |
| //if the user has not authorized the app, we must write his credentials in our database | |
| console.log("user is connected to facebook but has not authorized our app"); | |
| FB.api({ | |
| method:'fql.multiquery', | |
| queries:{ | |
| 'q1':'SELECT uid, first_name, last_name, email FROM user WHERE uid = ' + args.facebook_id, | |
| 'q2':'SELECT url FROM profile_pic WHERE width=800 AND height=800 AND id = ' + args.facebook_id | |
| } | |
| }, function (data) { | |
| //let's built the data to send to php in order to create our new user | |
| l(data); | |
| params = { | |
| serie_id: $scope.main.serieId, | |
| facebook_id: data[0]['fql_result_set'][0].uid, | |
| first_name: data[0]['fql_result_set'][0].first_name, | |
| last_name: data[0]['fql_result_set'][0].last_name, | |
| email: data[0]['fql_result_set'][0].email, | |
| picture: data[1]['fql_result_set'][0].url | |
| } | |
| authenticateViaFacebook(params); | |
| }); | |
| } else { | |
| console.log("user is connected to facebook and has authorized our app"); | |
| //the parameter needed in that case is just the users facebook id | |
| params = {'facebook_id':args.facebook_id}; | |
| authenticateViaFacebook(params); | |
| } | |
| }); | |
| $rootScope.updateSession = function () { | |
| //reads the session variables if exist from php | |
| $http.get('/api/v1/facebook/session').success(function (data) { | |
| //and transfers them to angular | |
| $rootScope.session = data; | |
| }); | |
| }; | |
| $rootScope.updateSession(); | |
| $scope.loginFacebook = function () { | |
| Facebook.login(); | |
| }; | |
| $scope.loadAuth(); | |
| $scope.loadSerie(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment