@based/client 3.3.0 → 4.0.1

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.
Files changed (211) hide show
  1. package/README.md +32 -336
  2. package/dist/Emitter.d.ts +7 -6
  3. package/dist/Emitter.js +31 -6
  4. package/dist/Emitter.js.map +1 -1
  5. package/dist/authState/parseAuthState.d.ts +3 -0
  6. package/dist/authState/parseAuthState.js +51 -0
  7. package/dist/authState/parseAuthState.js.map +1 -0
  8. package/dist/authState/updateAuthState.d.ts +3 -0
  9. package/dist/authState/updateAuthState.js +15 -0
  10. package/dist/authState/updateAuthState.js.map +1 -0
  11. package/dist/channel/cleanUp.d.ts +2 -0
  12. package/dist/channel/cleanUp.js +32 -0
  13. package/dist/channel/cleanUp.js.map +1 -0
  14. package/dist/channel/index.d.ts +12 -0
  15. package/dist/channel/index.js +67 -0
  16. package/dist/channel/index.js.map +1 -0
  17. package/dist/genObserveId.d.ts +1 -0
  18. package/dist/genObserveId.js +12 -0
  19. package/dist/genObserveId.js.map +1 -0
  20. package/dist/getTargetInfo.d.ts +6 -0
  21. package/dist/getTargetInfo.js +14 -0
  22. package/dist/getTargetInfo.js.map +1 -0
  23. package/dist/getUrlFromOpts.d.ts +3 -0
  24. package/dist/getUrlFromOpts.js +24 -0
  25. package/dist/getUrlFromOpts.js.map +1 -0
  26. package/dist/incoming/debug.d.ts +9 -0
  27. package/dist/incoming/debug.js +132 -0
  28. package/dist/incoming/debug.js.map +1 -0
  29. package/dist/incoming/index.d.ts +2 -0
  30. package/dist/incoming/index.js +341 -0
  31. package/dist/incoming/index.js.map +1 -0
  32. package/dist/incoming/protocol.d.ts +9 -0
  33. package/dist/incoming/protocol.js +62 -0
  34. package/dist/incoming/protocol.js.map +1 -0
  35. package/dist/index.d.ts +69 -127
  36. package/dist/index.js +224 -15
  37. package/dist/index.js.map +1 -1
  38. package/dist/outgoing/debug.d.ts +8 -0
  39. package/dist/outgoing/debug.js +70 -0
  40. package/dist/outgoing/debug.js.map +1 -0
  41. package/dist/outgoing/index.d.ts +14 -0
  42. package/dist/outgoing/index.js +227 -0
  43. package/dist/outgoing/index.js.map +1 -0
  44. package/dist/outgoing/protocol.d.ts +24 -0
  45. package/dist/outgoing/protocol.js +200 -0
  46. package/dist/outgoing/protocol.js.map +1 -0
  47. package/dist/persistentStorage/browser.d.ts +5 -0
  48. package/dist/persistentStorage/browser.js +150 -0
  49. package/dist/persistentStorage/browser.js.map +1 -0
  50. package/dist/persistentStorage/index.d.ts +6 -0
  51. package/dist/persistentStorage/index.js +56 -0
  52. package/dist/persistentStorage/index.js.map +1 -0
  53. package/dist/persistentStorage/node.d.ts +6 -0
  54. package/dist/persistentStorage/node.js +104 -0
  55. package/dist/persistentStorage/node.js.map +1 -0
  56. package/dist/query/index.d.ts +17 -0
  57. package/dist/query/index.js +112 -0
  58. package/dist/query/index.js.map +1 -0
  59. package/dist/stream/fetch.d.ts +4 -0
  60. package/dist/stream/fetch.js +42 -0
  61. package/dist/stream/fetch.js.map +1 -0
  62. package/dist/stream/index.d.ts +4 -0
  63. package/dist/stream/index.js +65 -0
  64. package/dist/stream/index.js.map +1 -0
  65. package/dist/stream/nodeStream.d.ts +7 -0
  66. package/dist/stream/nodeStream.js +117 -0
  67. package/dist/stream/nodeStream.js.map +1 -0
  68. package/dist/stream/types.d.ts +37 -0
  69. package/dist/stream/types.js +8 -0
  70. package/dist/stream/types.js.map +1 -0
  71. package/dist/stream/uploadFileBrowser.d.ts +4 -0
  72. package/dist/stream/uploadFileBrowser.js +113 -0
  73. package/dist/stream/uploadFileBrowser.js.map +1 -0
  74. package/dist/types/auth.d.ts +11 -0
  75. package/dist/{selvaTypes/get.js → types/auth.js} +1 -1
  76. package/dist/types/auth.js.map +1 -0
  77. package/dist/types/cache.d.ts +6 -0
  78. package/dist/{selvaTypes/set.js → types/cache.js} +1 -1
  79. package/dist/types/cache.js.map +1 -0
  80. package/dist/types/channel.d.ts +25 -0
  81. package/dist/types/channel.js +3 -0
  82. package/dist/types/channel.js.map +1 -0
  83. package/dist/types/error.d.ts +36 -0
  84. package/dist/types/error.js +40 -0
  85. package/dist/types/error.js.map +1 -0
  86. package/dist/types/events.d.ts +34 -0
  87. package/dist/{selvaTypes/schema.js → types/events.js} +1 -1
  88. package/dist/types/events.js.map +1 -0
  89. package/dist/types/functions.d.ts +8 -0
  90. package/dist/types/functions.js +3 -0
  91. package/dist/types/functions.js.map +1 -0
  92. package/dist/types/generic.d.ts +20 -0
  93. package/dist/types/generic.js +3 -0
  94. package/dist/types/generic.js.map +1 -0
  95. package/dist/types/index.d.ts +6 -0
  96. package/dist/{subscriptions → types}/index.js +6 -10
  97. package/dist/types/index.js.map +1 -0
  98. package/dist/types/observe.d.ts +46 -0
  99. package/dist/types/observe.js +3 -0
  100. package/dist/types/observe.js.map +1 -0
  101. package/dist/websocket/index.d.ts +1 -1
  102. package/dist/websocket/index.js +14 -5
  103. package/dist/websocket/index.js.map +1 -1
  104. package/dist/websocket/types.d.ts +0 -0
  105. package/dist/websocket/types.js +0 -0
  106. package/dist/websocket/types.js.map +0 -0
  107. package/dist/websocket/urlLoader.d.ts +0 -0
  108. package/dist/websocket/urlLoader.js +0 -0
  109. package/dist/websocket/urlLoader.js.map +0 -0
  110. package/package.json +17 -27
  111. package/dist/Client.d.ts +0 -80
  112. package/dist/Client.js +0 -271
  113. package/dist/Client.js.map +0 -1
  114. package/dist/auth.d.ts +0 -13
  115. package/dist/auth.js +0 -87
  116. package/dist/auth.js.map +0 -1
  117. package/dist/createError.d.ts +0 -3
  118. package/dist/createError.js +0 -67
  119. package/dist/createError.js.map +0 -1
  120. package/dist/debug.d.ts +0 -2
  121. package/dist/debug.js +0 -21
  122. package/dist/debug.js.map +0 -1
  123. package/dist/file/fetch.d.ts +0 -6
  124. package/dist/file/fetch.js +0 -27
  125. package/dist/file/fetch.js.map +0 -1
  126. package/dist/file/getUrl.d.ts +0 -4
  127. package/dist/file/getUrl.js +0 -42
  128. package/dist/file/getUrl.js.map +0 -1
  129. package/dist/file/index.d.ts +0 -6
  130. package/dist/file/index.js +0 -95
  131. package/dist/file/index.js.map +0 -1
  132. package/dist/file/stream/browser.d.ts +0 -7
  133. package/dist/file/stream/browser.js +0 -17
  134. package/dist/file/stream/browser.js.map +0 -1
  135. package/dist/file/stream/index.d.ts +0 -7
  136. package/dist/file/stream/index.js +0 -86
  137. package/dist/file/stream/index.js.map +0 -1
  138. package/dist/file/uploadFileBrowser.d.ts +0 -3
  139. package/dist/file/uploadFileBrowser.js +0 -72
  140. package/dist/file/uploadFileBrowser.js.map +0 -1
  141. package/dist/findPrefix.d.ts +0 -3
  142. package/dist/findPrefix.js +0 -20
  143. package/dist/findPrefix.js.map +0 -1
  144. package/dist/idleTimeout.d.ts +0 -3
  145. package/dist/idleTimeout.js +0 -17
  146. package/dist/idleTimeout.js.map +0 -1
  147. package/dist/observable/index.d.ts +0 -28
  148. package/dist/observable/index.js +0 -84
  149. package/dist/observable/index.js.map +0 -1
  150. package/dist/printBasedObject.d.ts +0 -4
  151. package/dist/printBasedObject.js +0 -76
  152. package/dist/printBasedObject.js.map +0 -1
  153. package/dist/queue.d.ts +0 -5
  154. package/dist/queue.js +0 -53
  155. package/dist/queue.js.map +0 -1
  156. package/dist/request.d.ts +0 -6
  157. package/dist/request.js +0 -67
  158. package/dist/request.js.map +0 -1
  159. package/dist/selvaTypes/get.d.ts +0 -97
  160. package/dist/selvaTypes/get.js.map +0 -1
  161. package/dist/selvaTypes/schema.d.ts +0 -89
  162. package/dist/selvaTypes/schema.js.map +0 -1
  163. package/dist/selvaTypes/set.d.ts +0 -73
  164. package/dist/selvaTypes/set.js.map +0 -1
  165. package/dist/subscriptions/addGetSubscriber.d.ts +0 -3
  166. package/dist/subscriptions/addGetSubscriber.js +0 -87
  167. package/dist/subscriptions/addGetSubscriber.js.map +0 -1
  168. package/dist/subscriptions/addSubscriber.d.ts +0 -2
  169. package/dist/subscriptions/addSubscriber.js +0 -136
  170. package/dist/subscriptions/addSubscriber.js.map +0 -1
  171. package/dist/subscriptions/generateId.d.ts +0 -1
  172. package/dist/subscriptions/generateId.js +0 -11
  173. package/dist/subscriptions/generateId.js.map +0 -1
  174. package/dist/subscriptions/incomingSubscription.d.ts +0 -3
  175. package/dist/subscriptions/incomingSubscription.js +0 -87
  176. package/dist/subscriptions/incomingSubscription.js.map +0 -1
  177. package/dist/subscriptions/incomingSubscriptionDiff.d.ts +0 -3
  178. package/dist/subscriptions/incomingSubscriptionDiff.js +0 -85
  179. package/dist/subscriptions/incomingSubscriptionDiff.js.map +0 -1
  180. package/dist/subscriptions/index.d.ts +0 -10
  181. package/dist/subscriptions/index.js.map +0 -1
  182. package/dist/subscriptions/logoutSubscriptions.d.ts +0 -3
  183. package/dist/subscriptions/logoutSubscriptions.js +0 -36
  184. package/dist/subscriptions/logoutSubscriptions.js.map +0 -1
  185. package/dist/subscriptions/removeSendSubscriptionDataFromQueue.d.ts +0 -2
  186. package/dist/subscriptions/removeSendSubscriptionDataFromQueue.js +0 -14
  187. package/dist/subscriptions/removeSendSubscriptionDataFromQueue.js.map +0 -1
  188. package/dist/subscriptions/removeSubscriber.d.ts +0 -2
  189. package/dist/subscriptions/removeSubscriber.js +0 -44
  190. package/dist/subscriptions/removeSubscriber.js.map +0 -1
  191. package/dist/subscriptions/removeUnsubscribesFromQueue.d.ts +0 -2
  192. package/dist/subscriptions/removeUnsubscribesFromQueue.js +0 -14
  193. package/dist/subscriptions/removeUnsubscribesFromQueue.js.map +0 -1
  194. package/dist/subscriptions/sendAllSubscriptions.d.ts +0 -2
  195. package/dist/subscriptions/sendAllSubscriptions.js +0 -114
  196. package/dist/subscriptions/sendAllSubscriptions.js.map +0 -1
  197. package/dist/token.d.ts +0 -4
  198. package/dist/token.js +0 -35
  199. package/dist/token.js.map +0 -1
  200. package/dist/track.d.ts +0 -8
  201. package/dist/track.js +0 -71
  202. package/dist/track.js.map +0 -1
  203. package/docs/auth-based-ui-howto.md +0 -74
  204. package/docs/auth-howto.md +0 -166
  205. package/docs/auth.md +0 -93
  206. package/docs/authorize.md +0 -56
  207. package/docs/files.md +0 -66
  208. package/docs/get-started.md +0 -60
  209. package/docs/get.md +0 -759
  210. package/docs/schema.md +0 -244
  211. package/docs/set.md +0 -652
@@ -1,114 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.sendAllSubscriptions = void 0;
4
- const types_1 = require("@based/types");
5
- const queue_1 = require("../queue");
6
- const sendAllSubscriptions = (client, reAuth = false) => {
7
- for (const key in client.subscriptions) {
8
- const subscriptionId = Number(key);
9
- const subscription = client.subscriptions[key];
10
- if (reAuth && !subscription.authError) {
11
- // delete subscrption.authError
12
- continue;
13
- }
14
- // for loop for has to include (for gets)
15
- let getInQ;
16
- let queued;
17
- let getIndex;
18
- for (let i = 0; i < client.subscriptionQueue.length; i++) {
19
- const [type, id] = client.subscriptionQueue[i];
20
- if (id === subscriptionId) {
21
- if (type === types_1.RequestTypes.GetSubscription) {
22
- getIndex = i;
23
- getInQ = client.subscriptionQueue[i];
24
- }
25
- else if (type === types_1.RequestTypes.Subscription) {
26
- queued = client.subscriptionQueue[i];
27
- }
28
- }
29
- }
30
- if (getInQ && queued) {
31
- console.error('GET IN Q AND SUB IN Q SHOULD BE IMPOSSIBLE');
32
- }
33
- const cache = client.cache[subscriptionId];
34
- // console.info(getInQ, queued)
35
- let x = false;
36
- if (getInQ) {
37
- let onlyGets = true;
38
- for (const k in subscription.subscribers) {
39
- const s = subscription.subscribers[k];
40
- if (s.onData) {
41
- onlyGets = false;
42
- break;
43
- }
44
- }
45
- if (onlyGets) {
46
- x = true;
47
- }
48
- else {
49
- console.info('not only gets remove get');
50
- client.subscriptionQueue.splice(getIndex, 1);
51
- }
52
- if (cache && getInQ[3] !== cache.checksum) {
53
- getInQ[3] = cache.checksum;
54
- }
55
- }
56
- if (!x) {
57
- if (queued) {
58
- if (cache && queued[3] !== cache.checksum) {
59
- queued[3] = cache.checksum;
60
- if (getInQ) {
61
- queued[4] = 2;
62
- }
63
- }
64
- }
65
- else {
66
- const { name, query } = client.subscriptions[subscriptionId];
67
- if (name) {
68
- if (cache) {
69
- (0, queue_1.addToQueue)(client, [
70
- types_1.RequestTypes.Subscription,
71
- subscriptionId,
72
- query,
73
- cache.checksum,
74
- getInQ ? 2 : 0,
75
- name,
76
- ]);
77
- }
78
- else {
79
- (0, queue_1.addToQueue)(client, [
80
- types_1.RequestTypes.Subscription,
81
- subscriptionId,
82
- query,
83
- 0,
84
- getInQ ? 2 : 0,
85
- name,
86
- ]);
87
- }
88
- }
89
- else {
90
- if (cache) {
91
- (0, queue_1.addToQueue)(client, [
92
- types_1.RequestTypes.Subscription,
93
- subscriptionId,
94
- query,
95
- cache.checksum,
96
- getInQ ? 2 : 0,
97
- ]);
98
- }
99
- else {
100
- (0, queue_1.addToQueue)(client, [
101
- types_1.RequestTypes.Subscription,
102
- subscriptionId,
103
- query,
104
- 0,
105
- getInQ ? 2 : 0,
106
- ]);
107
- }
108
- }
109
- }
110
- }
111
- }
112
- };
113
- exports.sendAllSubscriptions = sendAllSubscriptions;
114
- //# sourceMappingURL=sendAllSubscriptions.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sendAllSubscriptions.js","sourceRoot":"","sources":["../../src/subscriptions/sendAllSubscriptions.ts"],"names":[],"mappings":";;;AACA,wCAA2C;AAC3C,oCAAqC;AAE9B,MAAM,oBAAoB,GAAG,CAClC,MAAmB,EACnB,SAAkB,KAAK,EACvB,EAAE;IACF,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,aAAa,EAAE;QACtC,MAAM,cAAc,GAAG,MAAM,CAAC,GAAG,CAAC,CAAA;QAClC,MAAM,YAAY,GAAG,MAAM,CAAC,aAAa,CAAC,GAAG,CAAC,CAAA;QAC9C,IAAI,MAAM,IAAI,CAAC,YAAY,CAAC,SAAS,EAAE;YACrC,+BAA+B;YAC/B,SAAQ;SACT;QAED,yCAAyC;QAEzC,IAAI,MAAM,CAAA;Q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AAC,CAAC;yBACf,CAAC,CAAA;qBACH;yBAAM;wBACL,IAAA,kBAAU,EAAC,MAAM,EAAE;4BACjB,oBAAY,CAAC,YAAY;4BACzB,cAAc;4BACd,KAAK;4BACL,CAAC;4BACD,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;yBACf,CAAC,CAAA;qBACH;iBACF;aACF;SACF;KACF;AACH,CAAC,CAAA;AAnHY,QAAA,oBAAoB,wBAmHhC"}
package/dist/token.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import { BasedClient } from '.';
2
- import { SendTokenOptions } from '@based/types';
3
- declare const sendToken: (client: BasedClient, token?: string, options?: SendTokenOptions) => void;
4
- export default sendToken;
package/dist/token.js DELETED
@@ -1,35 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const types_1 = require("@based/types");
7
- const idleTimeout_1 = __importDefault(require("./idleTimeout"));
8
- const subscriptions_1 = require("./subscriptions");
9
- const notUndefinedOrNull = (v) => typeof v !== 'undefined' && v !== null;
10
- const sendToken = (client, token, options) => {
11
- client.beingAuth = true;
12
- if (token) {
13
- client.token = token;
14
- client.sendTokenOptions = options;
15
- }
16
- else {
17
- for (const id in client.cache) {
18
- if (!client.subscriptions[id]) {
19
- delete client.cache[id];
20
- }
21
- }
22
- delete client.token;
23
- delete client.sendTokenOptions;
24
- }
25
- if (client.connected) {
26
- const message = token
27
- ? [types_1.RequestTypes.Token, token, options].filter(notUndefinedOrNull)
28
- : [types_1.RequestTypes.Token];
29
- client.connection.ws.send(JSON.stringify(message));
30
- (0, idleTimeout_1.default)(client);
31
- (0, subscriptions_1.sendAllSubscriptions)(client, true);
32
- }
33
- };
34
- exports.default = sendToken;
35
- //# sourceMappingURL=token.js.map
package/dist/token.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"token.js","sour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Opts } from '.';
2
- export declare const genKey: (type: string, params?: {
3
- [key: string]: string | number | boolean;
4
- }) => string;
5
- declare const _default: (client: BasedClient, type: string, params?: {
6
- [key: string]: string | number | boolean;
7
- }, untrack?: boolean, event?: boolean, opts?: TrackOpts, del?: boolean) => void;
8
- export default _default;
package/dist/track.js DELETED
@@ -1,71 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.genKey = void 0;
4
- const queue_1 = require("./queue");
5
- const types_1 = require("@based/types");
6
- const hash_1 = require("@saulx/hash");
7
- const notUnique = new Set();
8
- const isUnique = (x) => {
9
- if (typeof window !== 'undefined') {
10
- if (notUnique.has(x)) {
11
- return false;
12
- }
13
- notUnique.add(x);
14
- try {
15
- const name = '_ba:' + (0, hash_1.hashCompact)(x);
16
- if (localStorage.getItem(name)) {
17
- return false;
18
- }
19
- localStorage.setItem(name, '1');
20
- return true;
21
- }
22
- catch (err) {
23
- return false;
24
- }
25
- }
26
- return false;
27
- };
28
- const genKey = (type, params) => {
29
- if (params) {
30
- let str = `${type}`;
31
- const keys = Object.keys(params).sort();
32
- for (const k of keys) {
33
- str += '_' + k + '_' + params[k];
34
- }
35
- return str;
36
- }
37
- else {
38
- return type;
39
- }
40
- };
41
- exports.genKey = genKey;
42
- exports.default = (client, type, params, untrack, event, opts, del) => {
43
- const k = (0, exports.genKey)(type, params);
44
- const payload = { t: k };
45
- if (del) {
46
- payload.r = 1;
47
- }
48
- if (event) {
49
- payload.e = 1;
50
- if (opts) {
51
- payload.o = opts;
52
- }
53
- }
54
- if (untrack) {
55
- payload.s = 1;
56
- client.tracking.delete(k);
57
- }
58
- else if (!del) {
59
- if (!event) {
60
- if (!client.tracking) {
61
- client.tracking = new Set();
62
- }
63
- client.tracking.add(k);
64
- }
65
- if (isUnique(k)) {
66
- payload.u = 1;
67
- }
68
- }
69
- (0, queue_1.addToQueue)(client, [types_1.RequestTypes.Track, payload]);
70
- };
71
- //# sourceMappingURL=track.js.map
package/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CACb,MAAmB,EACnB,IAAY,EACZ,MAAqD,EACrD,OAAiB,EACjB,KAAe,EACf,IAAgB,EAChB,GAAa,EACb,EAAE;IACF,MAAM,CAAC,GAAG,IAAA,cAAM,EAAC,IAAI,EAAE,MAAM,CAAC,CAAA;IAC9B,MAAM,OAAO,GAAiB,EAAE,CAAC,EAAE,CAAC,EAAE,CAAA;IACtC,IAAI,GAAG,EAAE;QACP,OAAO,CAAC,CAAC,GAAG,CAAC,CAAA;KACd;IACD,IAAI,KAAK,EAAE;QACT,OAAO,CAAC,CAAC,GAAG,CAAC,CAAA;QACb,IAAI,IAAI,EAAE;YACR,OAAO,CAAC,CAAC,GAAG,IAAI,CAAA;SACjB;KACF;IACD,IAAI,OAAO,EAAE;QACX,OAAO,CAAC,CAAC,GAAG,CAAC,CAAA;QACb,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;KAC1B;SAAM,IAAI,CAAC,GAAG,EAAE;QACf,IAAI,CAAC,KAAK,EAAE;YACV,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE;gBACpB,MAAM,CAAC,QAAQ,GAAG,IAAI,GAAG,EAAE,CAAA;aAC5B;YACD,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;SACvB;QAED,IAAI,QAAQ,CAAC,CAAC,CAAC,EAAE;YACf,OAAO,CAAC,CAAC,GAAG,CAAC,CAAA;SACd;KACF;IACD,IAAA,kBAAU,EAAC,MAAM,EAAE,CAAC,oBAAY,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAA;AACnD,CAAC,CAAA"}
@@ -1,74 +0,0 @@
1
- ## JWT Auth Flow HowTo using based/ui
2
-
3
- This guide shows how to implement authentication using [based/ui](https://github.com/atelier-saulx/ui) UI library and its authentication components.
4
- The tutorial example files can be found in [this repository](https://github.com/atelier-saulx/auth-demo).
5
-
6
- ### Imports
7
-
8
- Components can be imported or required from the [`@based/ui`](https://www.npmjs.com/package/@based/ui) npm package.
9
- ```bash
10
- $ npm install @based/ui
11
- ```
12
-
13
- ```javascript
14
- import { Provider, Authorize } from '@based/ui'
15
- ```
16
-
17
- ### Provider
18
-
19
- The first step is to wrap the app with a `<Provider />` component. This will set up the app to handle third-party auth provider callbacks and handle overlays and themes from the `based/ui` library.
20
- The `<Provider />` component expects the main app component as a child and a [based client](https://github.com/atelier-saulx/based/blob/main/packages/client/README.md) as the `client` argument.
21
-
22
- ```javascript
23
- const client = based(basedConfig)
24
- render(
25
- <Provider client={client}>
26
- <App />
27
- </Provider>,
28
- rootEl
29
- )
30
- ```
31
-
32
- ### Authorize component
33
-
34
- The Authorize component renders other components only when a user is authenticated. If the user is not authenticated, it will show a login/register dialog.
35
- Email, Google, Microsoft, and Github provider handler functions are installed in new environments by default. We encourage you to check these [templates](https://github.com/atelier-saulx/based/tree/main/packages/templates/jwtAuth/functions) and adapt them to your needs by adding the [data functions](https://github.com/atelier-saulx/based/tree/main/packages/templates/jwtAuth/functions) to your project and [deploying](https://github.com/atelier-saulx/based/blob/main/packages/cli/README.md#deploy) them with the [based CLI](https://github.com/atelier-saulx/based/blob/main/packages/cli/README.md).
36
-
37
- ```javascript
38
- <Authorize
39
- // googleClientId="<your_google_client_id>"
40
- // microsoftClientId="<your_microsoft_client_id>"
41
- // githubClientId="<your_github_client_id>"
42
- app={LoggedinBody}
43
- />
44
- ```
45
-
46
- The protected sections of your site should be wrapped in a component passed to the `app` argument.
47
- The component is passed as an argument instead of `children`, so the protected components are never rendered if the user is not authenticated.
48
-
49
- ### Email provider
50
-
51
- New environments came by default, with a `user` type ready to receive email registrations. The [`register` user data function](https://github.com/atelier-saulx/based/blob/main/packages/templates/jwtAuth/functions/registerUser/index.ts) registers new users using the name and email address, sends a confirmation email, and waits for the new user to click the email message he received confirming the email address.
52
-
53
- ### Google provider
54
-
55
- To enable the Sign In with Google provider and button, you need to configure the google client id and google client secret.
56
- Follow the instructions in [this](https://developers.google.com/identity/sign-in/web/sign-in) page to the point where you have both.
57
- The client id is configured in the `<Authorize />` component as the `googleClientId` argument.
58
- The client secret should be saved as a [secret](https://github.com/atelier-saulx/based/tree/main/packages/cli#secrets) with the name `google-client-secret-<project>-<env>` where `<project>` and `<env>` should be replaced with the name of your project and environment.
59
- The Google credential should be configured with your domain and `https://<your_domain>/auth-google` as the callback address.
60
-
61
- ### Microsoft provider
62
-
63
- To enable the Microsoft Identity Platform provider, you need to configure a Microsoft client id.
64
- Follow the instructions in [this](https://docs.microsoft.com/en-us/azure/active-directory/develop/web-api-quickstart?pivots=devlang-aspnet) page until you have the Microsoft client id.
65
- The client id is configured in the `<Authorize />` component with the `microsoftClientId` argument.
66
- The Azure application should be configured with your domain and `https://<your_domain>/auth-microsoft` as the callback address.
67
-
68
- ### GitHub provider
69
-
70
- To enable the GitHub Oauth provider, your need to configure your GitHub client id and client secret.
71
- Follow the instructions in [this](https://docs.github.com/en/developers/apps/building-github-apps/creating-a-github-app) page to get both.
72
- Be sure to de your domain name and user `https://<your_domain>/auth-github` as the callback address.
73
- The client id should be configured in the `<Authorize />` component using the `githubClientId` argument. The client secret should be saved as a [secret](https://github.com/atelier-saulx/based/tree/main/packages/cli#secrets) with the name `github-client-secret-<project>-<env>` where `<project>` and `<env>` should be replaced with the name of your project and environment.
74
-
@@ -1,166 +0,0 @@
1
- ## JWT Auth Flow HowTo
2
-
3
- A short guide to setting up the JWT authentication flow using based.
4
- An example with the info on this guide can be foud [here](https://github.com/atelier-saulx/auth-demo)
5
-
6
- If you created an environment using the based website, the JWT auth flow template is installed by default. This template includes the `user` type schema, the public and private keys, and the default `authorize`, `login`, `logout`, and `renewToken` data functions.
7
-
8
- These default data functions can be customized to your needs. Add them to your project from [here](https://github.com/atelier-saulx/based/tree/main/packages/auth-templates/jwt) and deploy with the [based cli `deploy` command](https://github.com/atelier-saulx/based/tree/main/packages/cli#deploy).
9
-
10
- ### Adding a user to the database
11
-
12
- You now need to add a user to the database so that he can log in.
13
- There are several ways to do this, including the data editor in the Based website admin area.
14
- We'll add it through a script from the developer's computer environment as it also shows how you can add this functionality from your own apps.
15
-
16
- The `user` type installed by default has the following schema:
17
-
18
- ```
19
- ...
20
- types: {
21
- user: {
22
- prefix: 'us',
23
- fields: {
24
- name: { type: 'string' },
25
- email: { type: 'email' },
26
- password: { type: 'digest' },
27
- },
28
- },
29
- },
30
- ```
31
-
32
- It is ready to accept users with an email, name string, and hashed password.
33
- Let's create a user from a script.
34
-
35
- #### Setting up an apiKey
36
-
37
- If we try to add a user now from a script, we get into a chicken and the egg issue. We don't have an authenticated user to add the user itself. You can work around this while developing using the apiKey functionality.
38
- This feature lets us create an apikey so we can use based in utility scripts and commands using the [based cli](https://github.com/atelier-saulx/based/tree/main/packages/cli).
39
-
40
- ```bash
41
- $ npx based apiKeys add --name auth-demo-api-key --file ./apiKey.key
42
- ```
43
-
44
- This command will create an apiKey called "auth-demo-api-key" and saves its value to a file we can load from the script or use as an argument in the [based cli](https://github.com/atelier-saulx/based/tree/main/packages/cli).
45
-
46
- **Note:** You should never store apiKeys as plain text in repositories or use them to make based connections client side.
47
-
48
- #### Setting a user with a script
49
-
50
- With the apiKey, we can now run a simple script to add a new user:
51
-
52
- ```javascript
53
- // scripts/createUser.ts
54
-
55
- import fs from 'fs'
56
- import based from '@based/client'
57
-
58
- // Loads the based config file that sets up your based connection
59
- const basedConfig = JSON.parse(fs.readFileSync('../based.json', 'utf8'))
60
-
61
- // Loads apiKey so we can have based access from a script
62
- // Note: apiKeys should only be used for util scripts or server/server connections
63
- const apiKey = fs.readFileSync('../apiKey.key', 'utf8')
64
-
65
- ;(async () => {
66
- // Create a based client
67
- const client = based(basedConfig)
68
-
69
- // Authenticate client with an apiKey
70
- await client.auth(apiKey, { isApiKey: true })
71
-
72
- // Add our user
73
- const { id } = await client.set({
74
- type: 'user',
75
- name: 'Demo User',
76
- email: 'demo@wawa.com',
77
- password: 'superStrongPassword',
78
- })
79
- console.log('Added user ' + id)
80
-
81
- process.exit()
82
- })()
83
- ```
84
-
85
- And lets run it:
86
- ```bash
87
- $ cd scripts
88
- $ npx ts-node scripts/createUser.ts
89
- Added user us1b3d5a36
90
- ```
91
-
92
- ## Adding login functionality to your app.
93
-
94
- To login a user from your app, you should create a login UI, and use the based client `.login()` method to validate your user and password.
95
- Example:
96
-
97
- ```javascript
98
- import based from '@based/client'
99
-
100
- // ...
101
-
102
-
103
- <button
104
- onClick={async () => {
105
- const { token, refreshToken } = await client.login({
106
- email,
107
- password,
108
- })
109
- setToken(token)
110
- setRefreshToken(refreshToken)
111
- }}
112
- >
113
- Login
114
- </button>
115
- ```
116
-
117
- The `.login()` method takes the email and password supplied by the user, passes it to the `login` data function, and returns the result. Usually, a token and refreshToken if it was successful.
118
- You should also store your token and refreshToken if you want the session to persist.
119
-
120
- ## Authenticating the user with a persisted token
121
-
122
- If your user already logged in and you persisted the tokens, you can authenticate it with the still valid token instead of login in.
123
- Example:
124
- ```javascript
125
- // ...
126
- if (token) {
127
- await client.auth(token, { refreshToken })
128
- }
129
- ```
130
-
131
- The `refreshToken` is also stored by the based client so it can be requested by the based servers if the token is expired and it tries to renew the token automatically.
132
-
133
- ## Login out user
134
-
135
- You should run the based client `.logout()` method to explicitly log out a user. This method will clear the tokens on the based client and run the `logout` data function meant to perform clean-up actions like invalidating the current refreshToken.
136
-
137
- Example:
138
- ```javascript
139
- // ...
140
- <button
141
- onClick={async () => {
142
- await client.logout()
143
- setToken(null)
144
- setRefreshToken(null)
145
- }}
146
- >
147
- Logout
148
- </button>
149
- ```
150
-
151
- You should also clean the stored token and refreshToken from the browser.
152
-
153
- ## Handling automatic token renewal
154
-
155
- The default template already has in place JWT token renewal using a refreshToken. You should however store the new token locally when this happens.
156
- For this we have the `renewToken` based client event that we can listen.
157
- Example:
158
-
159
- ```javascript
160
- // ...
161
- const renewHandler = ({ token: newToken }: { token: string }) => {
162
- setToken(newToken)
163
- }
164
-
165
- client.on('renewToken', renewHandler)
166
- ```
package/docs/auth.md DELETED
@@ -1,93 +0,0 @@
1
- # The Auth System
2
-
3
- The *Based Auth* system helps developers add a security layer to their data. It does not enforce any logic itself, instead relies on a set of user-configurable *data functions* that restrict data access and implement whatever security strategy the user needs. Be it a 3rd party implementation or its own.
4
-
5
- The central concept is that all messages through based go through a [`authorize`](https://github.com/atelier-saulx/based/blob/main/packages/client/docs/authorize.md) function that implements the security logic and decides if each specific call to data should continue or be blocked.
6
- Within the data request, a "token" is also sent. This token is passed along to the `authorize` function for context, and it is what identifies the user in most implementations.
7
-
8
- There are no assumptions about the "token" format. It is up to the user to choose what implementation it wants or needs. However, we provide templates for JSON Web Tokens (JWT) authorization and will soon offer session-based authentication templates. These templates provide minimal authorization strategy implementation and are meant to be tweaked and updated by the user.
9
- Along with the `authorize` function, these templates also implement more optional functions that make a login flow more straightforward and more semantic. `login`, `logout`, and `renewToken`.
10
-
11
- The *Based* client also catches exceptions triggered by token expiration. When this happens, it tries to call a `renewToken` function sending it a renew payload that includes, for example, the "refreshToken" used for JWT authorization flow. If that's successful and it returns a new token, it will re-request the data with the new token for transparent renewal.
12
-
13
- ## Auth data functions
14
-
15
- ### `authorize`
16
-
17
- This data function runs every time the based client tries to call, get, modify or observe data.
18
- It approves or denies the request depending on its context. See [`authorize`](https://github.com/atelier-saulx/based/blob/main/packages/client/docs/authorize.md)
19
- Returns boolean value allowing or disallowing the connection.
20
-
21
- Example: see [here](https://github.com/atelier-saulx/based/blob/main/packages/templates/jwtAuth/functions/authorize/index.ts)
22
-
23
- ### `login`
24
-
25
- Called then the client [login()]() method is called. It should authenticate the user and generate the tokens. Authentication flow is up to the user.
26
- For example, in a JWT flow, this function will validate the user in the data, sign a token and refreshToken returning them to the client.
27
-
28
- Example: see [here](https://github.com/atelier-saulx/based/blob/main/packages/templates/jwtAuth/functions/login/index.ts)
29
-
30
- ### `logout`
31
-
32
- When your app logs out a user, it should call the `client.logout()` method. This method calls this data function if it exists. It is meant to have token invalidation and any cleanup that should happen when a user logs out.
33
-
34
- Example: see [here](https://github.com/atelier-saulx/based/blob/main/packages/templates/jwtAuth/functions/logout/index.ts)
35
-
36
- ### `renewToken`
37
-
38
- For auth flows that rely on token renewal. This data function runs when a specific error is triggered in the `authorize` function.
39
- The result is returned to the client using the `renewToken` client event.
40
-
41
- Example: see [here](https://github.com/atelier-saulx/based/blob/main/packages/templates/jwtAuth/functions/renewToken/index.ts)
42
-
43
- ## Based client methods
44
-
45
- ### `client.auth()`
46
-
47
- Sets the token to be sent during client/server messages. The token will be passed to the `authorize` function and can be decoded from it with `based.token()`.
48
-
49
- #### Usage
50
- ```javascript
51
- // Setting token
52
- await client.auth(token)
53
-
54
- // Setting token and refreshToken for JWT flow
55
- await client.auth(token, {
56
- renewOptions: { refreshToken }
57
- })
58
- ```
59
-
60
- ### `client.login()`
61
-
62
- This data function sends the email and password to the `login` data function for authentication.
63
- Returns tokens if successful. Also, it will automatically set the returned token on the current client connection.
64
-
65
- #### Usage
66
- ```javascript
67
- const tokens = await client.login({email, password})
68
- ```
69
-
70
- ### `client.logout()`
71
-
72
- Clears the tokens stored by the client and calls the `logout` data function if it exists.
73
-
74
- #### Usage
75
- ```javascript
76
- await client.logout()
77
- ```
78
-
79
- ### `client.on('renewToken', fn)`
80
-
81
- This event is called when the renewToken data function is run. It is used to handle and persist the newly generated tokens.
82
-
83
- #### Usage
84
- ```javascript
85
- client.on('renewToken', (renewTokenResult) => {})
86
- ```
87
-
88
-
89
- ## How to use
90
-
91
- Check the guides bellow for examples of how the use:
92
- - [React](https://github.com/atelier-saulx/based/blob/main/packages/client/docs/auth-howto.md)
93
- - [React with based/ui library](https://github.com/atelier-saulx/based/blob/main/packages/client/docs/auth-based-ui-howto.md)
package/docs/authorize.md DELETED
@@ -1,56 +0,0 @@
1
- # `Authorize` data function
2
-
3
- The authorize data function is a special function that serves as an authorization gatekeeper for the based queries and calls to data functions. It is run every time one is requested and blocks or allows access to the data.
4
- It works and has the same signature as normal data functions and should return a Promise with `true` or `false` boolean.
5
- You can have your access logic here. For example, checking if the user is authenticated or if it matches whatever criteria you need for your application.
6
-
7
- example:
8
- ```javascript
9
- export default async ({ based, user, payload, name, type, callstack}) => {
10
- // ...
11
- return true // or false
12
- }
13
- ```
14
-
15
- ### Exposed parameters
16
-
17
- | Parameter | Description |
18
- |-------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
19
- | `based` | Based client |
20
- | `user` | User object. Exposes token information about the user |
21
- | `payload` | The query requested or the payload sent to the function |
22
- | `name` | Name of the function. Empty in case of a query |
23
- | `type` | Type of call. `function` or `query` usually |
24
- | `callstack` | Array with the names of the data function call stack. If one function calls another, the first function name will show in the callstack of the second function and so on. |
25
-
26
-
27
- ## `user.token()`
28
-
29
- The `user` parameter exposes functionality to identify the user. If a user is authenticated, the based client will send an authorization token in the message. You can validate the token and decode it using the `user.token()`
30
- Will return `undefined` in case of an invalid token.
31
-
32
- Example:
33
- ```javascript
34
- export default async ({ based, user, payload, name, type, callstack}) => {
35
- // Leve this for now. Identifies a user as an UI user.
36
- if (user.isBased) return true
37
-
38
- const token = user.token('publicKeySecret')
39
- if (token.id) {
40
- return true
41
- }
42
-
43
- return false
44
- }
45
- ```
46
-
47
- ## How to setup?
48
- The `authorize` function can be created directly on the Functions section of the Based UI or deployed using the CLI with the [`based deploy`](https://github.com/atelier-saulx/based/blob/main/packages/cli/README.md#deploy) command.
49
- It should be set as a non-observable function with the following config:
50
-
51
- ```json
52
- {
53
- "name": "authorize",
54
- "observable": false
55
- }
56
- ```