feathers-authentication-client
Advanced tools
Comparing version 0.1.10 to 0.2.0
# Change Log | ||
## [v0.1.10](https://github.com/feathersjs/feathers-authentication-client/tree/v0.1.10) (2017-03-03) | ||
[Full Changelog](https://github.com/feathersjs/feathers-authentication-client/compare/v0.1.9...v0.1.10) | ||
**Merged pull requests:** | ||
- Remove hardcoded values for Config and Credentials typings [\#28](https://github.com/feathersjs/feathers-authentication-client/pull/28) ([myknbani](https://github.com/myknbani)) | ||
## [v0.1.9](https://github.com/feathersjs/feathers-authentication-client/tree/v0.1.9) (2017-03-01) | ||
@@ -4,0 +11,0 @@ [Full Changelog](https://github.com/feathersjs/feathers-authentication-client/compare/v0.1.8...v0.1.9) |
@@ -19,3 +19,3 @@ 'use strict'; | ||
var defaults = { | ||
header: 'authorization', | ||
header: 'Authorization', | ||
cookie: 'feathers-jwt', | ||
@@ -26,3 +26,4 @@ storageKey: 'feathers-jwt', | ||
entity: 'user', | ||
service: 'users' | ||
service: 'users', | ||
timeout: 5000 | ||
}; | ||
@@ -29,0 +30,0 @@ |
@@ -21,2 +21,4 @@ 'use strict'; | ||
var _utils = require('./utils'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -36,5 +38,10 @@ | ||
this.options = options; | ||
this.app = app; | ||
this.storage = app.get('storage') || this.getStorage(options.storage); | ||
Object.assign(this, { | ||
options: options, | ||
app: app, | ||
payloadIsValid: _utils.payloadIsValid, | ||
getCookie: _utils.getCookie, | ||
clearCookie: _utils.clearCookie, | ||
storage: app.get('storage') || this.getStorage(options.storage) | ||
}); | ||
@@ -111,2 +118,4 @@ this.setJWT = this.setJWT.bind(this); | ||
value: function connected() { | ||
var _this2 = this; | ||
var app = this.app; | ||
@@ -132,2 +141,7 @@ | ||
var disconnect = app.io ? 'disconnect' : 'end'; | ||
var timeout = setTimeout(function () { | ||
debug('Socket connection timed out'); | ||
reject(new Error('Socket connection timed out')); | ||
}, _this2.options.timeout); | ||
debug('Waiting for socket connection'); | ||
@@ -146,2 +160,3 @@ | ||
socket.removeListener(disconnect, handleDisconnect); | ||
clearTimeout(timeout); | ||
resolve(socket); | ||
@@ -154,3 +169,3 @@ }); | ||
value: function authenticate() { | ||
var _this2 = this; | ||
var _this3 = this; | ||
@@ -171,3 +186,3 @@ var credentials = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
} | ||
return { strategy: _this2.options.jwtStrategy, accessToken: accessToken }; | ||
return { strategy: _this3.options.jwtStrategy, accessToken: accessToken }; | ||
}); | ||
@@ -178,10 +193,13 @@ } | ||
return getCredentials.then(function (credentials) { | ||
return _this2.connected(app).then(function (socket) { | ||
return _this3.connected(app).then(function (socket) { | ||
if (app.rest) { | ||
return app.service(_this2.options.path).create(credentials).then(_this2.setJWT); | ||
return app.service(_this3.options.path).create(credentials).then(_this3.setJWT); | ||
} | ||
var emit = app.io ? 'emit' : 'send'; | ||
return _this2.authenticateSocket(credentials, socket, emit).then(_this2.setJWT); | ||
return _this3.authenticateSocket(credentials, socket, emit).then(_this3.setJWT); | ||
}); | ||
}).then(function (payload) { | ||
app.emit('authenticated', payload); | ||
return payload; | ||
}); | ||
@@ -195,3 +213,10 @@ } | ||
value: function authenticateSocket(credentials, socket, emit) { | ||
var _this4 = this; | ||
return new Promise(function (resolve, reject) { | ||
var timeout = setTimeout(function () { | ||
debug('authenticateSocket timed out'); | ||
reject(new Error('Authentication timed out')); | ||
}, _this4.options.timeout); | ||
debug('Attempting to authenticate socket'); | ||
@@ -203,2 +228,3 @@ socket[emit]('authenticate', credentials, function (error, data) { | ||
clearTimeout(timeout); | ||
socket.authenticated = true; | ||
@@ -214,9 +240,18 @@ debug('Socket authenticated!'); | ||
value: function logoutSocket(socket, emit) { | ||
var _this5 = this; | ||
return new Promise(function (resolve, reject) { | ||
var timeout = setTimeout(function () { | ||
debug('logoutSocket timed out'); | ||
reject(new Error('Logout timed out')); | ||
}, _this5.options.timeout); | ||
socket[emit]('logout', function (error) { | ||
clearTimeout(timeout); | ||
socket.authenticated = false; | ||
if (error) { | ||
reject(error); | ||
return reject(error); | ||
} | ||
socket.authenticated = false; | ||
resolve(); | ||
@@ -229,3 +264,3 @@ }); | ||
value: function logout() { | ||
var _this3 = this; | ||
var _this6 = this; | ||
@@ -244,4 +279,8 @@ var app = this.app; | ||
return _this3.logoutSocket(socket, method); | ||
return _this6.logoutSocket(socket, method); | ||
} | ||
}).then(function (result) { | ||
app.emit('logout', result); | ||
return result; | ||
}); | ||
@@ -264,3 +303,3 @@ } | ||
value: function getJWT() { | ||
var _this4 = this; | ||
var _this7 = this; | ||
@@ -275,6 +314,6 @@ var app = this.app; | ||
return Promise.resolve(_this4.storage.getItem(_this4.options.storageKey)).then(function (jwt) { | ||
var token = jwt || _this4.getCookie(_this4.options.cookie); | ||
return Promise.resolve(_this7.storage.getItem(_this7.options.storageKey)).then(function (jwt) { | ||
var token = jwt || _this7.getCookie(_this7.options.cookie); | ||
if (token && token !== 'null' && !_this4.payloadIsValid((0, _jwtDecode2.default)(token))) { | ||
if (token && token !== 'null' && !_this7.payloadIsValid((0, _jwtDecode2.default)(token))) { | ||
token = undefined; | ||
@@ -310,33 +349,2 @@ } | ||
// Pass a decoded payload and it will return a boolean based on if it hasn't expired. | ||
}, { | ||
key: 'payloadIsValid', | ||
value: function payloadIsValid(payload) { | ||
return payload && payload.exp * 1000 > new Date().getTime(); | ||
} | ||
}, { | ||
key: 'getCookie', | ||
value: function getCookie(name) { | ||
if (typeof document !== 'undefined') { | ||
var value = '; ' + document.cookie; | ||
var parts = value.split('; ' + name + '='); | ||
if (parts.length === 2) { | ||
return parts.pop().split(';').shift(); | ||
} | ||
} | ||
return null; | ||
} | ||
}, { | ||
key: 'clearCookie', | ||
value: function clearCookie(name) { | ||
if (typeof document !== 'undefined') { | ||
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;'; | ||
} | ||
return null; | ||
} | ||
// Returns a storage implementation | ||
@@ -351,15 +359,3 @@ | ||
return { | ||
store: {}, | ||
getItem: function getItem(key) { | ||
return this.store[key]; | ||
}, | ||
setItem: function setItem(key, value) { | ||
return this.store[key] = value; | ||
}, | ||
removeItem: function removeItem(key) { | ||
delete this.store[key]; | ||
return this; | ||
} | ||
}; | ||
return new _utils.Storage(); | ||
} | ||
@@ -366,0 +362,0 @@ }]); |
{ | ||
"name": "feathers-authentication-client", | ||
"description": "The authentication plugin for feathers-client", | ||
"version": "0.1.10", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/feathersjs/feathers-authentication-client", | ||
@@ -6,0 +6,0 @@ "main": "lib/index", |
@@ -69,3 +69,3 @@ # feathers-authentication-client | ||
{ | ||
header: 'authorization', // the default authorization header | ||
header: 'Authorization', // the default authorization header | ||
path: '/authentication', // the server side authentication service path | ||
@@ -72,0 +72,0 @@ jwtStrategy: 'jwt', // the name of the JWT authentication strategy |
162456
19
693