-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauthio-login.js
93 lines (82 loc) · 2.61 KB
/
oauthio-login.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
@fileOverview
@toc
*/
'use strict';
angular.module('resseguie.angular-oauthio-login', [])
.factory('drrOauthioLogin', [ '$q', function ($q) {
//public methods & properties
var self ={
initialize : function(key){
OAuth.initialize(key);
},
login : function(provider){
provider = provider.toLowerCase();
var deferred = $q.defer();
OAuth.popup(provider, function(err, res){
if(err) {
deferred.reject(err);
}else{
var result = {};
result.endpoint = res;
res.me(['name','alias','email','bio','avatar'])
.done(function(data){
result.user = data;
deferred.resolve(result);
});
}
});
return deferred.promise;
}
};
//private methods and properties - should ONLY expose methods and properties publicly
//(via the 'return' object) that are supposed to be used; everything else (helper methods
//that aren't supposed to be called externally) should be private.
return self;
}])
.directive('drrOauthioLogin',
['drrOauthioLogin', function(oauthioLogin){
return {
restrict : "EA",
template : '<div class="oauthio-login">'+
'<button class="btn btn-s btn-primary" ng-click="login()">'+
'<i class="fa fa-{{providerIcon | lowercase}}" style="margin-right: 5px;"></i> Connect with {{oauthProvider}}'+
'</button>'+
'</div>',
scope : {
onLogin : "&", // callback function
oauthioKey : "@", // OAuthi.io public key
oauthProvider : "@", // OAuth provider to authenticate with
providerIcon : "@" // optional Font Awesome icon name to use
},
link: function(scope,element,attrs){
// Default to fa-{{oauthProvider}} if no icon specified
attrs.$observe('providerIcon',function(value){
if(!value){ scope.providerIcon = scope.oauthProvider.toLowerCase();}
});
scope.login = function(){
if(scope.oauthioKey){
oauthioLogin.initialize(scope.oauthioKey);
var promise = oauthioLogin.login(scope.oauthProvider);
promise.then(function(result){
var oauthUser = {};
oauthUser.endpoint = result.endpoint;
oauthUser.user = result.user;
oauthUser.provider = scope.oauthProvider;
oauthUser.error = null;
scope.onLogin(oauthUser);
},function(error){
var oauthUser = {};
oauthUser.endpoint = null;
oauthUser.user = null;
oauthUser.error = error;
oauthUser.provider = scope.oauthProvider;
scope.oauthUser = oauthUser;
scope.onLogin(oauthUser);
});
}
};
}
};
}]
);