@feathersjs/authentication-client 5.0.0-pre.22 → 5.0.0-pre.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/lib/core.js +9 -9
- package/lib/core.js.map +1 -1
- package/lib/hooks/authentication.js +4 -2
- package/lib/hooks/authentication.js.map +1 -1
- package/lib/hooks/index.js.map +1 -1
- package/lib/hooks/populate-header.js.map +1 -1
- package/lib/index.js +1 -4
- package/lib/index.js.map +1 -1
- package/lib/storage.js +1 -1
- package/lib/storage.js.map +1 -1
- package/package.json +15 -15
- package/src/core.ts +112 -113
- package/src/hooks/authentication.ts +19 -11
- package/src/hooks/index.ts +2 -2
- package/src/hooks/populate-header.ts +18 -11
- package/src/index.ts +34 -33
- package/src/storage.ts +23 -23
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [5.0.0-pre.23](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.22...v5.0.0-pre.23) (2022-06-06)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @feathersjs/authentication-client
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
6
14
|
# [5.0.0-pre.22](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.21...v5.0.0-pre.22) (2022-05-24)
|
|
7
15
|
|
|
8
16
|
**Note:** Version bump only for package @feathersjs/authentication-client
|
package/lib/core.js
CHANGED
|
@@ -40,11 +40,11 @@ class AuthenticationClient {
|
|
|
40
40
|
const connected = this.app.io ? 'connect' : 'open';
|
|
41
41
|
const disconnected = this.app.io ? 'disconnect' : 'disconnection';
|
|
42
42
|
socket.on(disconnected, () => {
|
|
43
|
-
const authPromise = new Promise(resolve => socket.once(connected, (data) => resolve(data)))
|
|
43
|
+
const authPromise = new Promise((resolve) => socket.once(connected, (data) => resolve(data)))
|
|
44
44
|
// Only reconnect when `reAuthenticate()` or `authenticate()`
|
|
45
45
|
// has been called explicitly first
|
|
46
46
|
// Force reauthentication with the server
|
|
47
|
-
.then(() => this.authenticated ? this.reAuthenticate(true) : null);
|
|
47
|
+
.then(() => (this.authenticated ? this.reAuthenticate(true) : null));
|
|
48
48
|
this.app.set('authentication', authPromise);
|
|
49
49
|
});
|
|
50
50
|
}
|
|
@@ -65,8 +65,7 @@ class AuthenticationClient {
|
|
|
65
65
|
return this.storage.setItem(this.options.storageKey, accessToken);
|
|
66
66
|
}
|
|
67
67
|
getAccessToken() {
|
|
68
|
-
return this.storage.getItem(this.options.storageKey)
|
|
69
|
-
.then((accessToken) => {
|
|
68
|
+
return this.storage.getItem(this.options.storageKey).then((accessToken) => {
|
|
70
69
|
if (!accessToken && typeof window !== 'undefined' && window.location) {
|
|
71
70
|
return this.getFromLocation(window.location);
|
|
72
71
|
}
|
|
@@ -93,7 +92,7 @@ class AuthenticationClient {
|
|
|
93
92
|
// tries to re-authenticate with the stored JWT and strategy
|
|
94
93
|
const authPromise = this.app.get('authentication');
|
|
95
94
|
if (!authPromise || force === true) {
|
|
96
|
-
return this.getAccessToken().then(accessToken => {
|
|
95
|
+
return this.getAccessToken().then((accessToken) => {
|
|
97
96
|
if (!accessToken) {
|
|
98
97
|
throw new errors_1.NotAuthenticated('No accessToken found in storage');
|
|
99
98
|
}
|
|
@@ -109,21 +108,22 @@ class AuthenticationClient {
|
|
|
109
108
|
if (!authentication) {
|
|
110
109
|
return this.reAuthenticate();
|
|
111
110
|
}
|
|
112
|
-
const promise = this.service
|
|
111
|
+
const promise = this.service
|
|
112
|
+
.create(authentication, params)
|
|
113
113
|
.then((authResult) => {
|
|
114
114
|
const { accessToken } = authResult;
|
|
115
115
|
this.authenticated = true;
|
|
116
116
|
this.app.emit('login', authResult);
|
|
117
117
|
this.app.emit('authenticated', authResult);
|
|
118
118
|
return this.setAccessToken(accessToken).then(() => authResult);
|
|
119
|
-
})
|
|
119
|
+
})
|
|
120
|
+
.catch((error) => this.handleError(error, 'authenticate'));
|
|
120
121
|
this.app.set('authentication', promise);
|
|
121
122
|
return promise;
|
|
122
123
|
}
|
|
123
124
|
logout() {
|
|
124
125
|
return Promise.resolve(this.app.get('authentication'))
|
|
125
|
-
.then(() => this.service.remove(null)
|
|
126
|
-
.then((authResult) => this.removeAccessToken()
|
|
126
|
+
.then(() => this.service.remove(null).then((authResult) => this.removeAccessToken()
|
|
127
127
|
.then(() => this.reset())
|
|
128
128
|
.then(() => {
|
|
129
129
|
this.app.emit('logout', authResult);
|
package/lib/core.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.js","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":";;;AAAA,+
|
|
1
|
+
{"version":3,"file":"core.js","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":";;;AAAA,+CAAoE;AAGpE,uCAAmD;AAEnD,MAAM,UAAW,SAAQ,sBAAa;IACpC,YAAY,OAAe,EAAE,IAAU;QACrC,KAAK,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,EAAE,aAAa,EAAE,IAAI,CAAC,CAAA;IACxD,CAAC;CACF;AAED,MAAM,QAAQ,GAAG,CAAC,QAAkB,EAAE,GAAW,EAAoB,EAAE;IACrE,MAAM,KAAK,GAAG,IAAI,MAAM,CAAC,UAAU,GAAG,UAAU,CAAC,CAAA;IACjD,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;IAE/D,IAAI,KAAK,KAAK,IAAI,EAAE;QAClB,MAAM,CAAC,EAAE,KAAK,CAAC,GAAG,KAAK,CAAA;QAEvB,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;KACtB;IAED,OAAO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,CAAC,CAAA;QAE3E,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,gBAAgB,EAAE,OAAO,CAAC,CAAA;QAEvC,OAAO,OAAO,CAAA;IAChB,CAAC;IAED,MAAM;QACJ,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;aACnD,IAAI,CAAC,GAAG,EAAE,CACT,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,UAAgC,EAAE,EAAE,CAClE,IAAI,CAAC,iBAAiB,EAAE;aACrB,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;aACxB,IAAI,CAAC,GAAG,EAAE;YACT,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAA;YAEnC,OAAO,UAAU,CAAA;QACnB,CAAC,CAAC,CACL,CACF;aACA,KAAK,CAAC,CAAC,KAAoB,EAAE,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAA;IACvE,CAAC;CACF;AA7JD,oDA6JC"}
|
|
@@ -8,11 +8,13 @@ const authentication = () => {
|
|
|
8
8
|
if ((0, commons_1.stripSlashes)(service.options.path) === path && method === 'create') {
|
|
9
9
|
return next();
|
|
10
10
|
}
|
|
11
|
-
return Promise.resolve(app.get('authentication'))
|
|
11
|
+
return Promise.resolve(app.get('authentication'))
|
|
12
|
+
.then((authResult) => {
|
|
12
13
|
if (authResult) {
|
|
13
14
|
context.params = Object.assign({}, authResult, params);
|
|
14
15
|
}
|
|
15
|
-
})
|
|
16
|
+
})
|
|
17
|
+
.then(next);
|
|
16
18
|
};
|
|
17
19
|
};
|
|
18
20
|
exports.authentication = authentication;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authentication.js","sourceRoot":"","sources":["../../src/hooks/authentication.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"authentication.js","sourceRoot":"","sources":["../../src/hooks/authentication.ts"],"names":[],"mappings":";;;AACA,iDAAkD;AAE3C,MAAM,cAAc,GAAG,GAAG,EAAE;IACjC,OAAO,CAAC,OAAoB,EAAE,IAAkB,EAAE,EAAE;QAClD,MAAM,EACJ,GAAG,EACH,MAAM,EACN,IAAI,EACJ,MAAM,EACN,GAAG,EAAE,EAAE,cAAc,EAAE,OAAO,EAAE,EACjC,GAAG,OAAO,CAAA;QAEX,IAAI,IAAA,sBAAY,EAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,IAAI,IAAI,MAAM,KAAK,QAAQ,EAAE;YACtE,OAAO,IAAI,EAAE,CAAA;SACd;QAED,OAAO,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;aAC9C,IAAI,CAAC,CAAC,UAAU,EAAE,EAAE;YACnB,IAAI,UAAU,EAAE;gBACd,OAAO,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,UAAU,EAAE,MAAM,CAAC,CAAA;aACvD;QACH,CAAC,CAAC;aACD,IAAI,CAAC,IAAI,CAAC,CAAA;IACf,CAAC,CAAA;AACH,CAAC,CAAA;AAtBY,QAAA,cAAc,kBAsB1B"}
|
package/lib/hooks/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":";;;AAAA,mDAAiD;AAAxC,gHAAA,cAAc,OAAA;AACvB,qDAAkD;AAAzC,iHAAA,cAAc,OAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"populate-header.js","sourceRoot":"","sources":["../../src/hooks/populate-header.ts"],"names":[],"mappings":";;;AAEO,MAAM,cAAc,GAAG,GAAG,EAAE;IACjC,OAAO,CAAC,OAAoB,EAAE,IAAkB,EAAE,EAAE;QAClD,MAAM,
|
|
1
|
+
{"version":3,"file":"populate-header.js","sourceRoot":"","sources":["../../src/hooks/populate-header.ts"],"names":[],"mappings":";;;AAEO,MAAM,cAAc,GAAG,GAAG,EAAE;IACjC,OAAO,CAAC,OAAoB,EAAE,IAAkB,EAAE,EAAE;QAClD,MAAM,EACJ,GAAG,EACH,MAAM,EAAE,EAAE,WAAW,EAAE,EACxB,GAAG,OAAO,CAAA;QACX,MAAM,cAAc,GAAG,GAAG,CAAC,cAAc,CAAA;QAEzC,+BAA+B;QAC/B,IAAI,GAAG,CAAC,IAAI,IAAI,WAAW,EAAE;YAC3B,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,cAAc,CAAC,OAAO,CAAA;YACjD,MAAM,UAAU,GAAG,GAAG,MAAM,IAAI,WAAW,EAAE,CAAA;YAE7C,OAAO,CAAC,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CACpC,EAAE,EACF;gBACE,CAAC,MAAM,CAAC,EAAE,UAAU;aACrB,EACD,OAAO,CAAC,MAAM,CAAC,OAAO,CACvB,CAAA;SACF;QAED,OAAO,IAAI,EAAE,CAAA;IACf,CAAC,CAAA;AACH,CAAC,CAAA;AAxBY,QAAA,cAAc,kBAwB1B"}
|
package/lib/index.js
CHANGED
|
@@ -59,10 +59,7 @@ const init = (_options = {}) => {
|
|
|
59
59
|
app.authenticate = authentication.authenticate.bind(authentication);
|
|
60
60
|
app.reAuthenticate = authentication.reAuthenticate.bind(authentication);
|
|
61
61
|
app.logout = authentication.logout.bind(authentication);
|
|
62
|
-
app.hooks([
|
|
63
|
-
hooks.authentication(),
|
|
64
|
-
hooks.populateHeader()
|
|
65
|
-
]);
|
|
62
|
+
app.hooks([hooks.authentication(), hooks.populateHeader()]);
|
|
66
63
|
};
|
|
67
64
|
};
|
|
68
65
|
exports.default = init;
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iCAA0E;AAyBjE,qGAzBA,2BAAoB,OAyBA;AAxB7B,+CAAgC;AAwBoD,sBAAK;AAtBzF,uCAAkE;AAsBG,8FAtBnD,uBAAa,OAsBmD;AAR3E,MAAM,iBAAiB,GAAG,GAAG,EAAE;IACpC,IAAI;QACF,OAAO,IAAI,wBAAc,CAAC,MAAM,CAAC,YAAY,CAAC,CAAA;KAC/C;IAAC,OAAO,KAAU,EAAE,GAAE;IAEvB,OAAO,IAAI,uBAAa,EAAE,CAAA;AAC5B,CAAC,CAAA;AANY,QAAA,iBAAiB,qBAM7B;AASY,QAAA,cAAc,GAAY,IAAA,yBAAiB,GAAE,CAAA;AAE7C,QAAA,QAAQ,GAAgC;IACnD,MAAM,EAAE,eAAe;IACvB,MAAM,EAAE,QAAQ;IAChB,UAAU,EAAE,cAAc;IAC1B,WAAW,EAAE,cAAc;IAC3B,gBAAgB,EAAE,OAAO;IACzB,WAAW,EAAE,KAAK;IAClB,IAAI,EAAE,iBAAiB;IACvB,cAAc,EAAE,2BAAoB;IACpC,OAAO,EAAE,sBAAc;CACxB,CAAA;AAED,MAAM,IAAI,GAAG,CAAC,WAAiD,EAAE,EAAE,EAAE;IACnE,MAAM,OAAO,GAAgC,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,gBAAQ,EAAE,QAAQ,CAAC,CAAA;IAClF,MAAM,EAAE,cAAc,EAAE,GAAG,OAAO,CAAA;IAElC,OAAO,CAAC,GAAgB,EAAE,EAAE;QAC1B,MAAM,cAAc,GAAG,IAAI,cAAc,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;QAEvD,GAAG,CAAC,cAAc,GAAG,cAAc,CAAA;QACnC,GAAG,CAAC,YAAY,GAAG,cAAc,CAAC,YAAY,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;QACnE,GAAG,CAAC,cAAc,GAAG,cAAc,CAAC,cAAc,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;QACvE,GAAG,CAAC,MAAM,GAAG,cAAc,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;QAEvD,GAAG,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,cAAc,EAAE,EAAE,KAAK,CAAC,cAAc,EAAE,CAAC,CAAC,CAAA;IAC7D,CAAC,CAAA;AACH,CAAC,CAAA;AAED,kBAAe,IAAI,CAAA;AAEnB,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;IACjC,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,OAAO,CAAC,CAAA;CACrD"}
|
package/lib/storage.js
CHANGED
package/lib/storage.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":";;;AAMA,MAAa,aAAa;IAGxB;QACE,IAAI,CAAC,KAAK,GAAG,EAAE,
|
|
1
|
+
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":";;;AAMA,MAAa,aAAa;IAGxB;QACE,IAAI,CAAC,KAAK,GAAG,EAAE,CAAA;IACjB,CAAC;IAED,OAAO,CAAC,GAAW;QACjB,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAA;IACzC,CAAC;IAED,OAAO,CAAC,GAAW,EAAE,KAAU;QAC7B,OAAO,OAAO,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC,CAAA;IACnD,CAAC;IAED,UAAU,CAAC,GAAW;QACpB,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QAE7B,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QAEtB,OAAO,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC/B,CAAC;CACF;AAtBD,sCAsBC;AAED,MAAa,cAAc;IAGzB,YAAY,OAAY;QACtB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;IACxB,CAAC;IAED,OAAO,CAAC,GAAW;QACjB,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAA;IACnD,CAAC;IAED,OAAO,CAAC,GAAW,EAAE,KAAU;QAC7B,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAA;IAC1D,CAAC;IAED,UAAU,CAAC,GAAW;QACpB,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAA;IACtD,CAAC;CACF;AAlBD,wCAkBC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@feathersjs/authentication-client",
|
|
3
3
|
"description": "The authentication plugin for feathers-client",
|
|
4
|
-
"version": "5.0.0-pre.
|
|
4
|
+
"version": "5.0.0-pre.23",
|
|
5
5
|
"homepage": "https://feathersjs.com",
|
|
6
6
|
"main": "lib/",
|
|
7
7
|
"types": "lib/",
|
|
@@ -52,25 +52,25 @@
|
|
|
52
52
|
"access": "public"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@feathersjs/authentication": "^5.0.0-pre.
|
|
56
|
-
"@feathersjs/commons": "^5.0.0-pre.
|
|
57
|
-
"@feathersjs/errors": "^5.0.0-pre.
|
|
58
|
-
"@feathersjs/feathers": "^5.0.0-pre.
|
|
55
|
+
"@feathersjs/authentication": "^5.0.0-pre.23",
|
|
56
|
+
"@feathersjs/commons": "^5.0.0-pre.23",
|
|
57
|
+
"@feathersjs/errors": "^5.0.0-pre.23",
|
|
58
|
+
"@feathersjs/feathers": "^5.0.0-pre.23"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
|
-
"@feathersjs/authentication-local": "^5.0.0-pre.
|
|
62
|
-
"@feathersjs/express": "^5.0.0-pre.
|
|
63
|
-
"@feathersjs/memory": "^5.0.0-pre.
|
|
64
|
-
"@feathersjs/rest-client": "^5.0.0-pre.
|
|
65
|
-
"@feathersjs/socketio": "^5.0.0-pre.
|
|
66
|
-
"@feathersjs/socketio-client": "^5.0.0-pre.
|
|
61
|
+
"@feathersjs/authentication-local": "^5.0.0-pre.23",
|
|
62
|
+
"@feathersjs/express": "^5.0.0-pre.23",
|
|
63
|
+
"@feathersjs/memory": "^5.0.0-pre.23",
|
|
64
|
+
"@feathersjs/rest-client": "^5.0.0-pre.23",
|
|
65
|
+
"@feathersjs/socketio": "^5.0.0-pre.23",
|
|
66
|
+
"@feathersjs/socketio-client": "^5.0.0-pre.23",
|
|
67
67
|
"@types/mocha": "^9.1.1",
|
|
68
|
-
"@types/node": "^17.0.
|
|
68
|
+
"@types/node": "^17.0.40",
|
|
69
69
|
"axios": "^0.27.2",
|
|
70
70
|
"mocha": "^10.0.0",
|
|
71
71
|
"shx": "^0.3.4",
|
|
72
|
-
"ts-node": "^10.
|
|
73
|
-
"typescript": "^4.
|
|
72
|
+
"ts-node": "^10.8.1",
|
|
73
|
+
"typescript": "^4.7.3"
|
|
74
74
|
},
|
|
75
|
-
"gitHead": "
|
|
75
|
+
"gitHead": "a60910bd730b88053ca6648337095f1ca1e3b39f"
|
|
76
76
|
}
|
package/src/core.ts
CHANGED
|
@@ -1,200 +1,199 @@
|
|
|
1
|
-
import { NotAuthenticated, FeathersError } from '@feathersjs/errors'
|
|
2
|
-
import { Application, Params } from '@feathersjs/feathers'
|
|
3
|
-
import { AuthenticationRequest, AuthenticationResult } from '@feathersjs/authentication'
|
|
4
|
-
import { Storage, StorageWrapper } from './storage'
|
|
1
|
+
import { NotAuthenticated, FeathersError } from '@feathersjs/errors'
|
|
2
|
+
import { Application, Params } from '@feathersjs/feathers'
|
|
3
|
+
import { AuthenticationRequest, AuthenticationResult } from '@feathersjs/authentication'
|
|
4
|
+
import { Storage, StorageWrapper } from './storage'
|
|
5
5
|
|
|
6
6
|
class OauthError extends FeathersError {
|
|
7
|
-
constructor
|
|
8
|
-
super(message, 'OauthError', 401, 'oauth-error', data)
|
|
7
|
+
constructor(message: string, data?: any) {
|
|
8
|
+
super(message, 'OauthError', 401, 'oauth-error', data)
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
const getMatch = (location: Location, key: string): [
|
|
13
|
-
const regex = new RegExp(`(?:\&?)${key}=([^&]*)`)
|
|
14
|
-
const match = location.hash ? location.hash.match(regex) : null
|
|
12
|
+
const getMatch = (location: Location, key: string): [string, RegExp] => {
|
|
13
|
+
const regex = new RegExp(`(?:\&?)${key}=([^&]*)`)
|
|
14
|
+
const match = location.hash ? location.hash.match(regex) : null
|
|
15
15
|
|
|
16
16
|
if (match !== null) {
|
|
17
|
-
const [
|
|
17
|
+
const [, value] = match
|
|
18
18
|
|
|
19
|
-
return [
|
|
19
|
+
return [value, regex]
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
return [
|
|
23
|
-
}
|
|
22
|
+
return [null, regex]
|
|
23
|
+
}
|
|
24
24
|
|
|
25
|
-
export type ClientConstructor = new (
|
|
26
|
-
|
|
25
|
+
export type ClientConstructor = new (
|
|
26
|
+
app: Application,
|
|
27
|
+
options: AuthenticationClientOptions
|
|
28
|
+
) => AuthenticationClient
|
|
27
29
|
|
|
28
30
|
export interface AuthenticationClientOptions {
|
|
29
|
-
storage: Storage
|
|
30
|
-
header: string
|
|
31
|
-
scheme: string
|
|
32
|
-
storageKey: string
|
|
33
|
-
locationKey: string
|
|
34
|
-
locationErrorKey: string
|
|
35
|
-
jwtStrategy: string
|
|
36
|
-
path: string
|
|
37
|
-
Authentication: ClientConstructor
|
|
31
|
+
storage: Storage
|
|
32
|
+
header: string
|
|
33
|
+
scheme: string
|
|
34
|
+
storageKey: string
|
|
35
|
+
locationKey: string
|
|
36
|
+
locationErrorKey: string
|
|
37
|
+
jwtStrategy: string
|
|
38
|
+
path: string
|
|
39
|
+
Authentication: ClientConstructor
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
export class AuthenticationClient {
|
|
41
|
-
app: Application
|
|
42
|
-
authenticated: boolean
|
|
43
|
-
options: AuthenticationClientOptions
|
|
43
|
+
app: Application
|
|
44
|
+
authenticated: boolean
|
|
45
|
+
options: AuthenticationClientOptions
|
|
44
46
|
|
|
45
|
-
constructor
|
|
46
|
-
const socket = app.io
|
|
47
|
-
const storage = new StorageWrapper(app.get('storage') || options.storage)
|
|
47
|
+
constructor(app: Application, options: AuthenticationClientOptions) {
|
|
48
|
+
const socket = app.io
|
|
49
|
+
const storage = new StorageWrapper(app.get('storage') || options.storage)
|
|
48
50
|
|
|
49
|
-
this.app = app
|
|
50
|
-
this.options = options
|
|
51
|
-
this.authenticated = false
|
|
52
|
-
this.app.set('storage', storage)
|
|
51
|
+
this.app = app
|
|
52
|
+
this.options = options
|
|
53
|
+
this.authenticated = false
|
|
54
|
+
this.app.set('storage', storage)
|
|
53
55
|
|
|
54
56
|
if (socket) {
|
|
55
|
-
this.handleSocket(socket)
|
|
57
|
+
this.handleSocket(socket)
|
|
56
58
|
}
|
|
57
59
|
}
|
|
58
60
|
|
|
59
|
-
get service
|
|
60
|
-
return this.app.service(this.options.path)
|
|
61
|
+
get service() {
|
|
62
|
+
return this.app.service(this.options.path)
|
|
61
63
|
}
|
|
62
64
|
|
|
63
|
-
get storage
|
|
64
|
-
return this.app.get('storage') as Storage
|
|
65
|
+
get storage() {
|
|
66
|
+
return this.app.get('storage') as Storage
|
|
65
67
|
}
|
|
66
68
|
|
|
67
|
-
handleSocket
|
|
69
|
+
handleSocket(socket: any) {
|
|
68
70
|
// Connection events happen on every reconnect
|
|
69
|
-
const connected = this.app.io ? 'connect' : 'open'
|
|
70
|
-
const disconnected = this.app.io ? 'disconnect' : 'disconnection'
|
|
71
|
+
const connected = this.app.io ? 'connect' : 'open'
|
|
72
|
+
const disconnected = this.app.io ? 'disconnect' : 'disconnection'
|
|
71
73
|
|
|
72
74
|
socket.on(disconnected, () => {
|
|
73
|
-
const authPromise = new Promise(resolve =>
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
this.app.set('authentication', authPromise);
|
|
82
|
-
});
|
|
75
|
+
const authPromise = new Promise((resolve) => socket.once(connected, (data: any) => resolve(data)))
|
|
76
|
+
// Only reconnect when `reAuthenticate()` or `authenticate()`
|
|
77
|
+
// has been called explicitly first
|
|
78
|
+
// Force reauthentication with the server
|
|
79
|
+
.then(() => (this.authenticated ? this.reAuthenticate(true) : null))
|
|
80
|
+
|
|
81
|
+
this.app.set('authentication', authPromise)
|
|
82
|
+
})
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
getFromLocation
|
|
86
|
-
const [
|
|
85
|
+
getFromLocation(location: Location) {
|
|
86
|
+
const [accessToken, tokenRegex] = getMatch(location, this.options.locationKey)
|
|
87
87
|
|
|
88
88
|
if (accessToken !== null) {
|
|
89
|
-
location.hash = location.hash.replace(tokenRegex, '')
|
|
89
|
+
location.hash = location.hash.replace(tokenRegex, '')
|
|
90
90
|
|
|
91
|
-
return Promise.resolve(accessToken)
|
|
91
|
+
return Promise.resolve(accessToken)
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
const [
|
|
94
|
+
const [message, errorRegex] = getMatch(location, this.options.locationErrorKey)
|
|
95
95
|
|
|
96
96
|
if (message !== null) {
|
|
97
|
-
location.hash = location.hash.replace(errorRegex, '')
|
|
97
|
+
location.hash = location.hash.replace(errorRegex, '')
|
|
98
98
|
|
|
99
|
-
return Promise.reject(new OauthError(decodeURIComponent(message)))
|
|
99
|
+
return Promise.reject(new OauthError(decodeURIComponent(message)))
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
return Promise.resolve(null)
|
|
102
|
+
return Promise.resolve(null)
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
setAccessToken
|
|
106
|
-
return this.storage.setItem(this.options.storageKey, accessToken)
|
|
105
|
+
setAccessToken(accessToken: string) {
|
|
106
|
+
return this.storage.setItem(this.options.storageKey, accessToken)
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
-
getAccessToken
|
|
110
|
-
return this.storage.getItem(this.options.storageKey)
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
109
|
+
getAccessToken(): Promise<string | null> {
|
|
110
|
+
return this.storage.getItem(this.options.storageKey).then((accessToken: string) => {
|
|
111
|
+
if (!accessToken && typeof window !== 'undefined' && window.location) {
|
|
112
|
+
return this.getFromLocation(window.location)
|
|
113
|
+
}
|
|
115
114
|
|
|
116
|
-
|
|
117
|
-
|
|
115
|
+
return accessToken || null
|
|
116
|
+
})
|
|
118
117
|
}
|
|
119
118
|
|
|
120
|
-
removeAccessToken
|
|
121
|
-
return this.storage.removeItem(this.options.storageKey)
|
|
119
|
+
removeAccessToken() {
|
|
120
|
+
return this.storage.removeItem(this.options.storageKey)
|
|
122
121
|
}
|
|
123
122
|
|
|
124
|
-
reset
|
|
125
|
-
this.app.set('authentication', null)
|
|
126
|
-
this.authenticated = false
|
|
123
|
+
reset() {
|
|
124
|
+
this.app.set('authentication', null)
|
|
125
|
+
this.authenticated = false
|
|
127
126
|
|
|
128
|
-
return Promise.resolve(null)
|
|
127
|
+
return Promise.resolve(null)
|
|
129
128
|
}
|
|
130
129
|
|
|
131
|
-
handleError
|
|
130
|
+
handleError(error: FeathersError, type: 'authenticate' | 'logout') {
|
|
132
131
|
if (error.code === 401 || error.code === 403) {
|
|
133
|
-
const promise = this.removeAccessToken().then(() => this.reset())
|
|
132
|
+
const promise = this.removeAccessToken().then(() => this.reset())
|
|
134
133
|
|
|
135
|
-
return type === 'logout' ? promise : promise.then(() => Promise.reject(error))
|
|
134
|
+
return type === 'logout' ? promise : promise.then(() => Promise.reject(error))
|
|
136
135
|
}
|
|
137
136
|
|
|
138
|
-
return Promise.reject(error)
|
|
137
|
+
return Promise.reject(error)
|
|
139
138
|
}
|
|
140
139
|
|
|
141
|
-
reAuthenticate
|
|
140
|
+
reAuthenticate(force = false, strategy?: string): Promise<AuthenticationResult> {
|
|
142
141
|
// Either returns the authentication state or
|
|
143
142
|
// tries to re-authenticate with the stored JWT and strategy
|
|
144
|
-
const authPromise = this.app.get('authentication')
|
|
143
|
+
const authPromise = this.app.get('authentication')
|
|
145
144
|
|
|
146
145
|
if (!authPromise || force === true) {
|
|
147
|
-
return this.getAccessToken().then(accessToken => {
|
|
146
|
+
return this.getAccessToken().then((accessToken) => {
|
|
148
147
|
if (!accessToken) {
|
|
149
|
-
throw new NotAuthenticated('No accessToken found in storage')
|
|
148
|
+
throw new NotAuthenticated('No accessToken found in storage')
|
|
150
149
|
}
|
|
151
150
|
|
|
152
151
|
return this.authenticate({
|
|
153
152
|
strategy: strategy || this.options.jwtStrategy,
|
|
154
153
|
accessToken
|
|
155
|
-
})
|
|
156
|
-
})
|
|
154
|
+
})
|
|
155
|
+
})
|
|
157
156
|
}
|
|
158
157
|
|
|
159
|
-
return authPromise
|
|
158
|
+
return authPromise
|
|
160
159
|
}
|
|
161
160
|
|
|
162
|
-
authenticate
|
|
161
|
+
authenticate(authentication?: AuthenticationRequest, params?: Params): Promise<AuthenticationResult> {
|
|
163
162
|
if (!authentication) {
|
|
164
|
-
return this.reAuthenticate()
|
|
163
|
+
return this.reAuthenticate()
|
|
165
164
|
}
|
|
166
165
|
|
|
167
|
-
const promise = this.service
|
|
166
|
+
const promise = this.service
|
|
167
|
+
.create(authentication, params)
|
|
168
168
|
.then((authResult: AuthenticationResult) => {
|
|
169
|
-
const { accessToken } = authResult
|
|
169
|
+
const { accessToken } = authResult
|
|
170
170
|
|
|
171
|
-
this.authenticated = true
|
|
172
|
-
this.app.emit('login', authResult)
|
|
173
|
-
this.app.emit('authenticated', authResult)
|
|
171
|
+
this.authenticated = true
|
|
172
|
+
this.app.emit('login', authResult)
|
|
173
|
+
this.app.emit('authenticated', authResult)
|
|
174
174
|
|
|
175
|
-
return this.setAccessToken(accessToken).then(() => authResult)
|
|
176
|
-
})
|
|
177
|
-
|
|
178
|
-
);
|
|
175
|
+
return this.setAccessToken(accessToken).then(() => authResult)
|
|
176
|
+
})
|
|
177
|
+
.catch((error: FeathersError) => this.handleError(error, 'authenticate'))
|
|
179
178
|
|
|
180
|
-
this.app.set('authentication', promise)
|
|
179
|
+
this.app.set('authentication', promise)
|
|
181
180
|
|
|
182
|
-
return promise
|
|
181
|
+
return promise
|
|
183
182
|
}
|
|
184
183
|
|
|
185
|
-
logout
|
|
184
|
+
logout(): Promise<AuthenticationResult | null> {
|
|
186
185
|
return Promise.resolve(this.app.get('authentication'))
|
|
187
|
-
.then(() =>
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
)
|
|
186
|
+
.then(() =>
|
|
187
|
+
this.service.remove(null).then((authResult: AuthenticationResult) =>
|
|
188
|
+
this.removeAccessToken()
|
|
189
|
+
.then(() => this.reset())
|
|
190
|
+
.then(() => {
|
|
191
|
+
this.app.emit('logout', authResult)
|
|
192
|
+
|
|
193
|
+
return authResult
|
|
194
|
+
})
|
|
195
|
+
)
|
|
196
|
+
)
|
|
197
|
+
.catch((error: FeathersError) => this.handleError(error, 'logout'))
|
|
199
198
|
}
|
|
200
199
|
}
|
|
@@ -1,18 +1,26 @@
|
|
|
1
|
-
import { HookContext, NextFunction } from '@feathersjs/feathers'
|
|
2
|
-
import { stripSlashes } from '@feathersjs/commons'
|
|
1
|
+
import { HookContext, NextFunction } from '@feathersjs/feathers'
|
|
2
|
+
import { stripSlashes } from '@feathersjs/commons'
|
|
3
3
|
|
|
4
4
|
export const authentication = () => {
|
|
5
5
|
return (context: HookContext, next: NextFunction) => {
|
|
6
|
-
const {
|
|
6
|
+
const {
|
|
7
|
+
app,
|
|
8
|
+
params,
|
|
9
|
+
path,
|
|
10
|
+
method,
|
|
11
|
+
app: { authentication: service }
|
|
12
|
+
} = context
|
|
7
13
|
|
|
8
14
|
if (stripSlashes(service.options.path) === path && method === 'create') {
|
|
9
|
-
return next()
|
|
15
|
+
return next()
|
|
10
16
|
}
|
|
11
17
|
|
|
12
|
-
return Promise.resolve(app.get('authentication'))
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
return Promise.resolve(app.get('authentication'))
|
|
19
|
+
.then((authResult) => {
|
|
20
|
+
if (authResult) {
|
|
21
|
+
context.params = Object.assign({}, authResult, params)
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
.then(next)
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/hooks/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { authentication } from './authentication'
|
|
2
|
-
export { populateHeader } from './populate-header'
|
|
1
|
+
export { authentication } from './authentication'
|
|
2
|
+
export { populateHeader } from './populate-header'
|
|
@@ -1,20 +1,27 @@
|
|
|
1
|
-
import { HookContext, NextFunction } from '@feathersjs/feathers'
|
|
1
|
+
import { HookContext, NextFunction } from '@feathersjs/feathers'
|
|
2
2
|
|
|
3
3
|
export const populateHeader = () => {
|
|
4
4
|
return (context: HookContext, next: NextFunction) => {
|
|
5
|
-
const {
|
|
6
|
-
|
|
5
|
+
const {
|
|
6
|
+
app,
|
|
7
|
+
params: { accessToken }
|
|
8
|
+
} = context
|
|
9
|
+
const authentication = app.authentication
|
|
7
10
|
|
|
8
11
|
// Set REST header if necessary
|
|
9
12
|
if (app.rest && accessToken) {
|
|
10
|
-
const { scheme, header } = authentication.options
|
|
11
|
-
const authHeader = `${scheme} ${accessToken}
|
|
13
|
+
const { scheme, header } = authentication.options
|
|
14
|
+
const authHeader = `${scheme} ${accessToken}`
|
|
12
15
|
|
|
13
|
-
context.params.headers = Object.assign(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
context.params.headers = Object.assign(
|
|
17
|
+
{},
|
|
18
|
+
{
|
|
19
|
+
[header]: authHeader
|
|
20
|
+
},
|
|
21
|
+
context.params.headers
|
|
22
|
+
)
|
|
16
23
|
}
|
|
17
24
|
|
|
18
|
-
return next()
|
|
19
|
-
}
|
|
20
|
-
}
|
|
25
|
+
return next()
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,32 +1,36 @@
|
|
|
1
|
-
import { AuthenticationClient, AuthenticationClientOptions } from './core'
|
|
2
|
-
import * as hooks from './hooks'
|
|
3
|
-
import { Application } from '@feathersjs/feathers'
|
|
4
|
-
import { Storage, MemoryStorage, StorageWrapper } from './storage'
|
|
1
|
+
import { AuthenticationClient, AuthenticationClientOptions } from './core'
|
|
2
|
+
import * as hooks from './hooks'
|
|
3
|
+
import { Application } from '@feathersjs/feathers'
|
|
4
|
+
import { Storage, MemoryStorage, StorageWrapper } from './storage'
|
|
5
5
|
|
|
6
6
|
declare module '@feathersjs/feathers/lib/declarations' {
|
|
7
|
-
interface Application<Services, Settings> {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
interface Application<Services, Settings> {
|
|
8
|
+
// eslint-disable-line
|
|
9
|
+
io?: any
|
|
10
|
+
rest?: any
|
|
11
|
+
authentication: AuthenticationClient
|
|
12
|
+
authenticate: AuthenticationClient['authenticate']
|
|
13
|
+
reAuthenticate: AuthenticationClient['reAuthenticate']
|
|
14
|
+
logout: AuthenticationClient['logout']
|
|
14
15
|
}
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
export const getDefaultStorage = () => {
|
|
18
19
|
try {
|
|
19
|
-
return new StorageWrapper(window.localStorage)
|
|
20
|
+
return new StorageWrapper(window.localStorage)
|
|
20
21
|
} catch (error: any) {}
|
|
21
22
|
|
|
22
|
-
return new MemoryStorage()
|
|
23
|
-
}
|
|
23
|
+
return new MemoryStorage()
|
|
24
|
+
}
|
|
24
25
|
|
|
25
|
-
export { AuthenticationClient, AuthenticationClientOptions, Storage, MemoryStorage, hooks }
|
|
26
|
+
export { AuthenticationClient, AuthenticationClientOptions, Storage, MemoryStorage, hooks }
|
|
26
27
|
|
|
27
|
-
export type ClientConstructor = new (
|
|
28
|
+
export type ClientConstructor = new (
|
|
29
|
+
app: Application,
|
|
30
|
+
options: AuthenticationClientOptions
|
|
31
|
+
) => AuthenticationClient
|
|
28
32
|
|
|
29
|
-
export const defaultStorage: Storage = getDefaultStorage()
|
|
33
|
+
export const defaultStorage: Storage = getDefaultStorage()
|
|
30
34
|
|
|
31
35
|
export const defaults: AuthenticationClientOptions = {
|
|
32
36
|
header: 'Authorization',
|
|
@@ -38,29 +42,26 @@ export const defaults: AuthenticationClientOptions = {
|
|
|
38
42
|
path: '/authentication',
|
|
39
43
|
Authentication: AuthenticationClient,
|
|
40
44
|
storage: defaultStorage
|
|
41
|
-
}
|
|
45
|
+
}
|
|
42
46
|
|
|
43
47
|
const init = (_options: Partial<AuthenticationClientOptions> = {}) => {
|
|
44
|
-
const options: AuthenticationClientOptions = Object.assign({}, defaults, _options)
|
|
45
|
-
const { Authentication } = options
|
|
48
|
+
const options: AuthenticationClientOptions = Object.assign({}, defaults, _options)
|
|
49
|
+
const { Authentication } = options
|
|
46
50
|
|
|
47
51
|
return (app: Application) => {
|
|
48
|
-
const authentication = new Authentication(app, options)
|
|
52
|
+
const authentication = new Authentication(app, options)
|
|
49
53
|
|
|
50
|
-
app.authentication = authentication
|
|
51
|
-
app.authenticate = authentication.authenticate.bind(authentication)
|
|
52
|
-
app.reAuthenticate = authentication.reAuthenticate.bind(authentication)
|
|
53
|
-
app.logout = authentication.logout.bind(authentication)
|
|
54
|
+
app.authentication = authentication
|
|
55
|
+
app.authenticate = authentication.authenticate.bind(authentication)
|
|
56
|
+
app.reAuthenticate = authentication.reAuthenticate.bind(authentication)
|
|
57
|
+
app.logout = authentication.logout.bind(authentication)
|
|
54
58
|
|
|
55
|
-
app.hooks([
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
]);
|
|
59
|
-
};
|
|
60
|
-
};
|
|
59
|
+
app.hooks([hooks.authentication(), hooks.populateHeader()])
|
|
60
|
+
}
|
|
61
|
+
}
|
|
61
62
|
|
|
62
|
-
export default init
|
|
63
|
+
export default init
|
|
63
64
|
|
|
64
65
|
if (typeof module !== 'undefined') {
|
|
65
|
-
module.exports = Object.assign(init, module.exports)
|
|
66
|
+
module.exports = Object.assign(init, module.exports)
|
|
66
67
|
}
|
package/src/storage.ts
CHANGED
|
@@ -1,49 +1,49 @@
|
|
|
1
1
|
export interface Storage {
|
|
2
|
-
getItem
|
|
3
|
-
setItem?
|
|
4
|
-
removeItem?
|
|
2
|
+
getItem(key: string): any
|
|
3
|
+
setItem?(key: string, value: any): any
|
|
4
|
+
removeItem?(key: string): any
|
|
5
5
|
}
|
|
6
6
|
|
|
7
7
|
export class MemoryStorage implements Storage {
|
|
8
|
-
store: { [key: string]: any }
|
|
8
|
+
store: { [key: string]: any }
|
|
9
9
|
|
|
10
|
-
constructor
|
|
11
|
-
this.store = {}
|
|
10
|
+
constructor() {
|
|
11
|
+
this.store = {}
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
getItem
|
|
15
|
-
return Promise.resolve(this.store[key])
|
|
14
|
+
getItem(key: string) {
|
|
15
|
+
return Promise.resolve(this.store[key])
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
setItem
|
|
19
|
-
return Promise.resolve(this.store[key] = value)
|
|
18
|
+
setItem(key: string, value: any) {
|
|
19
|
+
return Promise.resolve((this.store[key] = value))
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
removeItem
|
|
23
|
-
const value = this.store[key]
|
|
22
|
+
removeItem(key: string) {
|
|
23
|
+
const value = this.store[key]
|
|
24
24
|
|
|
25
|
-
delete this.store[key]
|
|
25
|
+
delete this.store[key]
|
|
26
26
|
|
|
27
|
-
return Promise.resolve(value)
|
|
27
|
+
return Promise.resolve(value)
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
export class StorageWrapper implements Storage {
|
|
32
|
-
storage: any
|
|
32
|
+
storage: any
|
|
33
33
|
|
|
34
|
-
constructor
|
|
35
|
-
this.storage = storage
|
|
34
|
+
constructor(storage: any) {
|
|
35
|
+
this.storage = storage
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
getItem
|
|
39
|
-
return Promise.resolve(this.storage.getItem(key))
|
|
38
|
+
getItem(key: string) {
|
|
39
|
+
return Promise.resolve(this.storage.getItem(key))
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
setItem
|
|
43
|
-
return Promise.resolve(this.storage.setItem(key, value))
|
|
42
|
+
setItem(key: string, value: any) {
|
|
43
|
+
return Promise.resolve(this.storage.setItem(key, value))
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
removeItem
|
|
47
|
-
return Promise.resolve(this.storage.removeItem(key))
|
|
46
|
+
removeItem(key: string) {
|
|
47
|
+
return Promise.resolve(this.storage.removeItem(key))
|
|
48
48
|
}
|
|
49
49
|
}
|