Instantly share code, notes, and snippets.
Created
April 7, 2014 09:06
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save swampie/10017032 to your computer and use it in GitHub Desktop.
Gist for Social Login tutorial
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
$scope.connected = []; | |
$scope.availables = accounts.getClient(auth).query({},function(d){ | |
d.$then(function(i){ | |
var available = availableSso; | |
var connected = i.data; | |
var names = []; | |
$(connected).each(function(i,n){ | |
names.push(n.from) | |
}) | |
$scope.connected = names; | |
var ret = []; | |
available.forEach(function(key) { | |
if (-1 === names.indexOf(key)) { | |
ret.push(key); | |
} | |
}, this); | |
$scope.available = ret; | |
}); | |
}); | |
$scope.available = availableSso |
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
window.app.factory('accounts',['$resource','baseServerUrl', function($resource,baseUrl){ | |
return { | |
getClient:function(auth){ | |
var resource = $resource(baseUrl+'/social/:sso', {sso:'@id'}, { | |
unlink: { | |
method:'DELETE', | |
isArray:false, | |
headers:{'X-BB-SESSION':auth.getToken()} | |
}, | |
query: { | |
method:'GET', | |
isArray:true, | |
headers:{'X-BB-SESSION':auth.getToken()}, | |
transformResponse: function (data, headersGetter) { | |
var data = JSON.parse(data)["data"]; | |
return data | |
} | |
} | |
}); | |
return resource; | |
}} | |
}]); |
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
FB.Event.subscribe('auth.statusChange', function(response) { | |
$rootScope.$broadcast("fb_statusChange", {'response': response}); | |
}); |
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
$scope.logincb = function(t,social,isLink){ | |
var token = t; | |
var link = isLink ? 'linkWith':'loginWith'; | |
var headers = {'Content-Type': 'application/json'} | |
if(isLink){ | |
headers['X-BB-SESSION'] = auth.getToken(); | |
} | |
$scope.$apply(function(){ | |
$http({ | |
method: isLink?'PUT':'POST', | |
url: serverUrl+"/social/"+social+"?oauth_token="+token+"&oauth_secret="+token, | |
data:{}, | |
headers: headers | |
}) | |
.success(function(data){ | |
if(!isLink){ | |
auth.setUser(data["data"].user,{"sso":social,"auth_token":token,"auth_secret":token},data["data"]["X-BB-SESSION"]); | |
$location.path("/posts") | |
}else{ | |
auth.getLogins().push(social); | |
} | |
}).error(function(data){ | |
console.log(data); | |
}) | |
}); | |
} |
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
$scope.googlelogin = function(isLink){ | |
$scope.gapi.auth.authorize({"client_id":googleAppId, | |
"scope":["https://www.googleapis.com/auth/plus.login", | |
"https://www.googleapis.com/auth/plus.me", | |
"https://www.googleapis.com/auth/userinfo.email", | |
"https://www.googleapis.com/auth/userinfo.profile"]}, | |
function(t){ | |
$scope.logincb(t["access_token"],'google',isLink); | |
}) | |
} | |
$scope.facebooklogin = function(isLink){ | |
$scope.fb.login(function(response){ | |
if (response.status === 'connected') { | |
var token = response.authResponse.accessToken; | |
$scope.logincb(token,'facebook',isLink); | |
} | |
}); | |
} |
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
window.app = angular.module("MyDearDiary",['ngCookies','ngResource']); | |
window.app.config(['$routeProvider','$locationProvider','$httpProvider', function( | |
$routeProvider,$locationProvider,$httpProvider) { | |
$routeProvider.when('/',{templateUrl:'js/app/views/index.html',controller:'MainCtrl'}) | |
$routeProvider.when('/posts',{templateUrl:'js/app/views/posts.html',controller:'PostsCtrl'}) | |
$routeProvider.when('/link',{templateUrl:'js/app/views/linkAccount.html',controller:'LinkAccountCtrl'}) | |
$routeProvider.when('/posts/new',{templateUrl:'/js/app/views/formPost.html',controller:'NewPostCtrl'}) | |
.otherwise({redirectTo:'/'}) | |
$locationProvider.html5Mode(false).hashPrefix('!') | |
$httpProvider.defaults.useXDomain = true; | |
$httpProvider.defaults.headers.common["X-BAASBOX-APPCODE"] = '1234567890' | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment