@mspkapps/auth-client 0.1.18 → 0.1.19
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/package.json +1 -1
- package/src/AuthClient.js +9 -35
package/package.json
CHANGED
package/src/AuthClient.js
CHANGED
|
@@ -14,6 +14,7 @@ export class AuthClient {
|
|
|
14
14
|
storage,
|
|
15
15
|
fetch: fetchFn,
|
|
16
16
|
keyInPath = true,
|
|
17
|
+
googleClientId = null, // new option
|
|
17
18
|
} = {}) {
|
|
18
19
|
if (!apiKey) throw new Error('apiKey is required');
|
|
19
20
|
if (!apiSecret) throw new Error('apiSecret is required');
|
|
@@ -21,6 +22,7 @@ export class AuthClient {
|
|
|
21
22
|
this.apiSecret = apiSecret;
|
|
22
23
|
this.baseUrl = baseUrl.replace(/\/$/, '');
|
|
23
24
|
this.keyInPath = !!keyInPath;
|
|
25
|
+
this.googleClientId = googleClientId || null; // store it
|
|
24
26
|
|
|
25
27
|
const f = fetchFn || (typeof window !== 'undefined' ? window.fetch : (typeof fetch !== 'undefined' ? fetch : null));
|
|
26
28
|
if (!f) throw new Error('No fetch available. Pass { fetch } or run on Node 18+/browsers.');
|
|
@@ -49,6 +51,7 @@ export class AuthClient {
|
|
|
49
51
|
'Content-Type': 'application/json',
|
|
50
52
|
'X-API-Key': this.apiKey,
|
|
51
53
|
'X-API-Secret': this.apiSecret,
|
|
54
|
+
...(this.googleClientId ? { 'X-Google-Client-Id': this.googleClientId } : {}),
|
|
52
55
|
...(this.token ? { Authorization: `UserToken ${this.token}` } : {}),
|
|
53
56
|
...extra
|
|
54
57
|
};
|
|
@@ -91,26 +94,6 @@ export class AuthClient {
|
|
|
91
94
|
return json;
|
|
92
95
|
}
|
|
93
96
|
|
|
94
|
-
/**
|
|
95
|
-
* Google Sign-In authentication
|
|
96
|
-
* @param {Object} params - Google authentication parameters
|
|
97
|
-
* @param {string} [params.id_token] - Google ID token from credential response
|
|
98
|
-
* @param {string} [params.access_token] - Google access token (alternative to id_token)
|
|
99
|
-
* @returns {Promise<Object>} Authentication response with user data and token
|
|
100
|
-
* @example
|
|
101
|
-
* // With Google Sign-In button (React)
|
|
102
|
-
* import { GoogleLogin } from '@react-oauth/google';
|
|
103
|
-
*
|
|
104
|
-
* const handleSuccess = async (credentialResponse) => {
|
|
105
|
-
* const result = await auth.googleAuth({
|
|
106
|
-
* id_token: credentialResponse.credential
|
|
107
|
-
* });
|
|
108
|
-
* console.log('User:', result.data.user);
|
|
109
|
-
* console.log('Is new user:', result.data.is_new_user);
|
|
110
|
-
* };
|
|
111
|
-
*
|
|
112
|
-
* <GoogleLogin onSuccess={handleSuccess} />
|
|
113
|
-
*/
|
|
114
97
|
async googleAuth({ id_token, access_token }) {
|
|
115
98
|
if (!id_token && !access_token) {
|
|
116
99
|
throw new AuthError(
|
|
@@ -121,10 +104,14 @@ export class AuthClient {
|
|
|
121
104
|
);
|
|
122
105
|
}
|
|
123
106
|
|
|
107
|
+
// include googleClientId in body too (helpful if backend needs it)
|
|
108
|
+
const body = { id_token, access_token };
|
|
109
|
+
if (this.googleClientId) body.google_client_id = this.googleClientId;
|
|
110
|
+
|
|
124
111
|
const resp = await this.fetch(this._buildUrl('auth/google'), {
|
|
125
112
|
method: 'POST',
|
|
126
113
|
headers: this._headers(),
|
|
127
|
-
body: JSON.stringify(
|
|
114
|
+
body: JSON.stringify(body)
|
|
128
115
|
});
|
|
129
116
|
|
|
130
117
|
const json = await safeJson(resp);
|
|
@@ -181,19 +168,16 @@ export class AuthClient {
|
|
|
181
168
|
}
|
|
182
169
|
|
|
183
170
|
async getEditableProfileFields() {
|
|
184
|
-
// either call profile (which contains editable flags) or a dedicated endpoint
|
|
185
171
|
const resp = await this.fetch(this._buildUrl('user/profile'), {
|
|
186
172
|
method: 'GET',
|
|
187
173
|
headers: this._headers()
|
|
188
174
|
});
|
|
189
175
|
const json = await safeJson(resp);
|
|
190
176
|
if (!resp.ok || json?.success === false) throw toError(resp, json, 'Get profile failed');
|
|
191
|
-
// return both profile and editable metadata
|
|
192
177
|
return json;
|
|
193
178
|
}
|
|
194
179
|
|
|
195
180
|
async updateProfile(updates = {}) {
|
|
196
|
-
// updates can contain { name, username, email, extra: { fieldName: value } }
|
|
197
181
|
const resp = await this.fetch(this._buildUrl('user/profile'), {
|
|
198
182
|
method: 'PATCH',
|
|
199
183
|
headers: this._headers(),
|
|
@@ -201,7 +185,6 @@ export class AuthClient {
|
|
|
201
185
|
});
|
|
202
186
|
const json = await safeJson(resp);
|
|
203
187
|
if (!resp.ok || json?.success === false) throw toError(resp, json, 'Update profile failed');
|
|
204
|
-
// If server indicates verification required, return that info to UI
|
|
205
188
|
return json;
|
|
206
189
|
}
|
|
207
190
|
|
|
@@ -215,10 +198,7 @@ export class AuthClient {
|
|
|
215
198
|
if (!resp.ok || json?.success === false) throw toError(resp, json, 'Send Google user set password email failed');
|
|
216
199
|
return json;
|
|
217
200
|
}
|
|
218
|
-
|
|
219
|
-
* Get current user profile (requires authentication)
|
|
220
|
-
* @returns {Promise<Object>} User profile data
|
|
221
|
-
*/
|
|
201
|
+
|
|
222
202
|
async getProfile() {
|
|
223
203
|
const resp = await this.fetch(this._buildUrl('user/profile'), {
|
|
224
204
|
method: 'GET',
|
|
@@ -229,12 +209,6 @@ export class AuthClient {
|
|
|
229
209
|
return json;
|
|
230
210
|
}
|
|
231
211
|
|
|
232
|
-
/**
|
|
233
|
-
* Generic authorized call for custom endpoints
|
|
234
|
-
* @param {string} path - API endpoint path
|
|
235
|
-
* @param {Object} options - Request options
|
|
236
|
-
* @returns {Promise<Object>} Response data
|
|
237
|
-
*/
|
|
238
212
|
async authed(path, { method = 'GET', body, headers } = {}) {
|
|
239
213
|
const resp = await this.fetch(this._buildUrl(path), {
|
|
240
214
|
method,
|