Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var jwt = (function (log, storage) {
- var TOKEN = 'auth_token';
- var EXPIRATION = 'auth_expiration';
- var get = function () {
- log.debug({ class: 'jwt', method: 'get' });
- var token = storage.get(TOKEN);
- var expiration = storage.get(EXPIRATION);
- return {
- token: token,
- expiration: expiration
- };
- };
- var set = function (auth) {
- log.debug({ class: 'jwt', method: 'set', token: auth });
- storage.set(TOKEN, auth.token);
- storage.set(EXPIRATION, auth.expiration);
- };
- var clear = function () {
- log.debug({ class: 'jwt', method: 'clear' });
- storage.remove(TOKEN);
- storage.remove(EXPIRATION);
- };
- var intercept = function () {
- $.ajaxSetup({
- beforeSend: function (xhr) {
- var auth = get();
- xhr.setRequestHeader('Authorization', auth.token);
- }
- });
- };
- var isExpired = function () {
- var auth = get();
- var expiration = new Date(auth.expiration);
- var current = new Date();
- return expiration >= current;
- };
- var isAuthenticated = function () {
- var auth = get();
- return (auth !== undefined && auth !== null);
- };
- return {
- get: get,
- set: set,
- clear: clear,
- intercept: intercept,
- isExpired: isExpired,
- isAuthenticated: isAuthenticated
- };
- })(log, storage);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement