-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathangular-token-authentication.min.js
More file actions
1 lines (1 loc) · 3.14 KB
/
angular-token-authentication.min.js
File metadata and controls
1 lines (1 loc) · 3.14 KB
1
!function(){"use strict";var a=angular.module("tokenAuthentication",["angular-jwt"]);a.value("tokenAuthParams",{sessionDuration:0,idleTime:0,accessTokenKey:"accessToken",jwt:!1,refreshToken:!1}),angular.module("tokenAuthentication").factory("sessionHandler",["$rootScope","$window","tokenAuthParams","jwtHelper",function(a,b,c,d){var e="accessToken",f=b.sessionStorage,g=function(a,b,c){var d=f.tokenAuth_session?JSON.parse(f.tokenAuth_session):{};d[a]={value:b,remember:c||!1},f.tokenAuth_session=JSON.stringify(d)},h={empty:function(){return"{}"===f.tokenAuth_session},setValue:function(b,c,d){b===e?(this.setExpiration(c),this.setIdleTimer(),a.$broadcast("tokenAuth:loggedIn"),c.refresh_token&&this.setValue("refreshToken",c.refresh_token),c=c.access_token):this.empty()&&(this.setExpiration(),this.setIdleTimer()),g(b,c,d)},setValues:function(a){var b=JSON.parse(f.tokenAuth_session);angular.forEach(a,function(a,c){"object"==typeof a&&a.hasOwnProperty("value")||(a={value:a,remember:!1}),b[c]=a}),f.tokenAuth_session=JSON.stringify(b)},getValue:function(a){var b=JSON.parse(f.tokenAuth_session)[a];return b?b.value:null},setAccessToken:function(a,b){this.setValue(e,a,b)},getAccessToken:function(){return this.getValue(e)},getRefreshToken:function(){return this.getValue("refreshToken")},setExpiration:function(a){var b,e,g=c.sessionDuration;a&&(c.jwt?b=d.getTokenExpirationDate(a):a.expires_in&&(b=new Date,b.setSeconds(b.getSeconds()+parseInt(a.expires_in)-60))),g&&(e=new Date,e.setMinutes(e.getMinutes()+g),(!b||e.getTime()<b.getTime())&&(b=e)),b&&(f.tokenAuth_expiration=b.getTime().toString())},checkExpiration:function(){var a=new Date;return f.tokenAuth_expiration&&parseInt(f.tokenAuth_expiration,10)<a.getTime()?!0:!1},setIdleTimer:function(){var a=c.idleTime,b=this;a&&(b.idleInterval&&clearInterval(b.idleInterval),angular.element(document).ready(function(){var c=0;b.idleInterval=setInterval(function(){c+=1,c>=a&&b.clean()},6e4),angular.element(this).on("mousemove",function(){c=0}),angular.element(this).on("keypress",function(){c=0})}))},clean:function(b){var c=JSON.parse(f.tokenAuth_session);angular.forEach(c,function(d,f){(b||!d.remember)&&(delete c[f],f===e&&a.$broadcast("tokenAuth:loggedOut"))}),f.tokenAuth_session=JSON.stringify(c),delete f.tokenAuth_expiration,this.idleInterval&&clearInterval(this.idleInterval)}};return a.$on("tokenAuth:logout",h.clean),f.tokenAuth_session||(f.tokenAuth_session="{}"),h}]),angular.module("tokenAuthResource",["ngResource","tokenAuthentication"]).factory("tokenAuthResource",["$resource","sessionHandler","tokenAuthParams","$http",function(a,b,c,d){return function(){var e=a.apply(this,arguments),f=["get","save","query","remove","delete"];return 3===arguments.length&&angular.forEach(arguments[2],function(a,b){f.push(b)}),angular.forEach(f,function(a){var f=e[a];e[a]=function(){var a=Array.prototype.slice.call(arguments),e=function(){0===a.length?a.push({}):"object"!=typeof a[0]&&a.unshift({}),a[0][c.accessTokenKey]=b.getAccessToken(),f.apply(this,a)};if(b.checkExpiration()){if(c.refreshToken){var g=angular.copy(c.refreshTokenRequest);g.data=g.data(b.getRefreshToken()),d(g).then(function(a){b.setAccessToken(a.data),e()})}}else e()}}),e}}])}();