@mspkapps/auth-client 0.1.6 → 0.1.11

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/AuthClient.js +77 -17
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mspkapps/auth-client",
3
- "version": "0.1.6",
3
+ "version": "0.1.11",
4
4
  "description": "Lightweight client for Your Auth Service",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/AuthClient.js CHANGED
@@ -13,10 +13,10 @@ export class AuthClient {
13
13
  baseUrl = 'https://cpanel.backend.mspkapps.in/api/v1',
14
14
  storage,
15
15
  fetch: fetchFn,
16
- keyInPath = true, // default: use headers, not key in URL
16
+ keyInPath = true,
17
17
  } = {}) {
18
18
  if (!apiKey) throw new Error('apiKey is required');
19
- if (!apiSecret) throw new Error('apiSecret is required'); // do not expose in browsers for prod
19
+ if (!apiSecret) throw new Error('apiSecret is required');
20
20
  this.apiKey = apiKey;
21
21
  this.apiSecret = apiSecret;
22
22
  this.baseUrl = baseUrl.replace(/\/$/, '');
@@ -24,7 +24,6 @@ export class AuthClient {
24
24
 
25
25
  const f = fetchFn || (typeof window !== 'undefined' ? window.fetch : (typeof fetch !== 'undefined' ? fetch : null));
26
26
  if (!f) throw new Error('No fetch available. Pass { fetch } or run on Node 18+/browsers.');
27
- // Bind to avoid “Illegal invocation”
28
27
  this.fetch = (...args) => f(...args);
29
28
 
30
29
  this.storage = storage ?? (typeof window !== 'undefined' ? window.localStorage : null);
@@ -92,6 +91,51 @@ export class AuthClient {
92
91
  return json;
93
92
  }
94
93
 
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
+ async googleAuth({ id_token, access_token }) {
115
+ if (!id_token && !access_token) {
116
+ throw new AuthError(
117
+ 'Either id_token or access_token is required for Google authentication',
118
+ 400,
119
+ 'MISSING_TOKEN',
120
+ null
121
+ );
122
+ }
123
+
124
+ const resp = await this.fetch(this._buildUrl('auth/google'), {
125
+ method: 'POST',
126
+ headers: this._headers(),
127
+ body: JSON.stringify({ id_token, access_token })
128
+ });
129
+
130
+ const json = await safeJson(resp);
131
+ if (!resp.ok || json?.success === false) throw toError(resp, json, 'Google authentication failed');
132
+
133
+ const token = json?.data?.user_token;
134
+ if (token) this.setToken(token);
135
+
136
+ return json;
137
+ }
138
+
95
139
  async requestPasswordReset({ email }) {
96
140
  const resp = await this.fetch(this._buildUrl('auth/request-password-reset'), {
97
141
  method: 'POST',
@@ -115,7 +159,6 @@ export class AuthClient {
115
159
  }
116
160
 
117
161
  async resendVerificationEmail({ email, purpose }) {
118
- // purpose: 'New Account' | 'Password change' | 'Profile Edit'
119
162
  const resp = await this.fetch(this._buildUrl('auth/resend-verification'), {
120
163
  method: 'POST',
121
164
  headers: this._headers(),
@@ -136,20 +179,37 @@ export class AuthClient {
136
179
  if (!resp.ok || json?.success === false) throw toError(resp, json, 'Delete account failed');
137
180
  return json;
138
181
  }
182
+
183
+ async sendGoogleUserSetPasswordEmail({ email }) {
184
+ const resp = await this.fetch(this._buildUrl('auth/set-password-google-user'), {
185
+ method: 'POST',
186
+ headers: this._headers(),
187
+ body: JSON.stringify({ email })
188
+ });
189
+ const json = await safeJson(resp);
190
+ if (!resp.ok || json?.success === false) throw toError(resp, json, 'Send Google user set password email failed');
191
+ return json;
192
+ }
193
+ /**
194
+ * Get current user profile (requires authentication)
195
+ * @returns {Promise<Object>} User profile data
196
+ */
197
+ async getProfile() {
198
+ const resp = await this.fetch(this._buildUrl('user/profile'), {
199
+ method: 'GET',
200
+ headers: this._headers()
201
+ });
202
+ const json = await safeJson(resp);
203
+ if (!resp.ok || json?.success === false) throw toError(resp, json, 'Get profile failed');
204
+ return json;
205
+ }
139
206
 
140
-
141
-
142
- // async getProfile() {
143
- // const resp = await this.fetch(this._buildUrl('user/profile'), {
144
- // method: 'GET',
145
- // headers: this._headers()
146
- // });
147
- // const json = await safeJson(resp);
148
- // if (!resp.ok || json?.success === false) throw toError(resp, json, 'Profile failed');
149
- // return json;
150
- // }
151
-
152
- // Generic authorized call for extra endpoints
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
+ */
153
213
  async authed(path, { method = 'GET', body, headers } = {}) {
154
214
  const resp = await this.fetch(this._buildUrl(path), {
155
215
  method,