auth0-angular
Advanced tools
Comparing version 1.1.2 to 1.1.4
@@ -232,2 +232,11 @@ (function () { | ||
}; | ||
function forbidden() { | ||
if (config.loginUrl) { | ||
$location.path(config.loginUrl); | ||
} else if (config.loginState) { | ||
$injector.get('$state').go(config.loginState); | ||
} else { | ||
callHandler('forbidden'); | ||
} | ||
} | ||
// Redirect mode | ||
@@ -243,2 +252,6 @@ $rootScope.$on('$locationChangeStart', function (e) { | ||
if (storedValues && storedValues.idToken) { | ||
if (auth.hasTokenExpired(storedValues.idToken)) { | ||
forbidden(); | ||
return; | ||
} | ||
onSigninOk(storedValues.idToken, storedValues.accessToken, storedValues.state, e); | ||
@@ -259,4 +272,4 @@ return; | ||
}); | ||
$rootScope.$on('auth0.forbidden', function (e, response) { | ||
callHandler('forbidden', { response: response }); | ||
$rootScope.$on('auth0.forbidden', function () { | ||
forbidden(); | ||
}); | ||
@@ -263,0 +276,0 @@ if (config.loginUrl) { |
@@ -1,1 +0,1 @@ | ||
!function(){angular.module("auth0",["auth0.storage","auth0.service","auth0.interceptor","auth0.utils"]),angular.module("auth0.utils",[]).provider("authUtils",function(){var a={capitalize:function(a){return a?a.charAt(0).toUpperCase()+a.substring(1).toLowerCase():null},urlBase64Decode:function(a){var b=a.replace("-","+").replace("_","/");switch(b.length%4){case 0:break;case 2:b+="==";break;case 3:b+="=";break;default:throw"Illegal base64url string!"}return window.atob(b)}};angular.extend(this,a),this.$get=["$rootScope","$q",function(b,c){var d={};return angular.extend(d,a),d.safeApply=function(a){var c=b.$root.$$phase;"$apply"===c||"$digest"===c?a&&"function"==typeof a&&a():b.$apply(a)},d.promisify=function(a,b){return angular.isFunction(a)?function(e){e=Array.prototype.slice.call(arguments);var f=c.defer(),g=function(a,b,c){a&&f.reject(a),c=Array.prototype.slice.call(arguments,1),f.resolve(c.length>1?c:b)};return e.push(d.applied(g)),a.apply(b,e),f.promise.spread=f.promise.spread||function(a,b){return f.promise.then(function(b){return Array.isArray(b)?a.apply(null,b):a(b)},b)},f.promise}:void 0},d.applied=function(a){return function(){var b=arguments;d.safeApply(function(){a.apply(null,b)})}},d}]}),angular.module("auth0.interceptor",[]).factory("authInterceptor",["$rootScope","$q","$injector",function(a,b,c){return{request:function(a){if(!c.has("auth"))return a;var b=c.get("auth");return a.headers=a.headers||{},b.idToken&&!a.headers.Authorization&&(a.headers.Authorization="Bearer "+b.idToken),a},responseError:function(c){return 401===c.status&&a.$broadcast("auth0.forbidden",c),b.reject(c)}}}]),angular.module("auth0.storage",["ngCookies"]).service("authStorage",["$cookieStore",function(a){this.store=function(b,c,d){a.put("idToken",b),a.put("accessToken",c),d&&a.put("state",d)},this.get=function(){return{idToken:a.get("idToken"),accessToken:a.get("accessToken"),state:a.get("state")}},this.remove=function(){a.remove("idToken"),a.remove("accessToken"),a.remove("state")}}]),angular.module("auth0.service",["auth0.storage","auth0.utils"]).provider("auth",["authUtilsProvider",function(a){var b={callbackOnLocationHash:!0},c=this;this.init=function(a,c){if(!c&&"undefined"==typeof Auth0Widget&&"undefined"==typeof Auth0)throw new Error("You must add either Auth0Widget.js or Auth0.js");if(!a)throw new Error("You must set options when calling init");this.loginUrl=a.loginUrl,this.loginState=a.loginState,this.clientID=a.clientID,this.sso=a.sso;var d=c;d||"undefined"==typeof Auth0Widget||(d=Auth0Widget),d||"undefined"==typeof Auth0||(d=Auth0),this.auth0lib=new d(angular.extend(b,a)),this.auth0lib.getClient?(this.auth0js=this.auth0lib.getClient(),this.isWidget=!0):(this.auth0js=this.auth0lib,this.isWidget=!1)},this.eventHandlers={},this.on=function(a,b){this.eventHandlers[a]||(this.eventHandlers[a]=[]),this.eventHandlers[a].push(b)};var d=["loginSuccess","loginFailure","logout","forbidden"];angular.forEach(d,function(b){c["add"+a.capitalize(b)+"Handler"]=function(a){c.on(b,a)}}),this.$get=["$rootScope","$q","$injector","authStorage","$window","$location","authUtils",function(a,b,d,e,f,g,h){var i={isAuthenticated:!1},j=function(a){return c.eventHandlers[a]},k=function(a,b){angular.forEach(j(a)||[],function(a){d.invoke(a,i,b)})},l=function(a,b,c,d){e.store(a,b,c);var f=i.getProfile(a),g={idToken:a,accessToken:b,state:c,isAuthenticated:!0};return angular.extend(i,g),k("loginSuccess",angular.extend({profile:f,locationEvent:d},g)),f};a.$on("$locationChangeStart",function(a){var b=c.auth0lib.parseHash(f.location.hash);if(!i.isAuthenticated){if(b&&b.id_token)return void l(b.id_token,b.access_token,b.state,a);var d=e.get();if(d&&d.idToken)return void l(d.idToken,d.accessToken,d.state,a);c.sso&&c.auth0js.getSSOData(h.applied(function(a,b){b.sso&&i.signin({popup:!1,connection:b.lastUsedConnection.strategy},c.auth0js)}))}}),a.$on("auth0.forbidden",function(a,b){k("forbidden",{response:b})}),c.loginUrl&&a.$on("$routeChangeStart",function(a,b){b.$$route&&b.$$route.requiresLogin&&(i.isAuthenticated||g.path(c.loginUrl))}),c.loginState&&a.$on("$stateChangeStart",function(a,b){b.data&&b.data.requiresLogin&&(i.isAuthenticated||(a.preventDefault(),d.get("$state").go(c.loginState)))}),i.config=c;var m=function(a){var b=j("loginSuccess");if(!(a.popup||a.username||b&&0!==b.length))throw new Error("You must define a loginSuccess handler if not using popup mode or not doing ro call because that means you are doing a redirect")};return i.hookEvents=function(){},i.hasTokenExpired=function(a){if(!a)return!0;var b=a.split(".");if(3!==b.length)return!0;var c=h.urlBase64Decode(b[1]);if(!c)return!0;try{c=JSON.parse(c)}catch(d){return!0}if(!c.exp)return!0;var e=new Date(0);return e.setUTCSeconds(c.exp),isNaN(e)?!0:e.valueOf()>(new Date).valueOf()?!1:!0},i.getToken=function(a,b){b=b||{scope:"openid"};var d=h.promisify(c.auth0js.getDelegationToken,c.auth0js);return d(a,i.idToken,b).then(function(a){return a.id_token})},i.refreshToken=function(a){return i.getToken(c.clientID,a)},i.signin=function(a,b){a=a||{},m(a);var d=b||c.auth0lib,e=h.promisify(d.signin,d),f=c.isWidget?e(a,null):e(a);return f.spread(function(a,b,c,d){return l(b,c,d)})["catch"](function(a){throw k("loginFailure",{error:a}),a})},i.signup=function(a){a=a||{},m(a);var b=c.auth0lib,d=h.promisify(b.signup,b),e=c.isWidget?d(a,null):d(a);return e.spread(function(a,b,c,d){return l(b,c,d)})["catch"](function(a){throw k("loginFailure",{error:a}),a})},i.reset=function(a){a=a||{};var b=c.auth0lib,d=h.promisify(b.reset,b);return c.isWidget?d(a,null):d(a)},i.signout=function(){e.remove(),i.profile=null,i.idToken=null,i.state=null,i.accessToken=null,i.isAuthenticated=!1,k("logout")},i.getProfile=function(a){var b=h.promisify(c.auth0lib.getProfile,c.auth0lib);return b(a||i.idToken).then(function(a){return i.profile=a,a})},i}]}])}(); | ||
!function(){angular.module("auth0",["auth0.storage","auth0.service","auth0.interceptor","auth0.utils"]),angular.module("auth0.utils",[]).provider("authUtils",function(){var a={capitalize:function(a){return a?a.charAt(0).toUpperCase()+a.substring(1).toLowerCase():null},urlBase64Decode:function(a){var b=a.replace("-","+").replace("_","/");switch(b.length%4){case 0:break;case 2:b+="==";break;case 3:b+="=";break;default:throw"Illegal base64url string!"}return window.atob(b)}};angular.extend(this,a),this.$get=["$rootScope","$q",function(b,c){var d={};return angular.extend(d,a),d.safeApply=function(a){var c=b.$root.$$phase;"$apply"===c||"$digest"===c?a&&"function"==typeof a&&a():b.$apply(a)},d.promisify=function(a,b){return angular.isFunction(a)?function(e){e=Array.prototype.slice.call(arguments);var f=c.defer(),g=function(a,b,c){a&&f.reject(a),c=Array.prototype.slice.call(arguments,1),f.resolve(c.length>1?c:b)};return e.push(d.applied(g)),a.apply(b,e),f.promise.spread=f.promise.spread||function(a,b){return f.promise.then(function(b){return Array.isArray(b)?a.apply(null,b):a(b)},b)},f.promise}:void 0},d.applied=function(a){return function(){var b=arguments;d.safeApply(function(){a.apply(null,b)})}},d}]}),angular.module("auth0.interceptor",[]).factory("authInterceptor",["$rootScope","$q","$injector",function(a,b,c){return{request:function(a){if(!c.has("auth"))return a;var b=c.get("auth");return a.headers=a.headers||{},b.idToken&&!a.headers.Authorization&&(a.headers.Authorization="Bearer "+b.idToken),a},responseError:function(c){return 401===c.status&&a.$broadcast("auth0.forbidden",c),b.reject(c)}}}]),angular.module("auth0.storage",["ngCookies"]).service("authStorage",["$cookieStore",function(a){this.store=function(b,c,d){a.put("idToken",b),a.put("accessToken",c),d&&a.put("state",d)},this.get=function(){return{idToken:a.get("idToken"),accessToken:a.get("accessToken"),state:a.get("state")}},this.remove=function(){a.remove("idToken"),a.remove("accessToken"),a.remove("state")}}]),angular.module("auth0.service",["auth0.storage","auth0.utils"]).provider("auth",["authUtilsProvider",function(a){var b={callbackOnLocationHash:!0},c=this;this.init=function(a,c){if(!c&&"undefined"==typeof Auth0Widget&&"undefined"==typeof Auth0)throw new Error("You must add either Auth0Widget.js or Auth0.js");if(!a)throw new Error("You must set options when calling init");this.loginUrl=a.loginUrl,this.loginState=a.loginState,this.clientID=a.clientID,this.sso=a.sso;var d=c;d||"undefined"==typeof Auth0Widget||(d=Auth0Widget),d||"undefined"==typeof Auth0||(d=Auth0),this.auth0lib=new d(angular.extend(b,a)),this.auth0lib.getClient?(this.auth0js=this.auth0lib.getClient(),this.isWidget=!0):(this.auth0js=this.auth0lib,this.isWidget=!1)},this.eventHandlers={},this.on=function(a,b){this.eventHandlers[a]||(this.eventHandlers[a]=[]),this.eventHandlers[a].push(b)};var d=["loginSuccess","loginFailure","logout","forbidden"];angular.forEach(d,function(b){c["add"+a.capitalize(b)+"Handler"]=function(a){c.on(b,a)}}),this.$get=["$rootScope","$q","$injector","authStorage","$window","$location","authUtils",function(a,b,d,e,f,g,h){function i(){c.loginUrl?g.path(c.loginUrl):c.loginState?d.get("$state").go(c.loginState):l("forbidden")}var j={isAuthenticated:!1},k=function(a){return c.eventHandlers[a]},l=function(a,b){angular.forEach(k(a)||[],function(a){d.invoke(a,j,b)})},m=function(a,b,c,d){e.store(a,b,c);var f=j.getProfile(a),g={idToken:a,accessToken:b,state:c,isAuthenticated:!0};return angular.extend(j,g),l("loginSuccess",angular.extend({profile:f,locationEvent:d},g)),f};a.$on("$locationChangeStart",function(a){var b=c.auth0lib.parseHash(f.location.hash);if(!j.isAuthenticated){if(b&&b.id_token)return void m(b.id_token,b.access_token,b.state,a);var d=e.get();if(d&&d.idToken)return j.hasTokenExpired(d.idToken)?void i():void m(d.idToken,d.accessToken,d.state,a);c.sso&&c.auth0js.getSSOData(h.applied(function(a,b){b.sso&&j.signin({popup:!1,connection:b.lastUsedConnection.strategy},c.auth0js)}))}}),a.$on("auth0.forbidden",function(){i()}),c.loginUrl&&a.$on("$routeChangeStart",function(a,b){b.$$route&&b.$$route.requiresLogin&&(j.isAuthenticated||g.path(c.loginUrl))}),c.loginState&&a.$on("$stateChangeStart",function(a,b){b.data&&b.data.requiresLogin&&(j.isAuthenticated||(a.preventDefault(),d.get("$state").go(c.loginState)))}),j.config=c;var n=function(a){var b=k("loginSuccess");if(!(a.popup||a.username||b&&0!==b.length))throw new Error("You must define a loginSuccess handler if not using popup mode or not doing ro call because that means you are doing a redirect")};return j.hookEvents=function(){},j.hasTokenExpired=function(a){if(!a)return!0;var b=a.split(".");if(3!==b.length)return!0;var c=h.urlBase64Decode(b[1]);if(!c)return!0;try{c=JSON.parse(c)}catch(d){return!0}if(!c.exp)return!0;var e=new Date(0);return e.setUTCSeconds(c.exp),isNaN(e)?!0:e.valueOf()>(new Date).valueOf()?!1:!0},j.getToken=function(a,b){b=b||{scope:"openid"};var d=h.promisify(c.auth0js.getDelegationToken,c.auth0js);return d(a,j.idToken,b).then(function(a){return a.id_token})},j.refreshToken=function(a){return j.getToken(c.clientID,a)},j.signin=function(a,b){a=a||{},n(a);var d=b||c.auth0lib,e=h.promisify(d.signin,d),f=c.isWidget?e(a,null):e(a);return f.spread(function(a,b,c,d){return m(b,c,d)})["catch"](function(a){throw l("loginFailure",{error:a}),a})},j.signup=function(a){a=a||{},n(a);var b=c.auth0lib,d=h.promisify(b.signup,b),e=c.isWidget?d(a,null):d(a);return e.spread(function(a,b,c,d){return m(b,c,d)})["catch"](function(a){throw l("loginFailure",{error:a}),a})},j.reset=function(a){a=a||{};var b=c.auth0lib,d=h.promisify(b.reset,b);return c.isWidget?d(a,null):d(a)},j.signout=function(){e.remove(),j.profile=null,j.idToken=null,j.state=null,j.accessToken=null,j.isAuthenticated=!1,l("logout")},j.getProfile=function(a){var b=h.promisify(c.auth0lib.getProfile,c.auth0lib);return b(a||j.idToken).then(function(a){return j.profile=a,a})},j}]}])}(); |
# 1.1.x | ||
## 1.1.3 | ||
* Implemented token expired functionality. Fixes #54 | ||
* Updated all documentation | ||
## 1.1.2 | ||
@@ -4,0 +8,0 @@ * Added `signup` and `reset` functions to trigger the widget in those modes |
@@ -232,2 +232,11 @@ (function () { | ||
}; | ||
function forbidden() { | ||
if (config.loginUrl) { | ||
$location.path(config.loginUrl); | ||
} else if (config.loginState) { | ||
$injector.get('$state').go(config.loginState); | ||
} else { | ||
callHandler('forbidden'); | ||
} | ||
} | ||
// Redirect mode | ||
@@ -243,2 +252,6 @@ $rootScope.$on('$locationChangeStart', function (e) { | ||
if (storedValues && storedValues.idToken) { | ||
if (auth.hasTokenExpired(storedValues.idToken)) { | ||
forbidden(); | ||
return; | ||
} | ||
onSigninOk(storedValues.idToken, storedValues.accessToken, storedValues.state, e); | ||
@@ -259,4 +272,4 @@ return; | ||
}); | ||
$rootScope.$on('auth0.forbidden', function (e, response) { | ||
callHandler('forbidden', { response: response }); | ||
$rootScope.$on('auth0.forbidden', function () { | ||
forbidden(); | ||
}); | ||
@@ -263,0 +276,0 @@ if (config.loginUrl) { |
@@ -232,2 +232,11 @@ (function () { | ||
}; | ||
function forbidden() { | ||
if (config.loginUrl) { | ||
$location.path(config.loginUrl); | ||
} else if (config.loginState) { | ||
$injector.get('$state').go(config.loginState); | ||
} else { | ||
callHandler('forbidden'); | ||
} | ||
} | ||
// Redirect mode | ||
@@ -243,2 +252,6 @@ $rootScope.$on('$locationChangeStart', function (e) { | ||
if (storedValues && storedValues.idToken) { | ||
if (auth.hasTokenExpired(storedValues.idToken)) { | ||
forbidden(); | ||
return; | ||
} | ||
onSigninOk(storedValues.idToken, storedValues.accessToken, storedValues.state, e); | ||
@@ -259,4 +272,4 @@ return; | ||
}); | ||
$rootScope.$on('auth0.forbidden', function (e, response) { | ||
callHandler('forbidden', { response: response }); | ||
$rootScope.$on('auth0.forbidden', function () { | ||
forbidden(); | ||
}); | ||
@@ -263,0 +276,0 @@ if (config.loginUrl) { |
@@ -232,2 +232,11 @@ (function () { | ||
}; | ||
function forbidden() { | ||
if (config.loginUrl) { | ||
$location.path(config.loginUrl); | ||
} else if (config.loginState) { | ||
$injector.get('$state').go(config.loginState); | ||
} else { | ||
callHandler('forbidden'); | ||
} | ||
} | ||
// Redirect mode | ||
@@ -243,2 +252,6 @@ $rootScope.$on('$locationChangeStart', function (e) { | ||
if (storedValues && storedValues.idToken) { | ||
if (auth.hasTokenExpired(storedValues.idToken)) { | ||
forbidden(); | ||
return; | ||
} | ||
onSigninOk(storedValues.idToken, storedValues.accessToken, storedValues.state, e); | ||
@@ -259,4 +272,4 @@ return; | ||
}); | ||
$rootScope.$on('auth0.forbidden', function (e, response) { | ||
callHandler('forbidden', { response: response }); | ||
$rootScope.$on('auth0.forbidden', function () { | ||
forbidden(); | ||
}); | ||
@@ -263,0 +276,0 @@ if (config.loginUrl) { |
@@ -232,2 +232,11 @@ (function () { | ||
}; | ||
function forbidden() { | ||
if (config.loginUrl) { | ||
$location.path(config.loginUrl); | ||
} else if (config.loginState) { | ||
$injector.get('$state').go(config.loginState); | ||
} else { | ||
callHandler('forbidden'); | ||
} | ||
} | ||
// Redirect mode | ||
@@ -243,2 +252,6 @@ $rootScope.$on('$locationChangeStart', function (e) { | ||
if (storedValues && storedValues.idToken) { | ||
if (auth.hasTokenExpired(storedValues.idToken)) { | ||
forbidden(); | ||
return; | ||
} | ||
onSigninOk(storedValues.idToken, storedValues.accessToken, storedValues.state, e); | ||
@@ -259,4 +272,4 @@ return; | ||
}); | ||
$rootScope.$on('auth0.forbidden', function (e, response) { | ||
callHandler('forbidden', { response: response }); | ||
$rootScope.$on('auth0.forbidden', function () { | ||
forbidden(); | ||
}); | ||
@@ -263,0 +276,0 @@ if (config.loginUrl) { |
@@ -232,2 +232,11 @@ (function () { | ||
}; | ||
function forbidden() { | ||
if (config.loginUrl) { | ||
$location.path(config.loginUrl); | ||
} else if (config.loginState) { | ||
$injector.get('$state').go(config.loginState); | ||
} else { | ||
callHandler('forbidden'); | ||
} | ||
} | ||
// Redirect mode | ||
@@ -243,2 +252,6 @@ $rootScope.$on('$locationChangeStart', function (e) { | ||
if (storedValues && storedValues.idToken) { | ||
if (auth.hasTokenExpired(storedValues.idToken)) { | ||
forbidden(); | ||
return; | ||
} | ||
onSigninOk(storedValues.idToken, storedValues.accessToken, storedValues.state, e); | ||
@@ -259,4 +272,4 @@ return; | ||
}); | ||
$rootScope.$on('auth0.forbidden', function (e, response) { | ||
callHandler('forbidden', { response: response }); | ||
$rootScope.$on('auth0.forbidden', function () { | ||
forbidden(); | ||
}); | ||
@@ -263,0 +276,0 @@ if (config.loginUrl) { |
@@ -232,2 +232,11 @@ (function () { | ||
}; | ||
function forbidden() { | ||
if (config.loginUrl) { | ||
$location.path(config.loginUrl); | ||
} else if (config.loginState) { | ||
$injector.get('$state').go(config.loginState); | ||
} else { | ||
callHandler('forbidden'); | ||
} | ||
} | ||
// Redirect mode | ||
@@ -243,2 +252,6 @@ $rootScope.$on('$locationChangeStart', function (e) { | ||
if (storedValues && storedValues.idToken) { | ||
if (auth.hasTokenExpired(storedValues.idToken)) { | ||
forbidden(); | ||
return; | ||
} | ||
onSigninOk(storedValues.idToken, storedValues.accessToken, storedValues.state, e); | ||
@@ -259,4 +272,4 @@ return; | ||
}); | ||
$rootScope.$on('auth0.forbidden', function (e, response) { | ||
callHandler('forbidden', { response: response }); | ||
$rootScope.$on('auth0.forbidden', function () { | ||
forbidden(); | ||
}); | ||
@@ -263,0 +276,0 @@ if (config.loginUrl) { |
@@ -232,2 +232,11 @@ (function () { | ||
}; | ||
function forbidden() { | ||
if (config.loginUrl) { | ||
$location.path(config.loginUrl); | ||
} else if (config.loginState) { | ||
$injector.get('$state').go(config.loginState); | ||
} else { | ||
callHandler('forbidden'); | ||
} | ||
} | ||
// Redirect mode | ||
@@ -243,2 +252,6 @@ $rootScope.$on('$locationChangeStart', function (e) { | ||
if (storedValues && storedValues.idToken) { | ||
if (auth.hasTokenExpired(storedValues.idToken)) { | ||
forbidden(); | ||
return; | ||
} | ||
onSigninOk(storedValues.idToken, storedValues.accessToken, storedValues.state, e); | ||
@@ -259,4 +272,4 @@ return; | ||
}); | ||
$rootScope.$on('auth0.forbidden', function (e, response) { | ||
callHandler('forbidden', { response: response }); | ||
$rootScope.$on('auth0.forbidden', function () { | ||
forbidden(); | ||
}); | ||
@@ -263,0 +276,0 @@ if (config.loginUrl) { |
@@ -232,2 +232,11 @@ (function () { | ||
}; | ||
function forbidden() { | ||
if (config.loginUrl) { | ||
$location.path(config.loginUrl); | ||
} else if (config.loginState) { | ||
$injector.get('$state').go(config.loginState); | ||
} else { | ||
callHandler('forbidden'); | ||
} | ||
} | ||
// Redirect mode | ||
@@ -243,2 +252,6 @@ $rootScope.$on('$locationChangeStart', function (e) { | ||
if (storedValues && storedValues.idToken) { | ||
if (auth.hasTokenExpired(storedValues.idToken)) { | ||
forbidden(); | ||
return; | ||
} | ||
onSigninOk(storedValues.idToken, storedValues.accessToken, storedValues.state, e); | ||
@@ -259,4 +272,4 @@ return; | ||
}); | ||
$rootScope.$on('auth0.forbidden', function (e, response) { | ||
callHandler('forbidden', { response: response }); | ||
$rootScope.$on('auth0.forbidden', function () { | ||
forbidden(); | ||
}); | ||
@@ -263,0 +276,0 @@ if (config.loginUrl) { |
@@ -232,2 +232,11 @@ (function () { | ||
}; | ||
function forbidden() { | ||
if (config.loginUrl) { | ||
$location.path(config.loginUrl); | ||
} else if (config.loginState) { | ||
$injector.get('$state').go(config.loginState); | ||
} else { | ||
callHandler('forbidden'); | ||
} | ||
} | ||
// Redirect mode | ||
@@ -243,2 +252,6 @@ $rootScope.$on('$locationChangeStart', function (e) { | ||
if (storedValues && storedValues.idToken) { | ||
if (auth.hasTokenExpired(storedValues.idToken)) { | ||
forbidden(); | ||
return; | ||
} | ||
onSigninOk(storedValues.idToken, storedValues.accessToken, storedValues.state, e); | ||
@@ -259,4 +272,4 @@ return; | ||
}); | ||
$rootScope.$on('auth0.forbidden', function (e, response) { | ||
callHandler('forbidden', { response: response }); | ||
$rootScope.$on('auth0.forbidden', function () { | ||
forbidden(); | ||
}); | ||
@@ -263,0 +276,0 @@ if (config.loginUrl) { |
@@ -232,2 +232,11 @@ (function () { | ||
}; | ||
function forbidden() { | ||
if (config.loginUrl) { | ||
$location.path(config.loginUrl); | ||
} else if (config.loginState) { | ||
$injector.get('$state').go(config.loginState); | ||
} else { | ||
callHandler('forbidden'); | ||
} | ||
} | ||
// Redirect mode | ||
@@ -243,2 +252,6 @@ $rootScope.$on('$locationChangeStart', function (e) { | ||
if (storedValues && storedValues.idToken) { | ||
if (auth.hasTokenExpired(storedValues.idToken)) { | ||
forbidden(); | ||
return; | ||
} | ||
onSigninOk(storedValues.idToken, storedValues.accessToken, storedValues.state, e); | ||
@@ -259,4 +272,4 @@ return; | ||
}); | ||
$rootScope.$on('auth0.forbidden', function (e, response) { | ||
callHandler('forbidden', { response: response }); | ||
$rootScope.$on('auth0.forbidden', function () { | ||
forbidden(); | ||
}); | ||
@@ -263,0 +276,0 @@ if (config.loginUrl) { |
{ | ||
"name": "auth0-angular", | ||
"version": "1.1.2", | ||
"version": "1.1.4", | ||
"main": "./build/auth0-angular.js", | ||
@@ -5,0 +5,0 @@ "devDependencies": { |
383
README.md
@@ -7,87 +7,360 @@ # Auth0 and AngularJS | ||
> **NPM / Bower users**: Find this dependecy as `auth0-angular`. Take into account that you will need to include either auth0-widget.js or auth0.js in order to run the examples using Bower or Auth0 Angular (they can be found as dependencies of this module). | ||
## Key Features | ||
## Tutorials | ||
* **User Login & Signup**: This module lets you easily sign in and sign up your users with any Social Provider, Enterprise Provider or Username and password. You can use the UI already made by Auth0 or create your own | ||
* **Persistent user authentication**: We'll take care of keeping the user logged in after page refresh, browser closed and so on. | ||
* **Authenticated API calls**: We'll take care of automatically adding the `JWT` in every request that is made to your API after the user is authenticated | ||
* **Events/Promise based services**: Our service supports both Events based actions as well as promise based ones | ||
* **Token management**: We'll handle the token storage and configuration all the time. You don't even need to know there's a token. | ||
There are two ways of implementing signin/singup. | ||
* [Login Widget](https://docs.auth0.com/login-widget2): A complete Login UI ready to go, that can be customized, translated and expanded. You can check the following getting started guides: | ||
* [JavaScript SDK](https://github.com/auth0/auth0.js): You can write your own UI from scratch in order to provide a more tailored user experience. | ||
## Installation | ||
### With the Login Widget | ||
You can install this plugin several ways | ||
The following guides will help you getting started: | ||
### Bower | ||
#### User/Password + Social Login with Auth0 Widget (redirect mode) | ||
````bash | ||
bower install auth0-angular | ||
```` | ||
Authenticate using the [Login Widget](https://docs.auth0.com/login-widget2) and listen to an event. Your angular app will be refreshed when it comes back from Auth0 as opposed to the __popup mode__. | ||
### NPM | ||
→ [Read the tutorial](docs/widget-redirect.md) | ||
![widget_guide](https://cloudup.com/ctj4-5kRNFf+) | ||
````bash | ||
npm install auth0-angular | ||
```` | ||
#### User/Password + Social Login with Auth0 Widget (popup mode) | ||
### CDN | ||
Authenticate using the [Login Widget](https://docs.auth0.com/login-widget2) and get back a promise. Your angular app won't refresh because it uses `window.open` popup for social providers and an ajax call for user/password auth). | ||
→ [Read the tutorial](docs/widget-popup.md) | ||
![widget_popup](https://cloudup.com/cg8u9kVV5Vh+) | ||
````html | ||
<script type="text/javascript" src="//cdn.auth0.com/w2/auth0-widget-5.js"></script> | ||
<script type="text/javascript" src="//cdn.auth0.com/w2/auth0-angular-1.js"></script> | ||
```` | ||
### With your own UI | ||
## TL;DR: Quick start guide | ||
#### User/Password Login | ||
### Add module dependency and configure it | ||
Authenticate user/passwords and get back a promise. | ||
````js | ||
angular.module('myCoolApp', ['auth0']) | ||
.config(function(authProvider, $httpProvider) { | ||
→ [Read the tutorial](docs/jssdk.md) | ||
![basic_guide](https://cloudup.com/cmaeJKX7LEM+) | ||
#### Social Login | ||
// routing configuration and other stuff | ||
// ... | ||
Authenticate using social providers | ||
authProvider.init({ | ||
domain: 'mydomain.auth0.com', | ||
clientId: 'myClientId', | ||
loginUrl: '/login', | ||
callbackUrl: location.href | ||
}); | ||
→ [Read the tutorial](docs/jssdk.md#social-authentication) | ||
![popup_guide](https://cloudup.com/cKpVNpR4s9y+) | ||
Also, we have an [examples](examples) section that contains comprehensive scenarios. | ||
// This automatically adds the token in every request | ||
$httpProvider.interceptors.push('authInterceptor'); | ||
}) | ||
.run(function(auth) { | ||
auth.hookEvents(); | ||
}); | ||
```` | ||
### Using SSO | ||
### Showing the signin popup and getting the information | ||
You can now use `sso`. If you have multiple Angular apps in different domains, you can have your users be automatically logged in to one of your apps if they're logged in to the another one. It's important that you **DON'T** use `popup` mode with this: | ||
````js | ||
// LoginCtrl.js | ||
angular.module('myCoolApp').controller('LoginCtrl', function(auth) { | ||
$scope.signin = function() { | ||
auth.signin({popup: true}).then(function() { | ||
$location.path('/user-info') | ||
}, function(err) { | ||
console.log("Error :(", err); | ||
}); | ||
} | ||
}); | ||
```` | ||
````html | ||
<a href="" ng-click="signin()" /> | ||
```` | ||
### Showing user information | ||
````js | ||
// in the config method | ||
authProvider.init({ | ||
clientID: '1234', | ||
callbackURL: location.href, | ||
domain: 'gonto.auth0.com', | ||
sso: true | ||
// UserInfo.js | ||
angular.module('myCoolApp').controller('UserInfoCtrl', function(auth) { | ||
$scope.profile = auth.profile; | ||
}); | ||
```` | ||
````html | ||
<!-- userInfo.html --> | ||
<span>{{profile.first_name}} {{profile.email}}</span> | ||
```` | ||
authProvider.on('loginSuccess', function($location) { | ||
// This will get called after successful login | ||
// and also after successful SSO automatic login | ||
## Getting Started Guide | ||
### Preface: Authentication Modes | ||
There're 2 modes to handle authentication with all the Providers (Facebook, Linkedin, Github, AD, LDAP, etc.) that Auth0 can handle. **Redirect mode** implies that the page you're seeing is going to get redirected to the page of the provider so that you can login. **Popup mode** implies that your angular app will open a popup window which will go to the provider website so that you can login and then close itself to show the Angular app again. This is really important to your app because if you use Redirect Mode, it means that your angular app will get **reloaded completely** after the user is authenticated with the provider. In Popup mode, the angular app will **remain open**. | ||
### Dependencies | ||
auth0-angular depends on either `auth0.js` or `auth0-widget.js`. | ||
If you want to use Auth0's beautiful Widget UI, you need to include `auth0-widget.js`. This lets you configure Title and Icons, but the UI is taken care for you. For all the customization properties, please check out [tihs link](https://docs.auth0.com/login-widget2#4) | ||
![widget](http://cl.ly/image/2k3i2c3t3s2W/Screen%20Shot%202014-07-24%20at%2017.51.32.png) | ||
Otherwise, if you'll use a custom UI, you need to include `auth0.js`. | ||
**It's important to note that this scripts must be included before auth0-angular**. | ||
If you're using `bower` or `npm`, this 2 scripts are set as a dependency of auth0-angular so that you choose the best for you. Otherwise, you can include them from the CDN: | ||
````html | ||
<!-- Either this --> | ||
<script type="text/javascript" src="//cdn.auth0.com/w2/auth0-widget-5.js"></script> | ||
<!-- or --> | ||
<script type="text/javascript" src="//cdn.auth0.com/w2/auth0-3.js"></script> | ||
```` | ||
### SDK API | ||
This is the API for the SDK. `[]` means optional parameter. | ||
#### auth.signin(options) | ||
This method does the signin for you. If you're using `auth0-widget`, it'll display Auth0's widget, otherwise it'll just do the login with the Identity provider that you ask for. | ||
The most important option is the **`popup` option. If set to `true`**, popup mode will be used and as the Angular page will not reload, **you can use a promise to handle the sigin success and failure**. | ||
````js | ||
auth.signin({popup: true}).then(function( | ||
// All good | ||
$location.path('/'); | ||
), function(error) { | ||
// Error | ||
}) | ||
```` | ||
You can [Check out the example here](https://github.com/auth0/auth0-angular/tree/master/examples/sso) | ||
**If you set `popup` option to `false`** (**this is the default value**), redirect mode will be used. As Angular page is realoded, you **cannot use promises** to handle login success and failure. You'll need to use `events` to handle them: | ||
## Documentation | ||
* [Using Redirect Mode](docs/redirect.md) | ||
* [Consuming a protected REST API](docs/backend.md) | ||
* [Advanced Routing Scenarios](docs/routing.md) | ||
* [Join or Link Accounts](docs/link-accounts.md) | ||
* [FAQ](docs/faq.md) | ||
* [jwt.io](http://jwt.io/): Useful for debugging JWT. | ||
* [Changelog](CHANGELOG.md) | ||
````js | ||
// app.js | ||
module.config(function(authProvider) { | ||
authProvider.on('loginSuccess', function($location) { | ||
$location.path('/'); | ||
}); | ||
authProvider.on('loginFailure', function($location, error) { | ||
$location.path('/error'); | ||
}); | ||
}); | ||
```` | ||
````js | ||
// LoginCtrl.js | ||
auth.signin( | ||
// popup: false. This is the default | ||
); | ||
```` | ||
### Development | ||
* [How to run auth0-angular tests](docs/testing.md) | ||
You can read **a more extensive tutorial on how to use auth0-angular with [popup mode here](docs/widget-popup.md) and with [redirect mode here](docs/widget-redirect.md)**. | ||
The rest of the **options that can be sent can be [checked here](https://docs.auth0.com/login-widget2#4)**. | ||
#### auth.signup(options) | ||
This shows the widget but in `signup` mode. It has the same options and parameters as the login. It's important to note that it'll perform a login after a successful signup. | ||
#### auth.reset(options) | ||
This will show the Forgot your password window. It returns a promise that will tell you if the change was done successfully or not. | ||
#### auth.signout() | ||
This signouts the user. Deletes the token from the client storage. | ||
#### auth.profile | ||
This property contains the profile from the user. **This will be filled after the user has logged in successfully**. If you want to use information from `auth.profile` only after the user is logged in, you can just do a `$watch` on this property to wait until it's set. | ||
#### auth.isAuthenticated | ||
This flag returns wether there's a user authenticated or not. | ||
#### auth.id_token, auth.access_token, auth.state | ||
This property contains the tokens returned after the user is logged in. Mostly for internal usage. | ||
#### auth.refreshToken() | ||
You can configure your token to expire after certain time. If you don't want your user to login again, you can just refresh the current token, which means getting a new token that will be valid for a certain amount of time. | ||
For example, let's imagine you have a token valid for 10 hours. After 9 hours, you can refresh the token to get a new token that's going to be valid for another 10 hours. You just need to call this method in that case and we'll handle everything for you. | ||
#### auth.hookEvents() | ||
auth0-angular takes care of checking that **unauthenticated users canoot access restricted resources**. For that, auth0-angular hooks to internal angular events so that we can redirect the user to the login page if he doesn't have the right permission to access a page. For that, you need to hook auth0-angular to all of this events on application run | ||
First, you need to configure the restricted routes: | ||
````js | ||
// Using ngRoute | ||
module.config(function($routeProvider) { | ||
$routeProvider. | ||
when('/info', { | ||
templateUrl: 'info.html', | ||
controller: 'InfoCtrl', | ||
requiresLogin: true | ||
}). | ||
when('/login', { | ||
tempalteUrl: 'login.html', | ||
controller: 'LoginCtrl' | ||
}); | ||
authProvider.init({ | ||
domain: 'domain', | ||
clientId: 'clientId', | ||
callbackUrl: location.href, | ||
loginUrl: '/login' | ||
}) | ||
}) | ||
// Using ui-router | ||
module.config(function($stateProvider) { | ||
$stateProvider. | ||
state('info', { | ||
url: '/info' | ||
templateUrl: 'info.html', | ||
controller: 'InfoCtrl', | ||
data: { | ||
requiresLogin: true | ||
} | ||
}). | ||
state('login', { | ||
url: '/login' | ||
tempalteUrl: 'login.html', | ||
controller: 'LoginCtrl' | ||
}); | ||
authProvider.init({ | ||
domain: 'domain', | ||
clientId: 'clientId', | ||
callbackUrl: location.href, | ||
loginState: 'login' | ||
}) | ||
}); | ||
```` | ||
Then, you just call `hookEvents` in the `run` method | ||
````js | ||
module.run(function(auth) { | ||
auth.hookEvents(); | ||
}); | ||
```` | ||
To learn more about routing and using `ngRoute` or `ui-router` with your app, please [read this tutorial](docs/routing.md) | ||
#### auth.getToken(targetClientId) | ||
This method does a Delegation Token request. Imagine you have 2 APIs. The user in your angular app is loged in to your angular app that uses API #1. If you want to use API #2, you need to exchange the token you have for the API #1 for a valid one for API #2. This is what this method does. The `targetClientId` parameter is just the identifier of the API #2 in this case. | ||
To learn more about delegated access [please click here](https://docs.auth0.com/auth-api#delegated). | ||
#### auth.hasTokenExpired(token) | ||
This returns if a particular token has expired or not. Mostly for internal usage. | ||
#### authProvider.init(options) | ||
You use this method to configure the auth service. You must set the following options: | ||
* **domain**: The domain you have from your Auth0 account | ||
* **callbackUrl**: The callback URL. Usually this is `location.href` | ||
* **clientId**: The identifier for the application you've created. This can be got from the settings from your app on Auth0. | ||
* **sso**: If you have more than one application and you want Single Sign On on your apps, just set this to true. This will mean that if a user signs in to app 1, when he tries to use app2, he will be already logged in | ||
* **loginUrl**: Set this to the login url **if you're using ngRoute** | ||
* **loginState**: Set this to the login state **if you're using ui-router** | ||
#### authProvider.on(event, handler) | ||
You can configure the handlers for all the different events that can happen in your app. The following are the available events right now: | ||
* **loginSucces**: This will get called after a user has successfully logged in. In the handler, you can inject any service you want besides the `profile` and `token` from the user | ||
* **loginFailure**: This will get called if there's an error authenticating the usr. In the handler, you can inject any service you want besides the `error` which was thrown | ||
* **logout**: This will get called after a user has successfully logged out. | ||
* **forbidden**: This will get called if a request to an API is made and it returns 401 meaning that the user cannot access that resource. That usually happens when the token is expired. In that case, you should redirect the user to the login page in most cases. | ||
It's important to note that in the case of **redirect mode, it's mandatory to handle login events in this way. In the case of popup mode, you can still handle the login events this way, but you can also handle them with a promise on the signin method**. | ||
## Tutorials & Examples | ||
This is the list of all of the available tutorials. | ||
### Using Auth0 Widget (You don't want your custom UI) | ||
#### Redirect mode | ||
**[Click here to read the tutorial](docs/widget-redirect.md)** | ||
**[Click here to see the tutorial](https://github.com/auth0/auth0-angular/tree/master/examples/widget-redirect)** | ||
![Widget redirect](http://cl.ly/image/2o423i362s2P/WidgetRedirect.gif) | ||
#### Popup mode | ||
**[Clcik here to read the tutorial](docs/widget-popup.md)** | ||
**[Click here to see the example](https://github.com/auth0/auth0-angular/tree/master/examples/widget)** | ||
![Widget Popup](https://cloudup.com/cg8u9kVV5Vh+) | ||
### With your own UI | ||
#### User/Password Login | ||
**[Click here to read the tutorial](docs/jssdk.md)** | ||
**[Click here to see the example](https://github.com/auth0/auth0-angular/tree/master/examples/custom-login)** | ||
![basic_guide](https://cloudup.com/cmaeJKX7LEM+) | ||
#### Social Login | ||
**[Click here to read the tutorial](docs/jssdk.md#social-authentication)** | ||
**[Click here to see the example](https://github.com/auth0/auth0-angular/tree/master/examples/custom-login)** | ||
![popup_guide](https://cloudup.com/cKpVNpR4s9y+) | ||
### Consuming a protected REST API | ||
**[Click here to read the tutorial](docs/backend.md)** | ||
**[Click here to see the example](https://github.com/auth0/auth0-angular/tree/master/examples/api-authentication)** | ||
### Join or Link accounts | ||
**[Click here to read the tutorial](docs/link-accounts.md)** | ||
### Integrating to routes (ui-router and ngRoute) | ||
**[Click here to read the tutorial](docs/routing.md)** | ||
**[Click here to see the ui-router example](https://github.com/auth0/auth0-angular/tree/master/examples/ui-router)** | ||
**[Click here to see the ngRoute example](https://github.com/auth0/auth0-angular/tree/master/examples/widget-redirect)** | ||
**[Click here to see the html5mode example](https://github.com/auth0/auth0-angular/tree/master/examples/html5mode)** | ||
### Delegation Token | ||
**[Click here to see the delegation token example](https://github.com/auth0/auth0-angular/tree/master/examples/delegation-token)** | ||
### Signup with custom fields (Besides Email & Password) | ||
**[Click here to see the delegation token example](https://github.com/auth0/auth0-angular/tree/master/examples/custom-signup)** | ||
### SSO | ||
**[Click here to see the SSO example](https://github.com/auth0/auth0-angular/tree/master/examples/sso)** | ||
## Changelog | ||
Check [the CHANGELOG file](CHANGELOG.md) to see the changes from version to version | ||
## Contributing | ||
[Read here how to run auth0-angular tests](docs/testing.md) | ||
## What is Auth0? | ||
@@ -94,0 +367,0 @@ |
@@ -233,2 +233,11 @@ (function () { | ||
function forbidden() { | ||
if (config.loginUrl) { | ||
$location.path(config.loginUrl); | ||
} else if (config.loginState) { | ||
$injector.get('$state').go(config.loginState); | ||
} else { | ||
callHandler('forbidden'); | ||
} | ||
} | ||
@@ -245,2 +254,6 @@ // Redirect mode | ||
if (storedValues && storedValues.idToken) { | ||
if (auth.hasTokenExpired(storedValues.idToken)) { | ||
forbidden(); | ||
return; | ||
} | ||
onSigninOk(storedValues.idToken, storedValues.accessToken, storedValues.state, e); | ||
@@ -262,4 +275,4 @@ return; | ||
$rootScope.$on('auth0.forbidden', function(e, response) { | ||
callHandler('forbidden', {response: response}); | ||
$rootScope.$on('auth0.forbidden', function() { | ||
forbidden(); | ||
}); | ||
@@ -266,0 +279,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
396290
128
6491
380
159