@mspkapps/auth-client 0.1.6 → 0.1.9
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 +66 -16
package/package.json
CHANGED
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,
|
|
16
|
+
keyInPath = true,
|
|
17
17
|
} = {}) {
|
|
18
18
|
if (!apiKey) throw new Error('apiKey is required');
|
|
19
|
-
if (!apiSecret) throw new Error('apiSecret is required');
|
|
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(),
|
|
@@ -137,19 +180,26 @@ export class AuthClient {
|
|
|
137
180
|
return json;
|
|
138
181
|
}
|
|
139
182
|
|
|
183
|
+
/**
|
|
184
|
+
* Get current user profile (requires authentication)
|
|
185
|
+
* @returns {Promise<Object>} User profile data
|
|
186
|
+
*/
|
|
187
|
+
async getProfile() {
|
|
188
|
+
const resp = await this.fetch(this._buildUrl('user/profile'), {
|
|
189
|
+
method: 'GET',
|
|
190
|
+
headers: this._headers()
|
|
191
|
+
});
|
|
192
|
+
const json = await safeJson(resp);
|
|
193
|
+
if (!resp.ok || json?.success === false) throw toError(resp, json, 'Get profile failed');
|
|
194
|
+
return json;
|
|
195
|
+
}
|
|
140
196
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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
|
|
197
|
+
/**
|
|
198
|
+
* Generic authorized call for custom endpoints
|
|
199
|
+
* @param {string} path - API endpoint path
|
|
200
|
+
* @param {Object} options - Request options
|
|
201
|
+
* @returns {Promise<Object>} Response data
|
|
202
|
+
*/
|
|
153
203
|
async authed(path, { method = 'GET', body, headers } = {}) {
|
|
154
204
|
const resp = await this.fetch(this._buildUrl(path), {
|
|
155
205
|
method,
|