authlify-js
Advanced tools
Comparing version 0.4.2 to 0.5.0
@@ -22,3 +22,2 @@ 'use strict'; | ||
var HTTPRegexp = /^http:\/\//; | ||
var storageKey = "authlify.user"; | ||
@@ -50,3 +49,3 @@ var Authlify = function () { | ||
key: 'login', | ||
value: function login(email, password) { | ||
value: function login(email, password, remember) { | ||
var _this = this; | ||
@@ -60,2 +59,7 @@ | ||
return new _user2.default(_this.api, response).reload(); | ||
}).then(function (user) { | ||
if (remember) { | ||
_this.persistSession(user); | ||
} | ||
return user; | ||
}); | ||
@@ -82,22 +86,2 @@ } | ||
}, { | ||
key: 'persistSession', | ||
value: function persistSession(user) { | ||
if (user) { | ||
localStorage.setItem(storageKey, JSON.stringify(user)); | ||
} else { | ||
localStorage.removeItem(storageKey); | ||
} | ||
} | ||
}, { | ||
key: 'recoverSession', | ||
value: function recoverSession() { | ||
var json = localStorage.getItem(storageKey); | ||
if (json) { | ||
var data = JSON.parse(json); | ||
return this.user(data.tokenResponse).process(data); | ||
} | ||
return null; | ||
} | ||
}, { | ||
key: 'user', | ||
@@ -108,2 +92,7 @@ value: function user(tokenResponse) { | ||
}, { | ||
key: 'currentUser', | ||
value: function currentUser() { | ||
return _user2.default.recoverSession(); | ||
} | ||
}, { | ||
key: 'verify', | ||
@@ -110,0 +99,0 @@ value: function verify(type, token) { |
@@ -11,5 +11,13 @@ 'use strict'; | ||
var _api = require('./api'); | ||
var _api2 = _interopRequireDefault(_api); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var ExpiryMargin = 60 * 1000; | ||
var storageKey = "authlify.user"; | ||
var currentUser = null; | ||
@@ -51,2 +59,3 @@ var User = function () { | ||
_this2.processTokenResponse(response); | ||
_this2.refreshPersistedSession(_this2); | ||
return _this2.jwt_token; | ||
@@ -60,3 +69,3 @@ }); | ||
value: function logout() { | ||
return this.request('/logout', { method: 'POST' }); | ||
return this.request('/logout', { method: 'POST' }).then(this.clearSession.bind(this)); | ||
} | ||
@@ -77,3 +86,3 @@ }, { | ||
value: function reload() { | ||
return this.request('/user').then(this.process.bind(this)); | ||
return this.request('/user').then(this.process.bind(this)).then(this.refreshPersistedSession.bind(this)); | ||
} | ||
@@ -84,3 +93,3 @@ }, { | ||
for (var key in attributes) { | ||
if (key in this.prototype) { | ||
if (key in User.prototype || key == 'api') { | ||
continue; | ||
@@ -102,2 +111,41 @@ } | ||
} | ||
}, { | ||
key: 'refreshPersistedSession', | ||
value: function refreshPersistedSession(user) { | ||
currentUser = user; | ||
if (localStorage.getItem(storageKey)) { | ||
this.persistSession(user); | ||
} | ||
return user; | ||
} | ||
}, { | ||
key: 'persistSession', | ||
value: function persistSession(user) { | ||
currentUser = user; | ||
if (user) { | ||
localStorage.setItem(storageKey, JSON.stringify(user)); | ||
} else { | ||
localStorage.removeItem(storageKey); | ||
} | ||
} | ||
}, { | ||
key: 'clearSession', | ||
value: function clearSession() { | ||
localStorage.removeItem(storageKey); | ||
} | ||
}], [{ | ||
key: 'recoverSession', | ||
value: function recoverSession() { | ||
if (currentUser) { | ||
return currentUser; | ||
} | ||
var json = localStorage.getItem(storageKey); | ||
if (json) { | ||
var data = JSON.parse(json); | ||
return new User(new _api2.default(data.api.apiURL), data.tokenResponse).process(data); | ||
} | ||
return null; | ||
} | ||
}]); | ||
@@ -104,0 +152,0 @@ |
{ | ||
"name": "authlify-js", | ||
"version": "0.4.2", | ||
"version": "0.5.0", | ||
"description": "Authlify API client for JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -42,2 +42,4 @@ # Authlify JS Client | ||
const user = authlify.currentUser() | ||
user.update({email: newEmail, password: newPassword}).then( | ||
@@ -44,0 +46,0 @@ (user) => console.log("Updated user"), |
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
13432
277
59