@mspkapps/auth-client 0.1.17 → 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 +33 -34
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,18 +104,22 @@ 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);
|
|
131
118
|
if (!resp.ok || json?.success === false) throw toError(resp, json, 'Google authentication failed');
|
|
132
|
-
|
|
119
|
+
|
|
133
120
|
const token = json?.data?.user_token;
|
|
134
121
|
if (token) this.setToken(token);
|
|
135
|
-
|
|
122
|
+
|
|
136
123
|
return json;
|
|
137
124
|
}
|
|
138
125
|
|
|
@@ -179,7 +166,28 @@ export class AuthClient {
|
|
|
179
166
|
if (!resp.ok || json?.success === false) throw toError(resp, json, 'Delete account failed');
|
|
180
167
|
return json;
|
|
181
168
|
}
|
|
182
|
-
|
|
169
|
+
|
|
170
|
+
async getEditableProfileFields() {
|
|
171
|
+
const resp = await this.fetch(this._buildUrl('user/profile'), {
|
|
172
|
+
method: 'GET',
|
|
173
|
+
headers: this._headers()
|
|
174
|
+
});
|
|
175
|
+
const json = await safeJson(resp);
|
|
176
|
+
if (!resp.ok || json?.success === false) throw toError(resp, json, 'Get profile failed');
|
|
177
|
+
return json;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async updateProfile(updates = {}) {
|
|
181
|
+
const resp = await this.fetch(this._buildUrl('user/profile'), {
|
|
182
|
+
method: 'PATCH',
|
|
183
|
+
headers: this._headers(),
|
|
184
|
+
body: JSON.stringify(updates)
|
|
185
|
+
});
|
|
186
|
+
const json = await safeJson(resp);
|
|
187
|
+
if (!resp.ok || json?.success === false) throw toError(resp, json, 'Update profile failed');
|
|
188
|
+
return json;
|
|
189
|
+
}
|
|
190
|
+
|
|
183
191
|
async sendGoogleUserSetPasswordEmail({ email }) {
|
|
184
192
|
const resp = await this.fetch(this._buildUrl('auth/set-password-google-user'), {
|
|
185
193
|
method: 'POST',
|
|
@@ -190,10 +198,7 @@ export class AuthClient {
|
|
|
190
198
|
if (!resp.ok || json?.success === false) throw toError(resp, json, 'Send Google user set password email failed');
|
|
191
199
|
return json;
|
|
192
200
|
}
|
|
193
|
-
|
|
194
|
-
* Get current user profile (requires authentication)
|
|
195
|
-
* @returns {Promise<Object>} User profile data
|
|
196
|
-
*/
|
|
201
|
+
|
|
197
202
|
async getProfile() {
|
|
198
203
|
const resp = await this.fetch(this._buildUrl('user/profile'), {
|
|
199
204
|
method: 'GET',
|
|
@@ -204,12 +209,6 @@ export class AuthClient {
|
|
|
204
209
|
return json;
|
|
205
210
|
}
|
|
206
211
|
|
|
207
|
-
/**
|
|
208
|
-
* Generic authorized call for custom endpoints
|
|
209
|
-
* @param {string} path - API endpoint path
|
|
210
|
-
* @param {Object} options - Request options
|
|
211
|
-
* @returns {Promise<Object>} Response data
|
|
212
|
-
*/
|
|
213
212
|
async authed(path, { method = 'GET', body, headers } = {}) {
|
|
214
213
|
const resp = await this.fetch(this._buildUrl(path), {
|
|
215
214
|
method,
|