UNPKG

angular-social-login

Version:

Angular Social Authentication (for PhoneGap/Cordova)

1 lines 2.72 kB
!function(e,o){"use strict";o.module("ngSocialLogin",["ngSocialLogin.facebook","ngSocialLogin.google","ngSocialLogin.phonegap"]),o.module("ngSocialLogin.google",[]).provider("$googleDefaults",function(){var e,t;return e={client_id:"",client_secret:"",redirect_uri:"http://localhost",scope:"profile"},t={useConfig:function(t){o.extend(e,t)},$get:function(){return{defaults:e}}}}).factory("$googleAuth",["$q","$http","$googleDefaults",function(e,t,n){var c;return c={getAuthURL:function(e){var t="https://accounts.google.com/o/oauth2/auth?",c=e||{client_id:n.defaults.client_id,redirect_uri:n.defaults.redirect_uri,response_type:"code",scope:n.defaults.scope},r=[];return o.forEach(c,function(e,o){r[r.length]=encodeURIComponent(o)+"="+encodeURIComponent(e)}),t+r.join("&")},getAccessToken:function(o,c,r){var i=e.defer(),u=r||"https://accounts.google.com/o/oauth2/token",a=c||{code:o,client_id:n.defaults.client_id,client_secret:n.defaults.client_secret,redirect_uri:n.defaults.redirect_uri,grant_type:"authorization_code"};return a.code=a.code||o,t({method:"POST",url:u,data:a}).success(function(e){i.resolve(e)}).error(function(){i.reject("err")}),i.promise}}}]),o.module("ngSocialLogin.facebook",[]).provider("$facebookDefaults",function(){var e,t;return e={client_id:"",redirect_uri:"https://www.facebook.com/connect/login_success.html",scope:"email,read_stream,publish_actions"},t={useConfig:function(t){o.extend(e,t)},$get:function(){return{defaults:e}}}}).factory("$facebookAuth",["$q","$http","$facebookDefaults",function(e,t,n){var c;return c={getAuthURL:function(e){var t="https://www.facebook.com/dialog/oauth?",c=e||{client_id:n.defaults.client_id,redirect_uri:n.defaults.redirect_uri,response_type:"token",scope:n.defaults.scope},r=[];return o.forEach(c,function(e,o){r[r.length]=encodeURIComponent(o)+"="+encodeURIComponent(e)}),t+r.join("&")}}}]),o.module("ngSocialLogin.phonegap",[]).factory("$socialAuthCordova",["$q","$http","$googleAuth","$facebookAuth",function(o,t,n,c){var r;return r={loginGoogle:function(e,o,t){return r.login("google",e,o,t)},loginFacebook:function(e,o,t){return r.login("facebook",e,o,t)},login:function(t,r,i,u){var a,l,s=o.defer();switch(t){case"google":a=n.getAuthURL(r);break;case"facebook":a=c.getAuthURL(r);break;default:s.reject("Social site not set!")}return l=e.open(a,"_blank","location=no,toolbar=no"),l.addEventListener("loadstart",function(e){var o=/\?code=(.+)$/.exec(e.url),t=/\?error=(.+)$/.exec(e.url),c=/access_token=(.*?)&/.exec(e.url);t&&(l.close(),s.reject(t)),o&&n.getAccessToken(o[1].split("&")[0],i,u).then(function(e){l.close(),s.resolve(e)},function(e){l.close(),s.reject(e)}),c&&(l.close(),s.resolve(c[0]))}),s.promise}}}])}(window,window.angular);