glitch-javascript-sdk 2.6.7 → 2.6.8
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/dist/cjs/index.js +29 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +29 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/util/Session.d.ts +1 -0
- package/dist/esm/util/Storage.d.ts +3 -0
- package/dist/index.d.ts +4 -0
- package/package.json +1 -1
- package/src/util/Session.ts +21 -9
- package/src/util/Storage.ts +40 -15
|
@@ -16,5 +16,8 @@ declare class Storage {
|
|
|
16
16
|
static eraseCookie(name: string): void;
|
|
17
17
|
private static setCookie;
|
|
18
18
|
private static getCookie;
|
|
19
|
+
static setTokenExpiry(expiresInSeconds: number): void;
|
|
20
|
+
static getTokenExpiry(): number | null;
|
|
21
|
+
static isTokenExpired(): boolean;
|
|
19
22
|
}
|
|
20
23
|
export default Storage;
|
package/dist/index.d.ts
CHANGED
|
@@ -7417,6 +7417,7 @@ declare class Session {
|
|
|
7417
7417
|
static processAuthentication(data: {
|
|
7418
7418
|
token: {
|
|
7419
7419
|
access_token: string;
|
|
7420
|
+
expires_in: number;
|
|
7420
7421
|
};
|
|
7421
7422
|
id: string;
|
|
7422
7423
|
first_name: string;
|
|
@@ -7452,6 +7453,9 @@ declare class Storage {
|
|
|
7452
7453
|
static eraseCookie(name: string): void;
|
|
7453
7454
|
private static setCookie;
|
|
7454
7455
|
private static getCookie;
|
|
7456
|
+
static setTokenExpiry(expiresInSeconds: number): void;
|
|
7457
|
+
static getTokenExpiry(): number | null;
|
|
7458
|
+
static isTokenExpired(): boolean;
|
|
7455
7459
|
}
|
|
7456
7460
|
|
|
7457
7461
|
declare class Data {
|
package/package.json
CHANGED
package/src/util/Session.ts
CHANGED
|
@@ -21,7 +21,7 @@ class BrowserCrypto implements CryptoInterface {
|
|
|
21
21
|
|
|
22
22
|
createHmac(algorithm: string, secret: string): HmacInterface {
|
|
23
23
|
let data = '';
|
|
24
|
-
|
|
24
|
+
|
|
25
25
|
const hmac: HmacInterface = {
|
|
26
26
|
update: (updateData: string): HmacInterface => {
|
|
27
27
|
data = updateData;
|
|
@@ -34,7 +34,7 @@ class BrowserCrypto implements CryptoInterface {
|
|
|
34
34
|
return this.CryptoJS.HmacSHA256(data, secret).toString(this.CryptoJS.enc.Hex);
|
|
35
35
|
}
|
|
36
36
|
};
|
|
37
|
-
|
|
37
|
+
|
|
38
38
|
return hmac;
|
|
39
39
|
}
|
|
40
40
|
}
|
|
@@ -94,9 +94,17 @@ class Session {
|
|
|
94
94
|
|
|
95
95
|
public static isLoggedIn(): boolean {
|
|
96
96
|
const authToken = Storage.getAuthToken();
|
|
97
|
+
const expired = Storage.isTokenExpired();
|
|
98
|
+
|
|
99
|
+
if (expired) {
|
|
100
|
+
Session.end(); // Auto-clear if expired
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
|
|
97
104
|
return authToken !== null && authToken !== 'null' && authToken !== undefined;
|
|
98
105
|
}
|
|
99
106
|
|
|
107
|
+
|
|
100
108
|
public static getAuthToken(): string | null {
|
|
101
109
|
return Storage.getAuthToken();
|
|
102
110
|
}
|
|
@@ -124,6 +132,8 @@ class Session {
|
|
|
124
132
|
|
|
125
133
|
public static end(): void {
|
|
126
134
|
Storage.setAuthToken(null);
|
|
135
|
+
Storage.set('glitch_token_expiry', null); // Clear expiry
|
|
136
|
+
Storage.eraseCookie('glitch_token_expiry');
|
|
127
137
|
Storage.set(Session._id_key, null);
|
|
128
138
|
Storage.set(Session._first_name_key, null);
|
|
129
139
|
Storage.set(Session._last_name_key, null);
|
|
@@ -131,15 +141,17 @@ class Session {
|
|
|
131
141
|
Storage.set(Session._username_key, null);
|
|
132
142
|
}
|
|
133
143
|
|
|
134
|
-
public static processAuthentication(data: {
|
|
135
|
-
token: { access_token: string },
|
|
136
|
-
id: string,
|
|
137
|
-
first_name: string,
|
|
138
|
-
last_name: string,
|
|
139
|
-
email: string,
|
|
140
|
-
username: string
|
|
144
|
+
public static processAuthentication(data: {
|
|
145
|
+
token: { access_token: string, expires_in: number }, // Added expires_in
|
|
146
|
+
id: string,
|
|
147
|
+
first_name: string,
|
|
148
|
+
last_name: string,
|
|
149
|
+
email: string,
|
|
150
|
+
username: string
|
|
141
151
|
}): void {
|
|
142
152
|
Storage.setAuthToken(data.token.access_token);
|
|
153
|
+
Storage.setTokenExpiry(data.token.expires_in); // Save the timeout
|
|
154
|
+
|
|
143
155
|
Storage.set(Session._id_key, data.id);
|
|
144
156
|
Storage.set(Session._first_name_key, data.first_name);
|
|
145
157
|
Storage.set(Session._last_name_key, data.last_name);
|
package/src/util/Storage.ts
CHANGED
|
@@ -27,7 +27,7 @@ class Storage {
|
|
|
27
27
|
} catch (e) {
|
|
28
28
|
try {
|
|
29
29
|
this.setCookie(key, value, 31);
|
|
30
|
-
} catch(e){
|
|
30
|
+
} catch (e) {
|
|
31
31
|
|
|
32
32
|
}
|
|
33
33
|
Storage.data[key] = value;
|
|
@@ -52,10 +52,10 @@ class Storage {
|
|
|
52
52
|
|
|
53
53
|
try {
|
|
54
54
|
value = Storage.getCookie(key);
|
|
55
|
-
} catch(e) {
|
|
55
|
+
} catch (e) {
|
|
56
56
|
|
|
57
57
|
}
|
|
58
|
-
|
|
58
|
+
|
|
59
59
|
if (!value) {
|
|
60
60
|
value = Storage.data[key];
|
|
61
61
|
}
|
|
@@ -67,11 +67,11 @@ class Storage {
|
|
|
67
67
|
public static setAuthToken(token: string | null) {
|
|
68
68
|
// Always set the cookie if we have a root domain to ensure cross-subdomain sync
|
|
69
69
|
if (Storage.rootDomain) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
70
|
+
if (token) {
|
|
71
|
+
this.setCookie('glitch_auth_token', token, 31);
|
|
72
|
+
} else {
|
|
73
|
+
this.eraseCookie('glitch_auth_token');
|
|
74
|
+
}
|
|
75
75
|
}
|
|
76
76
|
// Still set localStorage for the current domain
|
|
77
77
|
Storage.set('glitch_auth_token', token);
|
|
@@ -80,22 +80,22 @@ class Storage {
|
|
|
80
80
|
public static getAuthToken(): string | null {
|
|
81
81
|
// 1. Try Cookie first (best for cross-subdomain)
|
|
82
82
|
let token = Storage.getCookie('glitch_auth_token');
|
|
83
|
-
|
|
83
|
+
|
|
84
84
|
// 2. Fallback to LocalStorage
|
|
85
85
|
if (!token || token === 'null') {
|
|
86
|
-
|
|
86
|
+
token = Storage.get('glitch_auth_token');
|
|
87
87
|
}
|
|
88
|
-
|
|
88
|
+
|
|
89
89
|
return (token === 'null' || !token) ? null : token;
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
public static eraseCookie(name: string) {
|
|
93
93
|
|
|
94
|
-
if(document){
|
|
94
|
+
if (document) {
|
|
95
95
|
document.cookie =
|
|
96
96
|
name +
|
|
97
97
|
'=; Secure; HttpOnly=false; SameSite=none; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
|
|
98
|
-
|
|
98
|
+
}
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
private static setCookie(name: string, value: string, days: number) {
|
|
@@ -115,12 +115,12 @@ class Storage {
|
|
|
115
115
|
expires +
|
|
116
116
|
'; path=/; domain=' +
|
|
117
117
|
Storage.rootDomain +
|
|
118
|
-
'; SameSite=Lax; Secure';
|
|
118
|
+
'; SameSite=Lax; Secure';
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
121
|
|
|
122
122
|
private static getCookie(name: string): string | null {
|
|
123
|
-
if(document){
|
|
123
|
+
if (document) {
|
|
124
124
|
const nameEQ = name + '=';
|
|
125
125
|
const ca = document.cookie.split(';');
|
|
126
126
|
for (let i = 0; i < ca.length; i++) {
|
|
@@ -131,6 +131,31 @@ class Storage {
|
|
|
131
131
|
}
|
|
132
132
|
return null;
|
|
133
133
|
}
|
|
134
|
+
|
|
135
|
+
public static setTokenExpiry(expiresInSeconds: number) {
|
|
136
|
+
const expiryTime = Date.now() + (expiresInSeconds * 1000);
|
|
137
|
+
Storage.set('glitch_token_expiry', expiryTime);
|
|
138
|
+
|
|
139
|
+
// Also set a cookie for cross-subdomain consistency if rootDomain exists
|
|
140
|
+
if (Storage.rootDomain && typeof document !== 'undefined') {
|
|
141
|
+
this.setCookie('glitch_token_expiry', expiryTime.toString(), 31);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
public static getTokenExpiry(): number | null {
|
|
146
|
+
let expiry = Storage.getCookie('glitch_token_expiry');
|
|
147
|
+
if (!expiry) {
|
|
148
|
+
expiry = Storage.get('glitch_token_expiry');
|
|
149
|
+
}
|
|
150
|
+
return expiry ? parseInt(expiry) : null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
public static isTokenExpired(): boolean {
|
|
154
|
+
const expiry = this.getTokenExpiry();
|
|
155
|
+
if (!expiry) return false; // If no expiry set, assume valid or let API handle 401
|
|
156
|
+
|
|
157
|
+
return Date.now() > expiry;
|
|
158
|
+
}
|
|
134
159
|
}
|
|
135
160
|
|
|
136
161
|
export default Storage;
|