@insforge/sdk 1.3.0-ssr.0 → 1.3.0-ssr.2
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 +14 -2
- package/dist/ssr.d.mts +20 -14
- package/dist/ssr.d.ts +20 -14
- package/dist/ssr.js +134 -58
- package/dist/ssr.js.map +1 -1
- package/dist/ssr.mjs +134 -58
- package/dist/ssr.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -264,7 +264,7 @@ const insforge = createClient({
|
|
|
264
264
|
### SSR / Next.js
|
|
265
265
|
|
|
266
266
|
Use `@insforge/sdk/ssr` for apps that need the same auth session in Server Components, Client Components, Storage, and Realtime.
|
|
267
|
-
The helper
|
|
267
|
+
The helper uses explicit `baseUrl` / `anonKey` when provided. Otherwise it reads `NEXT_PUBLIC_INSFORGE_URL` / `NEXT_PUBLIC_INSFORGE_ANON_KEY`. Missing config throws a clear error.
|
|
268
268
|
|
|
269
269
|
By default, the SSR helpers use:
|
|
270
270
|
|
|
@@ -295,7 +295,19 @@ import { createRefreshAuthRouter } from "@insforge/sdk/ssr";
|
|
|
295
295
|
export const { POST } = createRefreshAuthRouter();
|
|
296
296
|
```
|
|
297
297
|
|
|
298
|
-
For server-owned refresh cookies, run sign-in in a Route Handler or Server Action and use `setAuthCookies()` from `@insforge/sdk/ssr` with the
|
|
298
|
+
For server-owned refresh cookies, run sign-in in a Route Handler or Server Action and use `setAuthCookies()` from `@insforge/sdk/ssr` with the framework cookie writer. In Next.js Route Handlers, pass `response.cookies`:
|
|
299
|
+
|
|
300
|
+
```typescript
|
|
301
|
+
import { NextResponse } from "next/server";
|
|
302
|
+
import { setAuthCookies } from "@insforge/sdk/ssr";
|
|
303
|
+
|
|
304
|
+
const response = NextResponse.json({ user: data.user });
|
|
305
|
+
setAuthCookies(response.cookies, {
|
|
306
|
+
accessToken: data.accessToken,
|
|
307
|
+
refreshToken: data.refreshToken,
|
|
308
|
+
});
|
|
309
|
+
return response;
|
|
310
|
+
```
|
|
299
311
|
|
|
300
312
|
If your refresh route needs custom side effects:
|
|
301
313
|
|
package/dist/ssr.d.mts
CHANGED
|
@@ -2,11 +2,6 @@ import { a as InsForgeConfig, I as InsForgeClient, l as AuthRefreshResponse, d a
|
|
|
2
2
|
import '@insforge/shared-schemas';
|
|
3
3
|
import '@supabase/postgrest-js';
|
|
4
4
|
|
|
5
|
-
type SsrClientConfig = Omit<InsForgeConfig, 'baseUrl' | 'anonKey' | 'edgeFunctionToken' | 'isServerMode' | 'auth'> & {
|
|
6
|
-
baseUrl?: string;
|
|
7
|
-
anonKey?: string;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
5
|
declare const DEFAULT_ACCESS_TOKEN_COOKIE = "insforge_access_token";
|
|
11
6
|
declare const DEFAULT_REFRESH_TOKEN_COOKIE = "insforge_refresh_token";
|
|
12
7
|
interface AuthCookieNames {
|
|
@@ -29,10 +24,21 @@ interface AuthCookieOptions {
|
|
|
29
24
|
type CookieStoreValue = string | {
|
|
30
25
|
value?: string | null;
|
|
31
26
|
} | undefined | null;
|
|
32
|
-
interface
|
|
27
|
+
interface CookieReader {
|
|
33
28
|
get(name: string): CookieStoreValue;
|
|
29
|
+
}
|
|
30
|
+
interface CookieWriter {
|
|
34
31
|
set?(name: string, value: string, options?: CookieOptions): unknown;
|
|
35
|
-
|
|
32
|
+
set?(options: {
|
|
33
|
+
name: string;
|
|
34
|
+
value: string;
|
|
35
|
+
} & CookieOptions): unknown;
|
|
36
|
+
delete?(name: string): unknown;
|
|
37
|
+
delete?(options: {
|
|
38
|
+
name: string;
|
|
39
|
+
} & CookieOptions): unknown;
|
|
40
|
+
}
|
|
41
|
+
interface CookieStore extends CookieReader, CookieWriter {
|
|
36
42
|
}
|
|
37
43
|
interface AuthCookieSettings {
|
|
38
44
|
names?: AuthCookieNames;
|
|
@@ -42,25 +48,25 @@ declare function getAccessTokenCookieName(names?: AuthCookieNames): string;
|
|
|
42
48
|
declare function getRefreshTokenCookieName(names?: AuthCookieNames): string;
|
|
43
49
|
declare function accessTokenCookieOptions(token: string, overrides?: CookieOptions): CookieOptions;
|
|
44
50
|
declare function refreshTokenCookieOptions(token: string, overrides?: CookieOptions): CookieOptions;
|
|
45
|
-
declare function setAuthCookies(
|
|
51
|
+
declare function setAuthCookies(cookies: CookieWriter | undefined, tokens: {
|
|
46
52
|
accessToken: string;
|
|
47
53
|
refreshToken?: string | null;
|
|
48
54
|
}, settings?: AuthCookieSettings): void;
|
|
49
|
-
declare function clearAuthCookies(
|
|
55
|
+
declare function clearAuthCookies(cookies: CookieWriter | undefined, settings?: AuthCookieSettings): void;
|
|
50
56
|
|
|
51
|
-
interface CreateBrowserClientOptions extends
|
|
57
|
+
interface CreateBrowserClientOptions extends Omit<InsForgeConfig, 'edgeFunctionToken' | 'isServerMode' | 'auth'>, AuthCookieSettings {
|
|
52
58
|
refreshUrl?: string;
|
|
53
59
|
refreshLeewaySeconds?: number;
|
|
54
60
|
}
|
|
55
61
|
declare function createBrowserClient(options?: CreateBrowserClientOptions): InsForgeClient;
|
|
56
62
|
|
|
57
|
-
interface CreateServerClientOptions extends
|
|
63
|
+
interface CreateServerClientOptions extends Omit<InsForgeConfig, 'edgeFunctionToken' | 'isServerMode' | 'auth'>, AuthCookieSettings {
|
|
58
64
|
cookies?: Pick<CookieStore, 'get'>;
|
|
59
65
|
accessToken?: string;
|
|
60
66
|
}
|
|
61
67
|
declare function createServerClient(options?: CreateServerClientOptions): InsForgeClient;
|
|
62
68
|
|
|
63
|
-
interface RefreshAuthOptions extends
|
|
69
|
+
interface RefreshAuthOptions extends Omit<InsForgeConfig, 'edgeFunctionToken' | 'isServerMode' | 'auth'>, AuthCookieSettings {
|
|
64
70
|
request?: Request;
|
|
65
71
|
cookies?: Pick<CookieStore, 'get'>;
|
|
66
72
|
refreshToken?: string;
|
|
@@ -78,7 +84,7 @@ declare function createRefreshAuthRouter(options?: Omit<RefreshAuthOptions, 'req
|
|
|
78
84
|
POST: RefreshAuthRouteHandler;
|
|
79
85
|
};
|
|
80
86
|
|
|
81
|
-
interface UpdateSessionOptions extends
|
|
87
|
+
interface UpdateSessionOptions extends Omit<InsForgeConfig, 'edgeFunctionToken' | 'isServerMode' | 'auth'>, AuthCookieSettings {
|
|
82
88
|
requestCookies: CookieStore;
|
|
83
89
|
responseCookies: CookieStore;
|
|
84
90
|
refreshLeewaySeconds?: number;
|
|
@@ -90,4 +96,4 @@ interface UpdateSessionResult {
|
|
|
90
96
|
}
|
|
91
97
|
declare function updateSession(options: UpdateSessionOptions): Promise<UpdateSessionResult>;
|
|
92
98
|
|
|
93
|
-
export { type AuthCookieNames, type AuthCookieOptions, type AuthCookieSettings, type CookieOptions, type CookieStore, type CreateBrowserClientOptions, type CreateServerClientOptions, DEFAULT_ACCESS_TOKEN_COOKIE, DEFAULT_REFRESH_TOKEN_COOKIE, type RefreshAuthOptions, type RefreshAuthResult, type RefreshAuthRouteHandler, type UpdateSessionOptions, type UpdateSessionResult, accessTokenCookieOptions, clearAuthCookies, createBrowserClient, createRefreshAuthRouter, createServerClient, getAccessTokenCookieName, getRefreshTokenCookieName, refreshAuth, refreshTokenCookieOptions, setAuthCookies, updateSession };
|
|
99
|
+
export { type AuthCookieNames, type AuthCookieOptions, type AuthCookieSettings, type CookieOptions, type CookieReader, type CookieStore, type CookieWriter, type CreateBrowserClientOptions, type CreateServerClientOptions, DEFAULT_ACCESS_TOKEN_COOKIE, DEFAULT_REFRESH_TOKEN_COOKIE, type RefreshAuthOptions, type RefreshAuthResult, type RefreshAuthRouteHandler, type UpdateSessionOptions, type UpdateSessionResult, accessTokenCookieOptions, clearAuthCookies, createBrowserClient, createRefreshAuthRouter, createServerClient, getAccessTokenCookieName, getRefreshTokenCookieName, refreshAuth, refreshTokenCookieOptions, setAuthCookies, updateSession };
|
package/dist/ssr.d.ts
CHANGED
|
@@ -2,11 +2,6 @@ import { a as InsForgeConfig, I as InsForgeClient, l as AuthRefreshResponse, d a
|
|
|
2
2
|
import '@insforge/shared-schemas';
|
|
3
3
|
import '@supabase/postgrest-js';
|
|
4
4
|
|
|
5
|
-
type SsrClientConfig = Omit<InsForgeConfig, 'baseUrl' | 'anonKey' | 'edgeFunctionToken' | 'isServerMode' | 'auth'> & {
|
|
6
|
-
baseUrl?: string;
|
|
7
|
-
anonKey?: string;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
5
|
declare const DEFAULT_ACCESS_TOKEN_COOKIE = "insforge_access_token";
|
|
11
6
|
declare const DEFAULT_REFRESH_TOKEN_COOKIE = "insforge_refresh_token";
|
|
12
7
|
interface AuthCookieNames {
|
|
@@ -29,10 +24,21 @@ interface AuthCookieOptions {
|
|
|
29
24
|
type CookieStoreValue = string | {
|
|
30
25
|
value?: string | null;
|
|
31
26
|
} | undefined | null;
|
|
32
|
-
interface
|
|
27
|
+
interface CookieReader {
|
|
33
28
|
get(name: string): CookieStoreValue;
|
|
29
|
+
}
|
|
30
|
+
interface CookieWriter {
|
|
34
31
|
set?(name: string, value: string, options?: CookieOptions): unknown;
|
|
35
|
-
|
|
32
|
+
set?(options: {
|
|
33
|
+
name: string;
|
|
34
|
+
value: string;
|
|
35
|
+
} & CookieOptions): unknown;
|
|
36
|
+
delete?(name: string): unknown;
|
|
37
|
+
delete?(options: {
|
|
38
|
+
name: string;
|
|
39
|
+
} & CookieOptions): unknown;
|
|
40
|
+
}
|
|
41
|
+
interface CookieStore extends CookieReader, CookieWriter {
|
|
36
42
|
}
|
|
37
43
|
interface AuthCookieSettings {
|
|
38
44
|
names?: AuthCookieNames;
|
|
@@ -42,25 +48,25 @@ declare function getAccessTokenCookieName(names?: AuthCookieNames): string;
|
|
|
42
48
|
declare function getRefreshTokenCookieName(names?: AuthCookieNames): string;
|
|
43
49
|
declare function accessTokenCookieOptions(token: string, overrides?: CookieOptions): CookieOptions;
|
|
44
50
|
declare function refreshTokenCookieOptions(token: string, overrides?: CookieOptions): CookieOptions;
|
|
45
|
-
declare function setAuthCookies(
|
|
51
|
+
declare function setAuthCookies(cookies: CookieWriter | undefined, tokens: {
|
|
46
52
|
accessToken: string;
|
|
47
53
|
refreshToken?: string | null;
|
|
48
54
|
}, settings?: AuthCookieSettings): void;
|
|
49
|
-
declare function clearAuthCookies(
|
|
55
|
+
declare function clearAuthCookies(cookies: CookieWriter | undefined, settings?: AuthCookieSettings): void;
|
|
50
56
|
|
|
51
|
-
interface CreateBrowserClientOptions extends
|
|
57
|
+
interface CreateBrowserClientOptions extends Omit<InsForgeConfig, 'edgeFunctionToken' | 'isServerMode' | 'auth'>, AuthCookieSettings {
|
|
52
58
|
refreshUrl?: string;
|
|
53
59
|
refreshLeewaySeconds?: number;
|
|
54
60
|
}
|
|
55
61
|
declare function createBrowserClient(options?: CreateBrowserClientOptions): InsForgeClient;
|
|
56
62
|
|
|
57
|
-
interface CreateServerClientOptions extends
|
|
63
|
+
interface CreateServerClientOptions extends Omit<InsForgeConfig, 'edgeFunctionToken' | 'isServerMode' | 'auth'>, AuthCookieSettings {
|
|
58
64
|
cookies?: Pick<CookieStore, 'get'>;
|
|
59
65
|
accessToken?: string;
|
|
60
66
|
}
|
|
61
67
|
declare function createServerClient(options?: CreateServerClientOptions): InsForgeClient;
|
|
62
68
|
|
|
63
|
-
interface RefreshAuthOptions extends
|
|
69
|
+
interface RefreshAuthOptions extends Omit<InsForgeConfig, 'edgeFunctionToken' | 'isServerMode' | 'auth'>, AuthCookieSettings {
|
|
64
70
|
request?: Request;
|
|
65
71
|
cookies?: Pick<CookieStore, 'get'>;
|
|
66
72
|
refreshToken?: string;
|
|
@@ -78,7 +84,7 @@ declare function createRefreshAuthRouter(options?: Omit<RefreshAuthOptions, 'req
|
|
|
78
84
|
POST: RefreshAuthRouteHandler;
|
|
79
85
|
};
|
|
80
86
|
|
|
81
|
-
interface UpdateSessionOptions extends
|
|
87
|
+
interface UpdateSessionOptions extends Omit<InsForgeConfig, 'edgeFunctionToken' | 'isServerMode' | 'auth'>, AuthCookieSettings {
|
|
82
88
|
requestCookies: CookieStore;
|
|
83
89
|
responseCookies: CookieStore;
|
|
84
90
|
refreshLeewaySeconds?: number;
|
|
@@ -90,4 +96,4 @@ interface UpdateSessionResult {
|
|
|
90
96
|
}
|
|
91
97
|
declare function updateSession(options: UpdateSessionOptions): Promise<UpdateSessionResult>;
|
|
92
98
|
|
|
93
|
-
export { type AuthCookieNames, type AuthCookieOptions, type AuthCookieSettings, type CookieOptions, type CookieStore, type CreateBrowserClientOptions, type CreateServerClientOptions, DEFAULT_ACCESS_TOKEN_COOKIE, DEFAULT_REFRESH_TOKEN_COOKIE, type RefreshAuthOptions, type RefreshAuthResult, type RefreshAuthRouteHandler, type UpdateSessionOptions, type UpdateSessionResult, accessTokenCookieOptions, clearAuthCookies, createBrowserClient, createRefreshAuthRouter, createServerClient, getAccessTokenCookieName, getRefreshTokenCookieName, refreshAuth, refreshTokenCookieOptions, setAuthCookies, updateSession };
|
|
99
|
+
export { type AuthCookieNames, type AuthCookieOptions, type AuthCookieSettings, type CookieOptions, type CookieReader, type CookieStore, type CookieWriter, type CreateBrowserClientOptions, type CreateServerClientOptions, DEFAULT_ACCESS_TOKEN_COOKIE, DEFAULT_REFRESH_TOKEN_COOKIE, type RefreshAuthOptions, type RefreshAuthResult, type RefreshAuthRouteHandler, type UpdateSessionOptions, type UpdateSessionResult, accessTokenCookieOptions, clearAuthCookies, createBrowserClient, createRefreshAuthRouter, createServerClient, getAccessTokenCookieName, getRefreshTokenCookieName, refreshAuth, refreshTokenCookieOptions, setAuthCookies, updateSession };
|
package/dist/ssr.js
CHANGED
|
@@ -2659,26 +2659,6 @@ function isJwtExpiredOrExpiring(token, leewaySeconds = 60) {
|
|
|
2659
2659
|
return expires.getTime() <= Date.now() + leewaySeconds * 1e3;
|
|
2660
2660
|
}
|
|
2661
2661
|
|
|
2662
|
-
// src/ssr/config.ts
|
|
2663
|
-
function env(name) {
|
|
2664
|
-
if (typeof process === "undefined") return void 0;
|
|
2665
|
-
return process.env[name];
|
|
2666
|
-
}
|
|
2667
|
-
function resolveBrowserConfig(config = {}) {
|
|
2668
|
-
return {
|
|
2669
|
-
...config,
|
|
2670
|
-
baseUrl: config.baseUrl ?? env("NEXT_PUBLIC_INSFORGE_URL"),
|
|
2671
|
-
anonKey: config.anonKey ?? env("NEXT_PUBLIC_INSFORGE_ANON_KEY")
|
|
2672
|
-
};
|
|
2673
|
-
}
|
|
2674
|
-
function resolveServerConfig(config = {}) {
|
|
2675
|
-
return {
|
|
2676
|
-
...config,
|
|
2677
|
-
baseUrl: config.baseUrl ?? env("INSFORGE_URL") ?? env("NEXT_PUBLIC_INSFORGE_URL"),
|
|
2678
|
-
anonKey: config.anonKey ?? env("INSFORGE_ANON_KEY") ?? env("NEXT_PUBLIC_INSFORGE_ANON_KEY")
|
|
2679
|
-
};
|
|
2680
|
-
}
|
|
2681
|
-
|
|
2682
2662
|
// src/ssr/browser-client.ts
|
|
2683
2663
|
var import_shared_schemas2 = require("@insforge/shared-schemas");
|
|
2684
2664
|
|
|
@@ -2755,11 +2735,11 @@ function setCookie(cookies, name, value, options) {
|
|
|
2755
2735
|
}
|
|
2756
2736
|
function deleteCookie(cookies, name, options) {
|
|
2757
2737
|
if (!cookies) return;
|
|
2758
|
-
if (cookies.
|
|
2759
|
-
cookies.
|
|
2738
|
+
if (cookies.set) {
|
|
2739
|
+
cookies.set(name, "", expiredCookieOptions(options));
|
|
2760
2740
|
return;
|
|
2761
2741
|
}
|
|
2762
|
-
cookies.
|
|
2742
|
+
cookies.delete?.(name);
|
|
2763
2743
|
}
|
|
2764
2744
|
function serializeCookie(name, value, options = {}) {
|
|
2765
2745
|
const parts = [`${encodeURIComponent(name)}=${encodeURIComponent(value)}`];
|
|
@@ -2778,32 +2758,17 @@ function serializeCookie(name, value, options = {}) {
|
|
|
2778
2758
|
function appendSetCookie(headers, name, value, options) {
|
|
2779
2759
|
headers.append("Set-Cookie", serializeCookie(name, value, options));
|
|
2780
2760
|
}
|
|
2781
|
-
function setAuthCookies(
|
|
2761
|
+
function setAuthCookies(cookies, tokens, settings = {}) {
|
|
2782
2762
|
const accessName = getAccessTokenCookieName(settings.names);
|
|
2783
2763
|
const refreshName = getRefreshTokenCookieName(settings.names);
|
|
2784
2764
|
const accessOptions = accessTokenCookieOptions(
|
|
2785
2765
|
tokens.accessToken,
|
|
2786
2766
|
settings.options?.accessToken
|
|
2787
2767
|
);
|
|
2788
|
-
|
|
2789
|
-
appendSetCookie(target, accessName, tokens.accessToken, accessOptions);
|
|
2790
|
-
if (tokens.refreshToken) {
|
|
2791
|
-
appendSetCookie(
|
|
2792
|
-
target,
|
|
2793
|
-
refreshName,
|
|
2794
|
-
tokens.refreshToken,
|
|
2795
|
-
refreshTokenCookieOptions(
|
|
2796
|
-
tokens.refreshToken,
|
|
2797
|
-
settings.options?.refreshToken
|
|
2798
|
-
)
|
|
2799
|
-
);
|
|
2800
|
-
}
|
|
2801
|
-
return;
|
|
2802
|
-
}
|
|
2803
|
-
setCookie(target, accessName, tokens.accessToken, accessOptions);
|
|
2768
|
+
setCookie(cookies, accessName, tokens.accessToken, accessOptions);
|
|
2804
2769
|
if (tokens.refreshToken) {
|
|
2805
2770
|
setCookie(
|
|
2806
|
-
|
|
2771
|
+
cookies,
|
|
2807
2772
|
refreshName,
|
|
2808
2773
|
tokens.refreshToken,
|
|
2809
2774
|
refreshTokenCookieOptions(
|
|
@@ -2813,18 +2778,48 @@ function setAuthCookies(target, tokens, settings = {}) {
|
|
|
2813
2778
|
);
|
|
2814
2779
|
}
|
|
2815
2780
|
}
|
|
2816
|
-
function clearAuthCookies(
|
|
2781
|
+
function clearAuthCookies(cookies, settings = {}) {
|
|
2817
2782
|
const accessName = getAccessTokenCookieName(settings.names);
|
|
2818
2783
|
const refreshName = getRefreshTokenCookieName(settings.names);
|
|
2819
2784
|
const accessOptions = expiredCookieOptions(settings.options?.accessToken);
|
|
2820
2785
|
const refreshOptions = expiredCookieOptions(settings.options?.refreshToken);
|
|
2821
|
-
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2786
|
+
deleteCookie(cookies, accessName, accessOptions);
|
|
2787
|
+
deleteCookie(cookies, refreshName, refreshOptions);
|
|
2788
|
+
}
|
|
2789
|
+
function setAuthCookieHeaders(headers, tokens, settings = {}) {
|
|
2790
|
+
const accessName = getAccessTokenCookieName(settings.names);
|
|
2791
|
+
const refreshName = getRefreshTokenCookieName(settings.names);
|
|
2792
|
+
appendSetCookie(
|
|
2793
|
+
headers,
|
|
2794
|
+
accessName,
|
|
2795
|
+
tokens.accessToken,
|
|
2796
|
+
accessTokenCookieOptions(tokens.accessToken, settings.options?.accessToken)
|
|
2797
|
+
);
|
|
2798
|
+
if (tokens.refreshToken) {
|
|
2799
|
+
appendSetCookie(
|
|
2800
|
+
headers,
|
|
2801
|
+
refreshName,
|
|
2802
|
+
tokens.refreshToken,
|
|
2803
|
+
refreshTokenCookieOptions(
|
|
2804
|
+
tokens.refreshToken,
|
|
2805
|
+
settings.options?.refreshToken
|
|
2806
|
+
)
|
|
2807
|
+
);
|
|
2825
2808
|
}
|
|
2826
|
-
|
|
2827
|
-
|
|
2809
|
+
}
|
|
2810
|
+
function clearAuthCookieHeaders(headers, settings = {}) {
|
|
2811
|
+
appendSetCookie(
|
|
2812
|
+
headers,
|
|
2813
|
+
getAccessTokenCookieName(settings.names),
|
|
2814
|
+
"",
|
|
2815
|
+
expiredCookieOptions(settings.options?.accessToken)
|
|
2816
|
+
);
|
|
2817
|
+
appendSetCookie(
|
|
2818
|
+
headers,
|
|
2819
|
+
getRefreshTokenCookieName(settings.names),
|
|
2820
|
+
"",
|
|
2821
|
+
expiredCookieOptions(settings.options?.refreshToken)
|
|
2822
|
+
);
|
|
2828
2823
|
}
|
|
2829
2824
|
|
|
2830
2825
|
// src/ssr/browser-client.ts
|
|
@@ -2873,6 +2868,17 @@ function withAuthHeader(init, token) {
|
|
|
2873
2868
|
};
|
|
2874
2869
|
}
|
|
2875
2870
|
function createBrowserClient(options = {}) {
|
|
2871
|
+
let { baseUrl, anonKey } = options;
|
|
2872
|
+
try {
|
|
2873
|
+
baseUrl || (baseUrl = process.env.NEXT_PUBLIC_INSFORGE_URL);
|
|
2874
|
+
anonKey || (anonKey = process.env.NEXT_PUBLIC_INSFORGE_ANON_KEY);
|
|
2875
|
+
} catch {
|
|
2876
|
+
}
|
|
2877
|
+
if (!baseUrl || !anonKey) {
|
|
2878
|
+
throw new Error(
|
|
2879
|
+
"Missing InsForge baseUrl or anonKey. Pass baseUrl and anonKey to createBrowserClient() or set NEXT_PUBLIC_INSFORGE_URL and NEXT_PUBLIC_INSFORGE_ANON_KEY."
|
|
2880
|
+
);
|
|
2881
|
+
}
|
|
2876
2882
|
let accessToken = getBrowserCookie(
|
|
2877
2883
|
getAccessTokenCookieName(options.names)
|
|
2878
2884
|
);
|
|
@@ -2949,7 +2955,9 @@ function createBrowserClient(options = {}) {
|
|
|
2949
2955
|
return fetchImpl(input, withAuthHeader(init, refreshed.accessToken));
|
|
2950
2956
|
};
|
|
2951
2957
|
client = new InsForgeClient({
|
|
2952
|
-
...
|
|
2958
|
+
...options,
|
|
2959
|
+
baseUrl,
|
|
2960
|
+
anonKey,
|
|
2953
2961
|
fetch: ssrFetch
|
|
2954
2962
|
});
|
|
2955
2963
|
const setAccessToken = client.setAccessToken.bind(client);
|
|
@@ -2968,12 +2976,25 @@ function createBrowserClient(options = {}) {
|
|
|
2968
2976
|
|
|
2969
2977
|
// src/ssr/server-client.ts
|
|
2970
2978
|
function createServerClient(options = {}) {
|
|
2979
|
+
let { baseUrl, anonKey } = options;
|
|
2980
|
+
try {
|
|
2981
|
+
baseUrl || (baseUrl = process.env.NEXT_PUBLIC_INSFORGE_URL);
|
|
2982
|
+
anonKey || (anonKey = process.env.NEXT_PUBLIC_INSFORGE_ANON_KEY);
|
|
2983
|
+
} catch {
|
|
2984
|
+
}
|
|
2985
|
+
if (!baseUrl || !anonKey) {
|
|
2986
|
+
throw new Error(
|
|
2987
|
+
"Missing InsForge baseUrl or anonKey. Pass baseUrl and anonKey to createServerClient() or set NEXT_PUBLIC_INSFORGE_URL and NEXT_PUBLIC_INSFORGE_ANON_KEY."
|
|
2988
|
+
);
|
|
2989
|
+
}
|
|
2971
2990
|
const accessToken = options.accessToken ?? getCookieValue(
|
|
2972
2991
|
options.cookies,
|
|
2973
2992
|
getAccessTokenCookieName(options.names)
|
|
2974
2993
|
);
|
|
2975
2994
|
return new InsForgeClient({
|
|
2976
|
-
...
|
|
2995
|
+
...options,
|
|
2996
|
+
baseUrl,
|
|
2997
|
+
anonKey,
|
|
2977
2998
|
isServerMode: true,
|
|
2978
2999
|
edgeFunctionToken: accessToken ?? void 0
|
|
2979
3000
|
});
|
|
@@ -2990,12 +3011,25 @@ function jsonResponse(body, init = {}, headers = new Headers(init.headers)) {
|
|
|
2990
3011
|
}
|
|
2991
3012
|
function normalizeError(error) {
|
|
2992
3013
|
if (error instanceof InsForgeError) return error;
|
|
3014
|
+
if (error && typeof error === "object") {
|
|
3015
|
+
const body = error;
|
|
3016
|
+
return new InsForgeError(
|
|
3017
|
+
typeof body.message === "string" ? body.message : "Failed to refresh auth session",
|
|
3018
|
+
typeof body.statusCode === "number" ? body.statusCode : 500,
|
|
3019
|
+
typeof body.error === "string" ? body.error : import_shared_schemas3.ERROR_CODES.UNKNOWN_ERROR
|
|
3020
|
+
);
|
|
3021
|
+
}
|
|
2993
3022
|
return new InsForgeError(
|
|
2994
3023
|
error instanceof Error ? error.message : "Failed to refresh auth session",
|
|
2995
3024
|
500,
|
|
2996
3025
|
import_shared_schemas3.ERROR_CODES.UNKNOWN_ERROR
|
|
2997
3026
|
);
|
|
2998
3027
|
}
|
|
3028
|
+
async function readJson(response) {
|
|
3029
|
+
const contentType = response.headers.get("content-type");
|
|
3030
|
+
if (!contentType?.includes("json")) return null;
|
|
3031
|
+
return response.json();
|
|
3032
|
+
}
|
|
2999
3033
|
function readRefreshToken(options) {
|
|
3000
3034
|
if (options.refreshToken) return options.refreshToken;
|
|
3001
3035
|
const refreshCookieName = getRefreshTokenCookieName(options.names);
|
|
@@ -3010,7 +3044,7 @@ async function refreshAuth(options = {}) {
|
|
|
3010
3044
|
const headers = new Headers();
|
|
3011
3045
|
const refreshToken = readRefreshToken(options);
|
|
3012
3046
|
if (!refreshToken) {
|
|
3013
|
-
|
|
3047
|
+
clearAuthCookieHeaders(headers, options);
|
|
3014
3048
|
const error2 = new InsForgeError(
|
|
3015
3049
|
"Refresh token cookie is missing",
|
|
3016
3050
|
401,
|
|
@@ -3032,13 +3066,55 @@ async function refreshAuth(options = {}) {
|
|
|
3032
3066
|
error: error2
|
|
3033
3067
|
};
|
|
3034
3068
|
}
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
3039
|
-
|
|
3069
|
+
let { baseUrl, anonKey } = options;
|
|
3070
|
+
try {
|
|
3071
|
+
baseUrl || (baseUrl = process.env.NEXT_PUBLIC_INSFORGE_URL);
|
|
3072
|
+
anonKey || (anonKey = process.env.NEXT_PUBLIC_INSFORGE_ANON_KEY);
|
|
3073
|
+
} catch {
|
|
3074
|
+
}
|
|
3075
|
+
if (!baseUrl || !anonKey) {
|
|
3076
|
+
throw new Error(
|
|
3077
|
+
"Missing InsForge baseUrl or anonKey. Pass baseUrl and anonKey to refreshAuth() or set NEXT_PUBLIC_INSFORGE_URL and NEXT_PUBLIC_INSFORGE_ANON_KEY."
|
|
3078
|
+
);
|
|
3079
|
+
}
|
|
3080
|
+
const fetchImpl = options.fetch ?? (globalThis.fetch ? globalThis.fetch.bind(globalThis) : void 0);
|
|
3081
|
+
if (!fetchImpl) {
|
|
3082
|
+
throw new Error(
|
|
3083
|
+
"Fetch is not available. Please provide a fetch implementation."
|
|
3084
|
+
);
|
|
3085
|
+
}
|
|
3086
|
+
const requestHeaders = new Headers(options.headers);
|
|
3087
|
+
requestHeaders.set("Authorization", `Bearer ${anonKey}`);
|
|
3088
|
+
requestHeaders.set("Content-Type", "application/json");
|
|
3089
|
+
requestHeaders.set("Accept", "application/json");
|
|
3090
|
+
let data = null;
|
|
3091
|
+
let error = null;
|
|
3092
|
+
try {
|
|
3093
|
+
const response = await fetchImpl(
|
|
3094
|
+
new URL("/api/auth/refresh?client_type=mobile", baseUrl).toString(),
|
|
3095
|
+
{
|
|
3096
|
+
method: "POST",
|
|
3097
|
+
headers: requestHeaders,
|
|
3098
|
+
body: JSON.stringify({ refresh_token: refreshToken })
|
|
3099
|
+
}
|
|
3100
|
+
);
|
|
3101
|
+
const body = await readJson(response);
|
|
3102
|
+
if (!response.ok) {
|
|
3103
|
+
error = normalizeError(
|
|
3104
|
+
body ?? {
|
|
3105
|
+
message: "Failed to refresh auth session",
|
|
3106
|
+
statusCode: response.status,
|
|
3107
|
+
error: import_shared_schemas3.ERROR_CODES.UNKNOWN_ERROR
|
|
3108
|
+
}
|
|
3109
|
+
);
|
|
3110
|
+
} else {
|
|
3111
|
+
data = body;
|
|
3112
|
+
}
|
|
3113
|
+
} catch (caught) {
|
|
3114
|
+
error = normalizeError(caught);
|
|
3115
|
+
}
|
|
3040
3116
|
if (error || !data?.accessToken) {
|
|
3041
|
-
|
|
3117
|
+
clearAuthCookieHeaders(headers, options);
|
|
3042
3118
|
const normalized = normalizeError(error);
|
|
3043
3119
|
return {
|
|
3044
3120
|
response: jsonResponse(
|
|
@@ -3057,7 +3133,7 @@ async function refreshAuth(options = {}) {
|
|
|
3057
3133
|
};
|
|
3058
3134
|
}
|
|
3059
3135
|
const nextRefreshToken = data.refreshToken ?? refreshToken;
|
|
3060
|
-
|
|
3136
|
+
setAuthCookieHeaders(
|
|
3061
3137
|
headers,
|
|
3062
3138
|
{
|
|
3063
3139
|
accessToken: data.accessToken,
|