Skip to content

Instantly share code, notes, and snippets.

@swampie
Created April 7, 2014 09:06
Show Gist options
  • Save swampie/10017032 to your computer and use it in GitHub Desktop.
Save swampie/10017032 to your computer and use it in GitHub Desktop.
Gist for Social Login tutorial
$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
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;
}}
}]);
FB.Event.subscribe('auth.statusChange', function(response) {
$rootScope.$broadcast("fb_statusChange", {'response': response});
});
$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);
})
});
}
$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);
}
});
}
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