@yusufstar07/sdk-client 1.0.0 → 1.1.1
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/README.md +1 -3
- package/dist/client.d.ts +7 -21
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +164 -189
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/types.d.ts +0 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +3 -21
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +3 -64
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -78,7 +78,6 @@ import { NextResponse } from "next/server";
|
|
|
78
78
|
const sdk = createSDKClient({
|
|
79
79
|
apiKey: process.env.API_KEY!,
|
|
80
80
|
baseUrl: process.env.API_URL!,
|
|
81
|
-
fetch: fetch, // Use native fetch for SSR
|
|
82
81
|
});
|
|
83
82
|
|
|
84
83
|
export async function GET() {
|
|
@@ -217,7 +216,6 @@ interface SDKClientConfig {
|
|
|
217
216
|
baseUrl: string; // Required: API base URL
|
|
218
217
|
cookieDomain?: string; // Optional: Cookie domain
|
|
219
218
|
useCookies?: boolean; // Optional: Use cookies for token storage (default: true)
|
|
220
|
-
fetch?: typeof fetch; // Optional: Custom fetch implementation (required for SSR)
|
|
221
219
|
}
|
|
222
220
|
```
|
|
223
221
|
|
|
@@ -240,7 +238,7 @@ The SDK is fully compatible with Next.js App Router and Server Components:
|
|
|
240
238
|
|
|
241
239
|
1. **Server Components**: Use `getAccessTokenFromCookieString()` to get tokens from cookies
|
|
242
240
|
2. **API Routes**: Use `cookies()` from `next/headers` to get cookie string
|
|
243
|
-
3. **
|
|
241
|
+
3. **Axios**: SDK uses axios internally, which works seamlessly in both client and server environments
|
|
244
242
|
|
|
245
243
|
## TypeScript Support
|
|
246
244
|
|
package/dist/client.d.ts
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
import type { SDKClientConfig, RegisterRequest, RegisterResponse, LoginRequest, LoginResponse, RefreshResponse, LogoutResponse, MeResponse } from "./types";
|
|
2
2
|
export declare class SDKClient {
|
|
3
3
|
private config;
|
|
4
|
-
private
|
|
4
|
+
private axiosInstance;
|
|
5
5
|
private refreshPromise;
|
|
6
6
|
constructor(config: SDKClientConfig);
|
|
7
|
-
/**
|
|
8
|
-
* Make authenticated request with automatic token refresh and retry
|
|
9
|
-
*/
|
|
10
|
-
private request;
|
|
11
7
|
/**
|
|
12
8
|
* Internal refresh token method (prevents concurrent refresh requests)
|
|
13
9
|
*/
|
|
@@ -17,19 +13,15 @@ export declare class SDKClient {
|
|
|
17
13
|
*/
|
|
18
14
|
private performRefresh;
|
|
19
15
|
/**
|
|
20
|
-
* Get access token from cookie
|
|
16
|
+
* Get access token from cookie (for client-side only, axios handles cookies automatically)
|
|
21
17
|
*/
|
|
22
18
|
private getAccessToken;
|
|
23
19
|
/**
|
|
24
|
-
* Get refresh token from cookie
|
|
20
|
+
* Get refresh token from cookie (for client-side only, axios handles cookies automatically)
|
|
25
21
|
*/
|
|
26
22
|
private getRefreshToken;
|
|
27
23
|
/**
|
|
28
|
-
*
|
|
29
|
-
*/
|
|
30
|
-
private storeTokens;
|
|
31
|
-
/**
|
|
32
|
-
* Clear tokens from cookies
|
|
24
|
+
* Clear tokens - axios will handle cookie clearing via backend response
|
|
33
25
|
*/
|
|
34
26
|
private clearTokens;
|
|
35
27
|
/**
|
|
@@ -50,8 +42,10 @@ export declare class SDKClient {
|
|
|
50
42
|
logout(accessToken?: string): Promise<LogoutResponse>;
|
|
51
43
|
/**
|
|
52
44
|
* Get current user
|
|
45
|
+
* Token is automatically sent via cookies (httpOnly)
|
|
46
|
+
* Backend reads token from cookie, no Authorization header needed
|
|
53
47
|
*/
|
|
54
|
-
me(
|
|
48
|
+
me(): Promise<MeResponse>;
|
|
55
49
|
/**
|
|
56
50
|
* Get access token (for manual token management)
|
|
57
51
|
*/
|
|
@@ -65,12 +59,4 @@ export declare class SDKClient {
|
|
|
65
59
|
* Create SDK client instance
|
|
66
60
|
*/
|
|
67
61
|
export declare function createSDKClient(config: SDKClientConfig): SDKClient;
|
|
68
|
-
/**
|
|
69
|
-
* Get access token from cookie string (for SSR)
|
|
70
|
-
*/
|
|
71
|
-
export declare function getAccessTokenFromCookieString(cookieString?: string): string | null;
|
|
72
|
-
/**
|
|
73
|
-
* Get refresh token from cookie string (for SSR)
|
|
74
|
-
*/
|
|
75
|
-
export declare function getRefreshTokenFromCookieString(cookieString?: string): string | null;
|
|
76
62
|
//# sourceMappingURL=client.d.ts.map
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACR,eAAe,EACf,eAAe,EACf,gBAAgB,EAChB,YAAY,EACZ,aAAa,EACb,eAAe,EACf,cAAc,EACd,UAAU,EAEb,MAAM,SAAS,CAAC;AAEjB,qBAAa,SAAS;IAClB,OAAO,CAAC,MAAM,CAKZ;IACF,OAAO,CAAC,aAAa,CAAgB;IACrC,OAAO,CAAC,cAAc,CAAyC;gBAEnD,MAAM,EAAE,eAAe;IAuFnC;;OAEG;YACW,oBAAoB;IAkBlC;;OAEG;YACW,cAAc;IAwC5B;;OAEG;IACH,OAAO,CAAC,cAAc;IAgBtB;;OAEG;IACH,OAAO,CAAC,eAAe;IAevB;;OAEG;IACH,OAAO,CAAC,WAAW;IASnB;;OAEG;IACG,QAAQ,CAAC,IAAI,EAAE,eAAe,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAchE;;OAEG;IACG,KAAK,CAAC,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC;IAkBvD;;OAEG;IACG,OAAO,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC;IAwC9D;;OAEG;IACG,MAAM,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC;IAwC3D;;;;OAIG;IACG,EAAE,IAAI,OAAO,CAAC,UAAU,CAAC;IAgB/B;;OAEG;IACH,QAAQ,IAAI,MAAM,GAAG,IAAI;IAIzB;;OAEG;IACH,eAAe,IAAI,OAAO;CAG7B;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,eAAe,GAAG,SAAS,CAElE"}
|
package/dist/client.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import axios from "axios";
|
|
2
2
|
export class SDKClient {
|
|
3
3
|
constructor(config) {
|
|
4
4
|
this.refreshPromise = null; // Prevent concurrent refresh requests
|
|
@@ -8,67 +8,68 @@ export class SDKClient {
|
|
|
8
8
|
if (!config.baseUrl) {
|
|
9
9
|
throw new Error("Base URL is required");
|
|
10
10
|
}
|
|
11
|
-
const fetchImpl = config.fetch ?? (typeof fetch !== "undefined" ? fetch : undefined);
|
|
12
|
-
if (!fetchImpl) {
|
|
13
|
-
throw new Error("Fetch is not available. Provide a custom fetch implementation for SSR.");
|
|
14
|
-
}
|
|
15
11
|
this.config = {
|
|
16
12
|
apiKey: config.apiKey,
|
|
17
13
|
baseUrl: config.baseUrl.replace(/\/$/, ""), // Remove trailing slash
|
|
18
14
|
cookieDomain: config.cookieDomain,
|
|
19
15
|
useCookies: config.useCookies ?? true,
|
|
20
16
|
};
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"Content-Type": "application/json",
|
|
30
|
-
"x-api-key": this.config.apiKey,
|
|
31
|
-
...options.headers,
|
|
32
|
-
};
|
|
33
|
-
// Add access token if needed
|
|
34
|
-
if (useAuth) {
|
|
35
|
-
const accessToken = this.getAccessToken();
|
|
36
|
-
if (accessToken) {
|
|
37
|
-
headers["Authorization"] = `Bearer ${accessToken}`;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
const response = await this.fetchImpl(url, {
|
|
41
|
-
...options,
|
|
42
|
-
headers,
|
|
43
|
-
credentials: "include", // Include cookies for SSR
|
|
17
|
+
// Create axios instance
|
|
18
|
+
this.axiosInstance = axios.create({
|
|
19
|
+
baseURL: this.config.baseUrl,
|
|
20
|
+
headers: {
|
|
21
|
+
"Content-Type": "application/json",
|
|
22
|
+
"x-api-key": this.config.apiKey,
|
|
23
|
+
},
|
|
24
|
+
withCredentials: true, // Include cookies automatically
|
|
44
25
|
});
|
|
45
|
-
|
|
46
|
-
//
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
//
|
|
52
|
-
return this.request(endpoint, options, useAuth, retryCount + 1);
|
|
26
|
+
// Add request interceptor for API key only
|
|
27
|
+
// Cookies (including auth tokens) are automatically sent via withCredentials: true
|
|
28
|
+
this.axiosInstance.interceptors.request.use((config) => {
|
|
29
|
+
// Always add API key
|
|
30
|
+
if (config.headers) {
|
|
31
|
+
config.headers["x-api-key"] = this.config.apiKey;
|
|
32
|
+
// Do NOT add Authorization header - backend will read token from cookie
|
|
53
33
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
34
|
+
return config;
|
|
35
|
+
}, (error) => {
|
|
36
|
+
return Promise.reject(error);
|
|
37
|
+
});
|
|
38
|
+
// Add response interceptor for automatic token refresh
|
|
39
|
+
this.axiosInstance.interceptors.response.use((response) => response, async (error) => {
|
|
40
|
+
const originalRequest = error.config;
|
|
41
|
+
// Handle 401 Unauthorized - try to refresh token and retry
|
|
42
|
+
if (error.response?.status === 401 && originalRequest && !originalRequest._retry) {
|
|
43
|
+
originalRequest._retry = true;
|
|
44
|
+
try {
|
|
45
|
+
// Attempt to refresh token
|
|
46
|
+
await this.refreshTokenInternal();
|
|
47
|
+
// Retry the original request with new token
|
|
48
|
+
const accessToken = this.getAccessToken();
|
|
49
|
+
if (accessToken) {
|
|
50
|
+
originalRequest.headers["Authorization"] = `Bearer ${accessToken}`;
|
|
51
|
+
}
|
|
52
|
+
return this.axiosInstance(originalRequest);
|
|
53
|
+
}
|
|
54
|
+
catch (refreshError) {
|
|
55
|
+
// Refresh failed - clear tokens and throw original 401 error
|
|
56
|
+
this.clearTokens();
|
|
57
|
+
const errorData = error.response?.data;
|
|
58
|
+
const sdkError = {
|
|
59
|
+
error: errorData?.error || "Unauthorized",
|
|
60
|
+
status: 401,
|
|
61
|
+
};
|
|
62
|
+
return Promise.reject(sdkError);
|
|
63
|
+
}
|
|
62
64
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
error:
|
|
67
|
-
status: response
|
|
65
|
+
// Handle other errors
|
|
66
|
+
const errorData = error.response?.data;
|
|
67
|
+
const sdkError = {
|
|
68
|
+
error: errorData?.error || error.message || "Request failed",
|
|
69
|
+
status: error.response?.status,
|
|
68
70
|
};
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
return data;
|
|
71
|
+
return Promise.reject(sdkError);
|
|
72
|
+
});
|
|
72
73
|
}
|
|
73
74
|
/**
|
|
74
75
|
* Internal refresh token method (prevents concurrent refresh requests)
|
|
@@ -100,119 +101,109 @@ export class SDKClient {
|
|
|
100
101
|
status: 401,
|
|
101
102
|
};
|
|
102
103
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
// Refresh failed - clear tokens
|
|
104
|
+
try {
|
|
105
|
+
const response = await this.axiosInstance.post("/api/sdk/refresh", {}, {
|
|
106
|
+
headers: {
|
|
107
|
+
Authorization: `Bearer ${refreshToken}`,
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
const refreshResponse = response.data;
|
|
111
|
+
// Axios automatically handles cookies from Set-Cookie headers
|
|
112
|
+
// No manual cookie setting needed
|
|
113
|
+
return refreshResponse;
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
// Refresh failed - clear tokens
|
|
116
117
|
this.clearTokens();
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
118
|
+
const errorData = error.response?.data || error;
|
|
119
|
+
const sdkError = {
|
|
120
|
+
error: errorData?.error || "Failed to refresh token",
|
|
121
|
+
status: error.response?.status || 401,
|
|
120
122
|
};
|
|
121
|
-
throw
|
|
123
|
+
throw sdkError;
|
|
122
124
|
}
|
|
123
|
-
const refreshResponse = data;
|
|
124
|
-
// Update access token in cookies
|
|
125
|
-
if (this.config.useCookies && refreshResponse.accessToken) {
|
|
126
|
-
setCookie("sdk_access_token", refreshResponse.accessToken, {
|
|
127
|
-
domain: this.config.cookieDomain,
|
|
128
|
-
maxAge: refreshResponse.expiresIn || 15 * 60, // Use expiresIn from response or default 15 minutes
|
|
129
|
-
path: "/",
|
|
130
|
-
secure: true,
|
|
131
|
-
sameSite: "lax",
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
return refreshResponse;
|
|
135
125
|
}
|
|
136
126
|
/**
|
|
137
|
-
* Get access token from cookie
|
|
127
|
+
* Get access token from cookie (for client-side only, axios handles cookies automatically)
|
|
138
128
|
*/
|
|
139
129
|
getAccessToken() {
|
|
130
|
+
// Axios automatically manages cookies with withCredentials: true
|
|
131
|
+
// We only need to read from cookies for manual token passing (SSR scenarios)
|
|
140
132
|
if (this.config.useCookies && typeof document !== "undefined") {
|
|
141
|
-
|
|
133
|
+
const cookies = document.cookie.split(";").reduce((acc, cookie) => {
|
|
134
|
+
const [key, value] = cookie.trim().split("=");
|
|
135
|
+
if (key && value) {
|
|
136
|
+
acc[key] = decodeURIComponent(value);
|
|
137
|
+
}
|
|
138
|
+
return acc;
|
|
139
|
+
}, {});
|
|
140
|
+
return cookies["sdk_access_token"] || null;
|
|
142
141
|
}
|
|
143
142
|
return null;
|
|
144
143
|
}
|
|
145
144
|
/**
|
|
146
|
-
* Get refresh token from cookie
|
|
145
|
+
* Get refresh token from cookie (for client-side only, axios handles cookies automatically)
|
|
147
146
|
*/
|
|
148
147
|
getRefreshToken() {
|
|
148
|
+
// Axios automatically manages cookies with withCredentials: true
|
|
149
149
|
if (this.config.useCookies && typeof document !== "undefined") {
|
|
150
|
-
|
|
150
|
+
const cookies = document.cookie.split(";").reduce((acc, cookie) => {
|
|
151
|
+
const [key, value] = cookie.trim().split("=");
|
|
152
|
+
if (key && value) {
|
|
153
|
+
acc[key] = decodeURIComponent(value);
|
|
154
|
+
}
|
|
155
|
+
return acc;
|
|
156
|
+
}, {});
|
|
157
|
+
return cookies["sdk_refresh_token"] || null;
|
|
151
158
|
}
|
|
152
159
|
return null;
|
|
153
160
|
}
|
|
154
161
|
/**
|
|
155
|
-
*
|
|
156
|
-
*/
|
|
157
|
-
storeTokens(accessToken, refreshToken) {
|
|
158
|
-
if (!this.config.useCookies || typeof document === "undefined") {
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
// Access token expires in 15 minutes
|
|
162
|
-
setCookie("sdk_access_token", accessToken, {
|
|
163
|
-
domain: this.config.cookieDomain,
|
|
164
|
-
maxAge: 15 * 60, // 15 minutes
|
|
165
|
-
path: "/",
|
|
166
|
-
secure: true,
|
|
167
|
-
sameSite: "lax",
|
|
168
|
-
});
|
|
169
|
-
// Refresh token expires in 7 days
|
|
170
|
-
setCookie("sdk_refresh_token", refreshToken, {
|
|
171
|
-
domain: this.config.cookieDomain,
|
|
172
|
-
maxAge: 7 * 24 * 60 * 60, // 7 days
|
|
173
|
-
path: "/",
|
|
174
|
-
secure: true,
|
|
175
|
-
sameSite: "lax",
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Clear tokens from cookies
|
|
162
|
+
* Clear tokens - axios will handle cookie clearing via backend response
|
|
180
163
|
*/
|
|
181
164
|
clearTokens() {
|
|
182
|
-
|
|
183
|
-
|
|
165
|
+
// Axios automatically handles cookies, but we can clear them manually if needed
|
|
166
|
+
if (typeof document !== "undefined") {
|
|
167
|
+
// Clear cookies by setting them to expire
|
|
168
|
+
document.cookie = `sdk_access_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; ${this.config.cookieDomain ? `domain=${this.config.cookieDomain};` : ""}`;
|
|
169
|
+
document.cookie = `sdk_refresh_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; ${this.config.cookieDomain ? `domain=${this.config.cookieDomain};` : ""}`;
|
|
184
170
|
}
|
|
185
|
-
deleteCookie("sdk_access_token", {
|
|
186
|
-
domain: this.config.cookieDomain,
|
|
187
|
-
path: "/",
|
|
188
|
-
});
|
|
189
|
-
deleteCookie("sdk_refresh_token", {
|
|
190
|
-
domain: this.config.cookieDomain,
|
|
191
|
-
path: "/",
|
|
192
|
-
});
|
|
193
171
|
}
|
|
194
172
|
/**
|
|
195
173
|
* Register a new user
|
|
196
174
|
*/
|
|
197
175
|
async register(data) {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
176
|
+
try {
|
|
177
|
+
const response = await this.axiosInstance.post("/api/sdk/register", data);
|
|
178
|
+
return response.data;
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
const errorData = error.response?.data || error;
|
|
182
|
+
const sdkError = {
|
|
183
|
+
error: errorData?.error || "Failed to register",
|
|
184
|
+
status: error.response?.status,
|
|
185
|
+
};
|
|
186
|
+
throw sdkError;
|
|
187
|
+
}
|
|
202
188
|
}
|
|
203
189
|
/**
|
|
204
190
|
* Login user
|
|
205
191
|
*/
|
|
206
192
|
async login(data) {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
193
|
+
try {
|
|
194
|
+
const response = await this.axiosInstance.post("/api/sdk/login", data);
|
|
195
|
+
// Axios automatically handles cookies from Set-Cookie headers
|
|
196
|
+
// No manual cookie setting needed
|
|
197
|
+
return response.data;
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
const errorData = error.response?.data || error;
|
|
201
|
+
const sdkError = {
|
|
202
|
+
error: errorData?.error || "Failed to login",
|
|
203
|
+
status: error.response?.status,
|
|
204
|
+
};
|
|
205
|
+
throw sdkError;
|
|
214
206
|
}
|
|
215
|
-
return response;
|
|
216
207
|
}
|
|
217
208
|
/**
|
|
218
209
|
* Refresh access token (manual refresh)
|
|
@@ -225,38 +216,27 @@ export class SDKClient {
|
|
|
225
216
|
status: 401,
|
|
226
217
|
};
|
|
227
218
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
219
|
+
try {
|
|
220
|
+
const response = await this.axiosInstance.post("/api/sdk/refresh", {}, {
|
|
221
|
+
headers: {
|
|
222
|
+
Authorization: `Bearer ${token}`,
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
const refreshResponse = response.data;
|
|
226
|
+
// Axios automatically handles cookies from Set-Cookie headers
|
|
227
|
+
// No manual cookie setting needed
|
|
228
|
+
return refreshResponse;
|
|
229
|
+
}
|
|
230
|
+
catch (error) {
|
|
240
231
|
// Refresh failed - clear tokens
|
|
241
232
|
this.clearTokens();
|
|
242
|
-
const
|
|
243
|
-
|
|
244
|
-
|
|
233
|
+
const errorData = error.response?.data || error;
|
|
234
|
+
const sdkError = {
|
|
235
|
+
error: errorData?.error || "Failed to refresh token",
|
|
236
|
+
status: error.response?.status || 401,
|
|
245
237
|
};
|
|
246
|
-
throw
|
|
238
|
+
throw sdkError;
|
|
247
239
|
}
|
|
248
|
-
const refreshResponse = data;
|
|
249
|
-
// Update access token in cookies
|
|
250
|
-
if (this.config.useCookies && refreshResponse.accessToken) {
|
|
251
|
-
setCookie("sdk_access_token", refreshResponse.accessToken, {
|
|
252
|
-
domain: this.config.cookieDomain,
|
|
253
|
-
maxAge: refreshResponse.expiresIn || 15 * 60,
|
|
254
|
-
path: "/",
|
|
255
|
-
secure: true,
|
|
256
|
-
sameSite: "lax",
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
return refreshResponse;
|
|
260
240
|
}
|
|
261
241
|
/**
|
|
262
242
|
* Logout user
|
|
@@ -272,39 +252,46 @@ export class SDKClient {
|
|
|
272
252
|
};
|
|
273
253
|
}
|
|
274
254
|
try {
|
|
275
|
-
const response = await this.
|
|
276
|
-
method: "POST",
|
|
255
|
+
const response = await this.axiosInstance.post("/api/sdk/logout", {}, {
|
|
277
256
|
headers: {
|
|
278
257
|
Authorization: `Bearer ${token}`,
|
|
279
258
|
},
|
|
280
|
-
}
|
|
259
|
+
});
|
|
281
260
|
// Clear tokens from cookies
|
|
282
261
|
this.clearTokens();
|
|
283
|
-
return response;
|
|
262
|
+
return response.data;
|
|
284
263
|
}
|
|
285
264
|
catch (error) {
|
|
286
265
|
// Clear cookies even if request fails
|
|
287
266
|
this.clearTokens();
|
|
288
|
-
|
|
267
|
+
const errorData = error.response?.data || error;
|
|
268
|
+
const sdkError = {
|
|
269
|
+
error: errorData?.error || "Failed to logout",
|
|
270
|
+
status: error.response?.status,
|
|
271
|
+
};
|
|
272
|
+
throw sdkError;
|
|
289
273
|
}
|
|
290
274
|
}
|
|
291
275
|
/**
|
|
292
276
|
* Get current user
|
|
277
|
+
* Token is automatically sent via cookies (httpOnly)
|
|
278
|
+
* Backend reads token from cookie, no Authorization header needed
|
|
293
279
|
*/
|
|
294
|
-
async me(
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
280
|
+
async me() {
|
|
281
|
+
try {
|
|
282
|
+
// No Authorization header - axios sends cookies automatically via withCredentials: true
|
|
283
|
+
// Backend reads token from cookie
|
|
284
|
+
const response = await this.axiosInstance.get("/api/sdk/me");
|
|
285
|
+
return response.data;
|
|
286
|
+
}
|
|
287
|
+
catch (error) {
|
|
288
|
+
const errorData = error.response?.data || error;
|
|
289
|
+
const sdkError = {
|
|
290
|
+
error: errorData?.error || "Failed to get user",
|
|
291
|
+
status: error.response?.status,
|
|
300
292
|
};
|
|
293
|
+
throw sdkError;
|
|
301
294
|
}
|
|
302
|
-
return this.request("/api/sdk/me", {
|
|
303
|
-
method: "GET",
|
|
304
|
-
headers: {
|
|
305
|
-
Authorization: `Bearer ${token}`,
|
|
306
|
-
},
|
|
307
|
-
}, true);
|
|
308
295
|
}
|
|
309
296
|
/**
|
|
310
297
|
* Get access token (for manual token management)
|
|
@@ -325,15 +312,3 @@ export class SDKClient {
|
|
|
325
312
|
export function createSDKClient(config) {
|
|
326
313
|
return new SDKClient(config);
|
|
327
314
|
}
|
|
328
|
-
/**
|
|
329
|
-
* Get access token from cookie string (for SSR)
|
|
330
|
-
*/
|
|
331
|
-
export function getAccessTokenFromCookieString(cookieString) {
|
|
332
|
-
return getAccessTokenFromCookies(cookieString);
|
|
333
|
-
}
|
|
334
|
-
/**
|
|
335
|
-
* Get refresh token from cookie string (for SSR)
|
|
336
|
-
*/
|
|
337
|
-
export function getRefreshTokenFromCookieString(cookieString) {
|
|
338
|
-
return getRefreshTokenFromCookies(cookieString);
|
|
339
|
-
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Multi-Auth Platform SDK Client
|
|
3
3
|
*
|
|
4
|
-
* A
|
|
5
|
-
*
|
|
4
|
+
* A client-side authentication client for Next.js and React applications.
|
|
5
|
+
* Uses axios with automatic cookie management.
|
|
6
6
|
*/
|
|
7
|
-
export { SDKClient, createSDKClient
|
|
7
|
+
export { SDKClient, createSDKClient } from "./client";
|
|
8
8
|
export type { SDKUser, RegisterRequest, RegisterResponse, LoginRequest, LoginResponse, RefreshResponse, LogoutResponse, MeResponse, SDKError, SDKClientConfig, } from "./types";
|
|
9
9
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AACtD,YAAY,EACR,OAAO,EACP,eAAe,EACf,gBAAgB,EAChB,YAAY,EACZ,aAAa,EACb,eAAe,EACf,cAAc,EACd,UAAU,EACV,QAAQ,EACR,eAAe,GAClB,MAAM,SAAS,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Multi-Auth Platform SDK Client
|
|
3
3
|
*
|
|
4
|
-
* A
|
|
5
|
-
*
|
|
4
|
+
* A client-side authentication client for Next.js and React applications.
|
|
5
|
+
* Uses axios with automatic cookie management.
|
|
6
6
|
*/
|
|
7
|
-
export { SDKClient, createSDKClient
|
|
7
|
+
export { SDKClient, createSDKClient } from "./client";
|
package/dist/types.d.ts
CHANGED
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,OAAO;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,eAAe,EAAE,OAAO,CAAC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AC;IAChB;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;CACxB"}
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,27 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Utility functions for SDK Client
|
|
3
|
+
*
|
|
4
|
+
* Note: Axios automatically handles cookies with withCredentials: true
|
|
5
|
+
* These utilities are only for SSR scenarios where we need to read cookies manually
|
|
3
6
|
*/
|
|
4
|
-
/**
|
|
5
|
-
* Get cookie value by name
|
|
6
|
-
*/
|
|
7
|
-
export declare function getCookie(name: string): string | null;
|
|
8
|
-
/**
|
|
9
|
-
* Set cookie
|
|
10
|
-
*/
|
|
11
|
-
export declare function setCookie(name: string, value: string, options?: {
|
|
12
|
-
domain?: string;
|
|
13
|
-
maxAge?: number;
|
|
14
|
-
path?: string;
|
|
15
|
-
secure?: boolean;
|
|
16
|
-
sameSite?: "strict" | "lax" | "none";
|
|
17
|
-
}): void;
|
|
18
|
-
/**
|
|
19
|
-
* Delete cookie
|
|
20
|
-
*/
|
|
21
|
-
export declare function deleteCookie(name: string, options?: {
|
|
22
|
-
domain?: string;
|
|
23
|
-
path?: string;
|
|
24
|
-
}): void;
|
|
25
7
|
/**
|
|
26
8
|
* Get cookies from cookie string (for SSR)
|
|
27
9
|
*/
|
package/dist/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;GAEG;AACH,wBAAgB,YAAY,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAY1E;AAED;;GAEG;AACH,wBAAgB,yBAAyB,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAG9E;AAED;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAG/E"}
|
package/dist/utils.js
CHANGED
|
@@ -1,70 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Utility functions for SDK Client
|
|
3
|
+
*
|
|
4
|
+
* Note: Axios automatically handles cookies with withCredentials: true
|
|
5
|
+
* These utilities are only for SSR scenarios where we need to read cookies manually
|
|
3
6
|
*/
|
|
4
|
-
/**
|
|
5
|
-
* Get cookie value by name
|
|
6
|
-
*/
|
|
7
|
-
export function getCookie(name) {
|
|
8
|
-
if (typeof document === "undefined") {
|
|
9
|
-
return null;
|
|
10
|
-
}
|
|
11
|
-
const value = `; ${document.cookie}`;
|
|
12
|
-
const parts = value.split(`; ${name}=`);
|
|
13
|
-
if (parts.length === 2) {
|
|
14
|
-
return parts.pop()?.split(";").shift() || null;
|
|
15
|
-
}
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Set cookie
|
|
20
|
-
*/
|
|
21
|
-
export function setCookie(name, value, options = {}) {
|
|
22
|
-
if (typeof document === "undefined") {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
let cookieString = `${name}=${encodeURIComponent(value)}`;
|
|
26
|
-
if (options.maxAge) {
|
|
27
|
-
cookieString += `; max-age=${options.maxAge}`;
|
|
28
|
-
}
|
|
29
|
-
if (options.path) {
|
|
30
|
-
cookieString += `; path=${options.path}`;
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
cookieString += `; path=/`;
|
|
34
|
-
}
|
|
35
|
-
if (options.domain) {
|
|
36
|
-
cookieString += `; domain=${options.domain}`;
|
|
37
|
-
}
|
|
38
|
-
if (options.secure) {
|
|
39
|
-
cookieString += `; secure`;
|
|
40
|
-
}
|
|
41
|
-
if (options.sameSite) {
|
|
42
|
-
cookieString += `; samesite=${options.sameSite}`;
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
cookieString += `; samesite=lax`;
|
|
46
|
-
}
|
|
47
|
-
document.cookie = cookieString;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Delete cookie
|
|
51
|
-
*/
|
|
52
|
-
export function deleteCookie(name, options = {}) {
|
|
53
|
-
if (typeof document === "undefined") {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
let cookieString = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC`;
|
|
57
|
-
if (options.path) {
|
|
58
|
-
cookieString += `; path=${options.path}`;
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
cookieString += `; path=/`;
|
|
62
|
-
}
|
|
63
|
-
if (options.domain) {
|
|
64
|
-
cookieString += `; domain=${options.domain}`;
|
|
65
|
-
}
|
|
66
|
-
document.cookie = cookieString;
|
|
67
|
-
}
|
|
68
7
|
/**
|
|
69
8
|
* Get cookies from cookie string (for SSR)
|
|
70
9
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yusufstar07/sdk-client",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "SDK client for Multi-Auth Platform authentication",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -23,12 +23,15 @@
|
|
|
23
23
|
"dist",
|
|
24
24
|
"README.md"
|
|
25
25
|
],
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"axios": "^1.13.2"
|
|
28
|
+
},
|
|
26
29
|
"devDependencies": {
|
|
27
|
-
"@types/node": "^
|
|
28
|
-
"typescript": "^5.
|
|
30
|
+
"@types/node": "^25.0.3",
|
|
31
|
+
"typescript": "^5.9.3"
|
|
29
32
|
},
|
|
30
33
|
"peerDependencies": {
|
|
31
|
-
"react": "
|
|
34
|
+
"react": "19.2.3"
|
|
32
35
|
},
|
|
33
36
|
"peerDependenciesMeta": {
|
|
34
37
|
"react": {
|