@contentcredits/sdk 2.0.1 → 2.0.3
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/LICENSE +178 -0
- package/README.md +9 -1
- package/dist/auth/session.d.ts +14 -0
- package/dist/auth/storage.d.ts +14 -0
- package/dist/content-credits.cjs.js +162 -20
- package/dist/content-credits.cjs.js.map +1 -1
- package/dist/content-credits.esm.js +162 -20
- package/dist/content-credits.esm.js.map +1 -1
- package/dist/content-credits.umd.min.js +1 -1
- package/dist/content-credits.umd.min.js.map +1 -1
- package/package.json +2 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship made available under
|
|
36
|
+
the License, as indicated by a copyright notice that is included in
|
|
37
|
+
or attached to the work (an example is provided in the Appendix below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean, as submitted to the Licensor for inclusion
|
|
48
|
+
in the Work by the copyright owner or by an individual or Legal Entity
|
|
49
|
+
authorized to submit on behalf of the copyright owner. For the purposes
|
|
50
|
+
of this definition, "submitted" means any form of electronic, verbal,
|
|
51
|
+
or written communication sent to the Licensor or its representatives,
|
|
52
|
+
including but not limited to communication on electronic mailing lists,
|
|
53
|
+
source code control systems, and issue tracking systems that are managed
|
|
54
|
+
by, or on behalf of, the Licensor for the purpose of discussing and
|
|
55
|
+
improving the Work, but excluding communication that is conspicuously
|
|
56
|
+
marked or designated in writing by the copyright owner as "Not a
|
|
57
|
+
Contribution."
|
|
58
|
+
|
|
59
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
60
|
+
whom a Contribution has been received by the Licensor and included
|
|
61
|
+
within the Work.
|
|
62
|
+
|
|
63
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
64
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
65
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
66
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
67
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
68
|
+
Work and such Derivative Works in Source or Object form.
|
|
69
|
+
|
|
70
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
71
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
72
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
73
|
+
(except as stated in this section) patent license to make, have made,
|
|
74
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
75
|
+
where such license applies only to those patent claims licensable
|
|
76
|
+
by such Contributor that are necessarily infringed by their
|
|
77
|
+
Contribution(s) alone or by the combination of their Contribution(s)
|
|
78
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
79
|
+
institute patent litigation against any entity (including a cross-claim
|
|
80
|
+
or counterclaim in a lawsuit) alleging that the Work or any
|
|
81
|
+
Contribution embodied within the Work constitutes direct or contributory
|
|
82
|
+
patent infringement, then any patent licenses granted to You under
|
|
83
|
+
this License for that Work shall terminate as of the date such
|
|
84
|
+
litigation is filed.
|
|
85
|
+
|
|
86
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
87
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
88
|
+
modifications, and in Source or Object form, provided that You
|
|
89
|
+
meet the following conditions:
|
|
90
|
+
|
|
91
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
92
|
+
Works a copy of this License; and
|
|
93
|
+
|
|
94
|
+
(b) You must cause any modified files to carry prominent notices
|
|
95
|
+
stating that You changed the files; and
|
|
96
|
+
|
|
97
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
98
|
+
that You distribute, all copyright, patent, trademark, and
|
|
99
|
+
attribution notices from the Source form of the Work,
|
|
100
|
+
excluding those notices that do not pertain to any part of
|
|
101
|
+
the Derivative Works; and
|
|
102
|
+
|
|
103
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
104
|
+
distribution, You must include a readable copy of the
|
|
105
|
+
attribution notices contained within such NOTICE file, in
|
|
106
|
+
at least one of the following places: within a NOTICE text
|
|
107
|
+
file distributed as part of the Derivative Works; within
|
|
108
|
+
the Source form or documentation, if provided along with the
|
|
109
|
+
Derivative Works; or, within a display generated by the
|
|
110
|
+
Derivative Works, if and wherever such third-party notices
|
|
111
|
+
normally appear. The contents of the NOTICE file are for
|
|
112
|
+
informational purposes only and do not modify the License.
|
|
113
|
+
You may add Your own attribution notices within Derivative
|
|
114
|
+
Works that You distribute, alongside or in addition to the
|
|
115
|
+
NOTICE text from the Work, provided that such additional
|
|
116
|
+
attribution notices cannot be construed as modifying the License.
|
|
117
|
+
|
|
118
|
+
You may add Your own license statement for Your modifications and
|
|
119
|
+
may provide additional grant of rights to use, copy, modify, and
|
|
120
|
+
distribute those modifications.
|
|
121
|
+
|
|
122
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
123
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
124
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
125
|
+
this License, without any additional terms or conditions.
|
|
126
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
127
|
+
the terms of any separate license agreement you may have executed
|
|
128
|
+
with Licensor regarding such Contributions.
|
|
129
|
+
|
|
130
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
131
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
132
|
+
except as required for reasonable and customary use in describing the
|
|
133
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
134
|
+
|
|
135
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
136
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
137
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
138
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
139
|
+
implied, including, without limitation, any warranties or conditions
|
|
140
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
141
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
142
|
+
appropriateness of using or reproducing the Work and assume any
|
|
143
|
+
risks associated with Your exercise of permissions under this License.
|
|
144
|
+
|
|
145
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
146
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
147
|
+
unless required by applicable law (such as deliberate and grossly
|
|
148
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
149
|
+
liable to You for damages, including any direct, indirect, special,
|
|
150
|
+
incidental, or exemplary damages of any character arising as a
|
|
151
|
+
result of this License or out of the use or inability to use the
|
|
152
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
153
|
+
work stoppage, computer failure or malfunction, or all other
|
|
154
|
+
commercial damages or losses), even if such Contributor has been
|
|
155
|
+
advised of the possibility of such damages.
|
|
156
|
+
|
|
157
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
158
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
159
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
160
|
+
or other liability obligations and/or rights consistent with this
|
|
161
|
+
License. However, in accepting such obligations, You may offer only
|
|
162
|
+
conditions consistent with this License.
|
|
163
|
+
|
|
164
|
+
END OF TERMS AND CONDITIONS
|
|
165
|
+
|
|
166
|
+
Copyright 2025 Content Credits
|
|
167
|
+
|
|
168
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
169
|
+
you may not use this file except in compliance with the License.
|
|
170
|
+
You may obtain a copy of the License at
|
|
171
|
+
|
|
172
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
173
|
+
|
|
174
|
+
Unless required by applicable law or agreed to in writing, software
|
|
175
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
176
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
177
|
+
implied. See the License for the specific language governing
|
|
178
|
+
permissions and limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @contentcredits/sdk
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@contentcredits/sdk)
|
|
4
|
-
[](https://github.com/contentcredits/sdk/blob/main/LICENSE)
|
|
5
5
|
[](https://www.typescriptlang.org/)
|
|
6
6
|
|
|
7
7
|
Drop-in paywall and threaded comment system for any website. Add credit-based article monetisation in under 5 minutes — no backend changes required.
|
|
@@ -171,6 +171,14 @@ export function PremiumGate({ apiKey, children }) {
|
|
|
171
171
|
|
|
172
172
|
---
|
|
173
173
|
|
|
174
|
+
## Examples
|
|
175
|
+
|
|
176
|
+
| Example | Description |
|
|
177
|
+
|---------|-------------|
|
|
178
|
+
| [`examples/nextjs-blog`](./examples/nextjs-blog) | Next.js 14 (App Router) blog with free and premium articles — the fastest way to see the SDK in a real project |
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
174
182
|
## Links
|
|
175
183
|
|
|
176
184
|
- [Full documentation](https://docs.contentcredits.com)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attempt a silent token refresh using the stored refresh token.
|
|
3
|
+
*
|
|
4
|
+
* Uses a raw fetch (not the SDK API client) to avoid:
|
|
5
|
+
* 1. Circular dependency: client → session → client
|
|
6
|
+
* 2. Triggering the client's own 401 → tryRefreshSession loop
|
|
7
|
+
*
|
|
8
|
+
* Returns true if a new access token was obtained and stored.
|
|
9
|
+
*
|
|
10
|
+
* On network error the refresh token is NOT cleared — it may still be
|
|
11
|
+
* valid once connectivity returns. On 401/403 the token is cleared
|
|
12
|
+
* because the server has explicitly rejected it (expired / revoked).
|
|
13
|
+
*/
|
|
14
|
+
export declare function tryRefreshSession(apiBaseUrl: string): Promise<boolean>;
|
package/dist/auth/storage.d.ts
CHANGED
|
@@ -4,3 +4,17 @@ export declare const tokenStorage: {
|
|
|
4
4
|
clear(): void;
|
|
5
5
|
has(): boolean;
|
|
6
6
|
};
|
|
7
|
+
/**
|
|
8
|
+
* Refresh token storage — localStorage only.
|
|
9
|
+
*
|
|
10
|
+
* Stored on the publisher's domain (first-party storage) so it is never
|
|
11
|
+
* subject to third-party cookie / storage blocking in any browser.
|
|
12
|
+
* Refresh tokens are opaque strings issued by the backend and rotated
|
|
13
|
+
* on every use.
|
|
14
|
+
*/
|
|
15
|
+
export declare const refreshTokenStorage: {
|
|
16
|
+
set(token: string): void;
|
|
17
|
+
get(): string | null;
|
|
18
|
+
clear(): void;
|
|
19
|
+
has(): boolean;
|
|
20
|
+
};
|
|
@@ -164,15 +164,22 @@ function getUserIdFromToken(token) {
|
|
|
164
164
|
}
|
|
165
165
|
|
|
166
166
|
const SESSION_KEY = 'cc_sdk_token';
|
|
167
|
+
const REFRESH_KEY = 'cc_rt';
|
|
167
168
|
/**
|
|
168
|
-
*
|
|
169
|
-
* 1. In-memory (primary) — invisible to other scripts, survives page navigations
|
|
170
|
-
* within the same JS context but gone on hard reload.
|
|
171
|
-
* 2. sessionStorage (secondary) — survives soft reloads, cleared when the tab
|
|
172
|
-
* closes, never shared across tabs.
|
|
169
|
+
* Three-layer auth storage:
|
|
173
170
|
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
171
|
+
* ACCESS TOKEN (short-lived JWT, ~15 min)
|
|
172
|
+
* ┌─ Layer 1: in-memory — invisible to other scripts; cleared on page close
|
|
173
|
+
* └─ Layer 2: sessionStorage — survives soft reloads; cleared when tab closes
|
|
174
|
+
*
|
|
175
|
+
* REFRESH TOKEN (long-lived opaque token, ~30 days)
|
|
176
|
+
* └─ Layer 3: localStorage — survives browser close; used to silently
|
|
177
|
+
* re-authenticate on the next visit without showing a popup
|
|
178
|
+
*
|
|
179
|
+
* We intentionally never write to document.cookie (no HttpOnly = XSS risk).
|
|
180
|
+
* The refresh token in localStorage is the accepted industry trade-off:
|
|
181
|
+
* it persists across sessions at the cost of XSS accessibility, mitigated
|
|
182
|
+
* by short-lived access tokens and server-side refresh token rotation.
|
|
176
183
|
*/
|
|
177
184
|
let memoryToken = null;
|
|
178
185
|
const tokenStorage = {
|
|
@@ -224,6 +231,95 @@ const tokenStorage = {
|
|
|
224
231
|
return this.get() !== null;
|
|
225
232
|
},
|
|
226
233
|
};
|
|
234
|
+
/**
|
|
235
|
+
* Refresh token storage — localStorage only.
|
|
236
|
+
*
|
|
237
|
+
* Stored on the publisher's domain (first-party storage) so it is never
|
|
238
|
+
* subject to third-party cookie / storage blocking in any browser.
|
|
239
|
+
* Refresh tokens are opaque strings issued by the backend and rotated
|
|
240
|
+
* on every use.
|
|
241
|
+
*/
|
|
242
|
+
const refreshTokenStorage = {
|
|
243
|
+
set(token) {
|
|
244
|
+
try {
|
|
245
|
+
localStorage.setItem(REFRESH_KEY, token);
|
|
246
|
+
}
|
|
247
|
+
catch (_a) {
|
|
248
|
+
// localStorage unavailable (private mode with strict settings) — degrade
|
|
249
|
+
// to session-only auth gracefully
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
get() {
|
|
253
|
+
try {
|
|
254
|
+
return localStorage.getItem(REFRESH_KEY);
|
|
255
|
+
}
|
|
256
|
+
catch (_a) {
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
},
|
|
260
|
+
clear() {
|
|
261
|
+
try {
|
|
262
|
+
localStorage.removeItem(REFRESH_KEY);
|
|
263
|
+
}
|
|
264
|
+
catch (_a) {
|
|
265
|
+
// ignore
|
|
266
|
+
}
|
|
267
|
+
},
|
|
268
|
+
has() {
|
|
269
|
+
return this.get() !== null;
|
|
270
|
+
},
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Attempt a silent token refresh using the stored refresh token.
|
|
275
|
+
*
|
|
276
|
+
* Uses a raw fetch (not the SDK API client) to avoid:
|
|
277
|
+
* 1. Circular dependency: client → session → client
|
|
278
|
+
* 2. Triggering the client's own 401 → tryRefreshSession loop
|
|
279
|
+
*
|
|
280
|
+
* Returns true if a new access token was obtained and stored.
|
|
281
|
+
*
|
|
282
|
+
* On network error the refresh token is NOT cleared — it may still be
|
|
283
|
+
* valid once connectivity returns. On 401/403 the token is cleared
|
|
284
|
+
* because the server has explicitly rejected it (expired / revoked).
|
|
285
|
+
*/
|
|
286
|
+
async function tryRefreshSession(apiBaseUrl) {
|
|
287
|
+
const rt = refreshTokenStorage.get();
|
|
288
|
+
if (!rt)
|
|
289
|
+
return false;
|
|
290
|
+
const controller = new AbortController();
|
|
291
|
+
const timeoutId = setTimeout(() => controller.abort(), 8000);
|
|
292
|
+
try {
|
|
293
|
+
const resp = await fetch(`${apiBaseUrl}/auth/refresh`, {
|
|
294
|
+
method: 'POST',
|
|
295
|
+
headers: { 'Content-Type': 'application/json' },
|
|
296
|
+
body: JSON.stringify({ refreshToken: rt }),
|
|
297
|
+
credentials: 'omit',
|
|
298
|
+
signal: controller.signal,
|
|
299
|
+
});
|
|
300
|
+
clearTimeout(timeoutId);
|
|
301
|
+
if (!resp.ok) {
|
|
302
|
+
// Server rejected the token — it has expired or been revoked.
|
|
303
|
+
// Clear it so we don't retry on every page load.
|
|
304
|
+
refreshTokenStorage.clear();
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
const data = (await resp.json());
|
|
308
|
+
if (!(data === null || data === void 0 ? void 0 : data.accessToken) || !(data === null || data === void 0 ? void 0 : data.refreshToken)) {
|
|
309
|
+
refreshTokenStorage.clear();
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
// Store new access token and rotate the refresh token
|
|
313
|
+
tokenStorage.set(data.accessToken);
|
|
314
|
+
refreshTokenStorage.set(data.refreshToken);
|
|
315
|
+
return true;
|
|
316
|
+
}
|
|
317
|
+
catch (_a) {
|
|
318
|
+
// Network error or timeout — don't clear the refresh token
|
|
319
|
+
clearTimeout(timeoutId);
|
|
320
|
+
return false;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
227
323
|
|
|
228
324
|
const REQUEST_TIMEOUT_MS = 12000;
|
|
229
325
|
const MAX_RETRIES = 3;
|
|
@@ -240,7 +336,8 @@ function shouldRetry(status) {
|
|
|
240
336
|
return status >= 500 || status === 429;
|
|
241
337
|
}
|
|
242
338
|
function createApiClient(baseUrl, emitter) {
|
|
243
|
-
async function request(method, path, body, attempt = 0
|
|
339
|
+
async function request(method, path, body, attempt = 0, authRetried = false // true after one silent-refresh attempt, prevents loops
|
|
340
|
+
) {
|
|
244
341
|
const url = `${baseUrl}${path}`;
|
|
245
342
|
const serialisedBody = body ? JSON.stringify(body) : undefined;
|
|
246
343
|
const key = requestKey(method, url, serialisedBody);
|
|
@@ -268,7 +365,18 @@ function createApiClient(baseUrl, emitter) {
|
|
|
268
365
|
var _a;
|
|
269
366
|
clearTimeout(timeoutId);
|
|
270
367
|
if (response.status === 401) {
|
|
368
|
+
// Try a silent refresh once before giving up.
|
|
369
|
+
// authRetried guard prevents an infinite loop if the refresh
|
|
370
|
+
// endpoint itself returns 401.
|
|
371
|
+
if (!authRetried) {
|
|
372
|
+
const refreshed = await tryRefreshSession(baseUrl);
|
|
373
|
+
if (refreshed) {
|
|
374
|
+
inFlight.delete(key);
|
|
375
|
+
return request(method, path, body, attempt, true);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
271
378
|
tokenStorage.clear();
|
|
379
|
+
refreshTokenStorage.clear();
|
|
272
380
|
emitter.emit('auth:logout', {});
|
|
273
381
|
throw new ApiError(401, 'Unauthorized — session expired');
|
|
274
382
|
}
|
|
@@ -494,16 +602,29 @@ function getPaywallStyles(primaryColor, fontFamily) {
|
|
|
494
602
|
pointer-events: none;
|
|
495
603
|
}
|
|
496
604
|
|
|
605
|
+
/* Full-viewport backdrop — centers the overlay and blocks background clicks */
|
|
606
|
+
.cc-paywall-backdrop {
|
|
607
|
+
position: fixed;
|
|
608
|
+
inset: 0;
|
|
609
|
+
background: rgba(0, 0, 0, 0.45);
|
|
610
|
+
display: flex;
|
|
611
|
+
align-items: center;
|
|
612
|
+
justify-content: center;
|
|
613
|
+
padding: 20px;
|
|
614
|
+
pointer-events: all;
|
|
615
|
+
}
|
|
616
|
+
|
|
497
617
|
.cc-paywall-overlay {
|
|
498
618
|
background: #fff;
|
|
499
619
|
border: 1px solid #e5e7eb;
|
|
500
620
|
border-radius: 12px;
|
|
501
621
|
padding: 32px 24px;
|
|
622
|
+
width: 100%;
|
|
502
623
|
max-width: 480px;
|
|
503
|
-
margin: 24px auto;
|
|
504
624
|
text-align: center;
|
|
505
625
|
font-family: ${fontFamily};
|
|
506
|
-
box-shadow: 0
|
|
626
|
+
box-shadow: 0 8px 40px rgba(0,0,0,0.18);
|
|
627
|
+
pointer-events: all;
|
|
507
628
|
}
|
|
508
629
|
|
|
509
630
|
.cc-paywall-overlay h2 {
|
|
@@ -1049,9 +1170,14 @@ function createPaywallRenderer(config) {
|
|
|
1049
1170
|
const { root: shadowRoot } = createShadowHost(HOST_ID);
|
|
1050
1171
|
root = shadowRoot;
|
|
1051
1172
|
injectStyles(root, getPaywallStyles(config.theme.primaryColor, config.theme.fontFamily));
|
|
1173
|
+
// Backdrop: fixed full-viewport flex container — centers the overlay and
|
|
1174
|
+
// blocks interaction with the page behind it (pointer-events: all in CSS).
|
|
1175
|
+
const backdrop = el('div');
|
|
1176
|
+
backdrop.className = 'cc-paywall-backdrop';
|
|
1177
|
+
root.appendChild(backdrop);
|
|
1052
1178
|
overlay = el('div');
|
|
1053
1179
|
overlay.className = 'cc-paywall-overlay';
|
|
1054
|
-
|
|
1180
|
+
backdrop.appendChild(overlay);
|
|
1055
1181
|
}
|
|
1056
1182
|
function render(state, callbacks, meta) {
|
|
1057
1183
|
var _a, _b, _c;
|
|
@@ -1323,7 +1449,7 @@ function scrubTokenFromUrl() {
|
|
|
1323
1449
|
try {
|
|
1324
1450
|
const url = new URL(window.location.href);
|
|
1325
1451
|
let changed = false;
|
|
1326
|
-
['token', 'cc_token'].forEach(param => {
|
|
1452
|
+
['token', 'cc_token', 'refresh_token', 'cc_refresh_token'].forEach(param => {
|
|
1327
1453
|
if (url.searchParams.has(param)) {
|
|
1328
1454
|
url.searchParams.delete(param);
|
|
1329
1455
|
changed = true;
|
|
@@ -1343,17 +1469,22 @@ function scrubTokenFromUrl() {
|
|
|
1343
1469
|
* Always scrubs the token from the URL after reading it.
|
|
1344
1470
|
*/
|
|
1345
1471
|
function consumeTokenFromUrl() {
|
|
1346
|
-
var _a;
|
|
1472
|
+
var _a, _b;
|
|
1347
1473
|
try {
|
|
1348
1474
|
const url = new URL(window.location.href);
|
|
1349
1475
|
const token = (_a = url.searchParams.get('token')) !== null && _a !== void 0 ? _a : url.searchParams.get('cc_token');
|
|
1476
|
+
const refreshToken = (_b = url.searchParams.get('refresh_token')) !== null && _b !== void 0 ? _b : url.searchParams.get('cc_refresh_token');
|
|
1477
|
+
// Scrub ALL auth params before doing anything else
|
|
1350
1478
|
scrubTokenFromUrl();
|
|
1479
|
+
if (refreshToken) {
|
|
1480
|
+
refreshTokenStorage.set(refreshToken);
|
|
1481
|
+
}
|
|
1351
1482
|
if (token) {
|
|
1352
1483
|
tokenStorage.set(token);
|
|
1353
1484
|
return token;
|
|
1354
1485
|
}
|
|
1355
1486
|
}
|
|
1356
|
-
catch (
|
|
1487
|
+
catch (_c) {
|
|
1357
1488
|
// ignore
|
|
1358
1489
|
}
|
|
1359
1490
|
return null;
|
|
@@ -1381,7 +1512,7 @@ function openAuthPopup(authUrl) {
|
|
|
1381
1512
|
const MAX_WAIT_MS = 5 * 60 * 1000; // 5 minutes
|
|
1382
1513
|
let elapsed = 0;
|
|
1383
1514
|
const timer = setInterval(() => {
|
|
1384
|
-
var _a;
|
|
1515
|
+
var _a, _b;
|
|
1385
1516
|
elapsed += POLL_MS;
|
|
1386
1517
|
if (!popup || popup.closed) {
|
|
1387
1518
|
clearInterval(timer);
|
|
@@ -1393,7 +1524,7 @@ function openAuthPopup(authUrl) {
|
|
|
1393
1524
|
try {
|
|
1394
1525
|
popup.close();
|
|
1395
1526
|
}
|
|
1396
|
-
catch ( /* ignore */
|
|
1527
|
+
catch ( /* ignore */_c) { /* ignore */ }
|
|
1397
1528
|
resolve(null);
|
|
1398
1529
|
return;
|
|
1399
1530
|
}
|
|
@@ -1403,18 +1534,22 @@ function openAuthPopup(authUrl) {
|
|
|
1403
1534
|
if (popupUrl.includes('/auth/callback') || popupUrl.includes('cc_token=') || popupUrl.includes('token=')) {
|
|
1404
1535
|
const params = new URLSearchParams(popup.location.search);
|
|
1405
1536
|
const token = (_a = params.get('token')) !== null && _a !== void 0 ? _a : params.get('cc_token');
|
|
1537
|
+
const refreshToken = (_b = params.get('refresh_token')) !== null && _b !== void 0 ? _b : params.get('cc_refresh_token');
|
|
1406
1538
|
if (token) {
|
|
1407
1539
|
tokenStorage.set(token);
|
|
1540
|
+
if (refreshToken) {
|
|
1541
|
+
refreshTokenStorage.set(refreshToken);
|
|
1542
|
+
}
|
|
1408
1543
|
clearInterval(timer);
|
|
1409
1544
|
try {
|
|
1410
1545
|
popup.close();
|
|
1411
1546
|
}
|
|
1412
|
-
catch ( /* ignore */
|
|
1547
|
+
catch ( /* ignore */_d) { /* ignore */ }
|
|
1413
1548
|
resolve(token);
|
|
1414
1549
|
}
|
|
1415
1550
|
}
|
|
1416
1551
|
}
|
|
1417
|
-
catch (
|
|
1552
|
+
catch (_e) {
|
|
1418
1553
|
// cross-origin error — popup is on accounts domain, not ours yet; keep polling
|
|
1419
1554
|
}
|
|
1420
1555
|
}, POLL_MS);
|
|
@@ -2485,8 +2620,15 @@ class ContentCredits {
|
|
|
2485
2620
|
}
|
|
2486
2621
|
// ── Internal start ────────────────────────────────────────────────────────
|
|
2487
2622
|
async _start() {
|
|
2488
|
-
//
|
|
2623
|
+
// 1. Consume any token that arrived in the URL (mobile redirect flow)
|
|
2489
2624
|
consumeTokenFromUrl();
|
|
2625
|
+
// 2. If no access token in memory/session, attempt a silent refresh.
|
|
2626
|
+
// This runs on every new browser session (after the browser was closed)
|
|
2627
|
+
// and silently re-authenticates the user using their stored refresh token
|
|
2628
|
+
// — no popup, no visible delay, no paywall flash.
|
|
2629
|
+
if (!tokenStorage.has()) {
|
|
2630
|
+
await tryRefreshSession(this.config.apiBaseUrl);
|
|
2631
|
+
}
|
|
2490
2632
|
this.paywallModule = createPaywall(this.config, this.creditsApi, this.state, this.emitter);
|
|
2491
2633
|
if (this.config.enableComments) {
|
|
2492
2634
|
this.commentsModule = createComments(this.config, this.commentsApi, this.emitter);
|
|
@@ -2537,7 +2679,7 @@ class ContentCredits {
|
|
|
2537
2679
|
}
|
|
2538
2680
|
/** SDK version string. */
|
|
2539
2681
|
static get version() {
|
|
2540
|
-
return "2.0.
|
|
2682
|
+
return "2.0.3";
|
|
2541
2683
|
}
|
|
2542
2684
|
}
|
|
2543
2685
|
// ── Auto-init from script data attributes (CDN usage) ────────────────────────
|