@crossmint/client-sdk-react-ui 1.3.23 → 1.4.0
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/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +20 -2
- package/dist/index.d.ts +20 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +7 -10
- package/src/components/CrossmintNFTCollectionView.tsx +1 -1
- package/src/components/CrossmintNFTDetail.test.tsx +1 -1
- package/src/components/CrossmintNFTDetail.tsx +1 -1
- package/src/components/auth/AuthModal.tsx +15 -11
- package/src/components/embed/EmbeddedCheckoutIFrame.tsx +2 -2
- package/src/components/embed/crypto/CryptoEmbeddedCheckout.tsx +1 -1
- package/src/components/embed/crypto/CryptoEmbeddedCheckoutIFrame.tsx +5 -5
- package/src/components/embed/fiat/FiatEmbeddedCheckout.tsx +1 -1
- package/src/components/embed/fiat/FiatEmbeddedCheckoutIFrame.tsx +1 -1
- package/src/components/embed/index.tsx +1 -1
- package/src/components/hosted/CrossmintPayButton.tsx +3 -3
- package/src/components/hosted/styles.ts +4 -5
- package/src/consts/version.ts +3 -1
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useCrossmint.test.tsx +44 -8
- package/src/hooks/useCrossmint.tsx +14 -3
- package/src/hooks/useDeepEffect.ts +1 -1
- package/src/hooks/useRefreshToken.test.ts +142 -0
- package/src/hooks/useRefreshToken.ts +62 -0
- package/src/index.ts +0 -1
- package/src/providers/CrossmintAuthProvider.test.tsx +105 -15
- package/src/providers/CrossmintAuthProvider.tsx +31 -26
- package/src/providers/CrossmintWalletProvider.test.tsx +3 -3
- package/src/providers/CrossmintWalletProvider.tsx +4 -4
- package/src/utils/authCookies.test.ts +41 -0
- package/src/utils/authCookies.ts +16 -0
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import bs58 from "bs58";
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
-
CryptoEmbeddedCheckoutPropsWithSigner,
|
|
5
|
-
ETHEmbeddedCheckoutSigner,
|
|
6
|
-
IncomingInternalEvent,
|
|
4
|
+
type CryptoEmbeddedCheckoutPropsWithSigner,
|
|
5
|
+
type ETHEmbeddedCheckoutSigner,
|
|
6
|
+
type IncomingInternalEvent,
|
|
7
7
|
IncomingInternalEvents,
|
|
8
8
|
OutgoingInternalEvents,
|
|
9
|
-
SOLEmbeddedCheckoutSigner,
|
|
9
|
+
type SOLEmbeddedCheckoutSigner,
|
|
10
10
|
crossmintIFrameService,
|
|
11
11
|
embeddedCheckoutPropsToUpdatableParamsPayload,
|
|
12
12
|
} from "@crossmint/client-sdk-base";
|
|
13
|
-
import { EVMBlockchainIncludingTestnet } from "@crossmint/common-sdk-base";
|
|
13
|
+
import type { EVMBlockchainIncludingTestnet } from "@crossmint/common-sdk-base";
|
|
14
14
|
|
|
15
15
|
import useDeepEffect from "../../../hooks/useDeepEffect";
|
|
16
16
|
import CrossmintEmbeddedCheckoutIFrame from "../EmbeddedCheckoutIFrame";
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { CSSProperties, MouseEvent, useMemo } from "react";
|
|
1
|
+
import { type CSSProperties, type MouseEvent, useMemo } from "react";
|
|
2
2
|
import { useState } from "react";
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
|
-
CheckoutProps,
|
|
6
|
-
CrossmintPayButtonProps,
|
|
5
|
+
type CheckoutProps,
|
|
6
|
+
type CrossmintPayButtonProps,
|
|
7
7
|
clientNames,
|
|
8
8
|
crossmintModalService,
|
|
9
9
|
crossmintPayButtonService,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Styles, createUseStyles } from "react-jss";
|
|
1
|
+
import { type Styles, createUseStyles } from "react-jss";
|
|
2
2
|
|
|
3
3
|
const DARK_BG = "#1e1e1e";
|
|
4
4
|
|
|
@@ -52,8 +52,7 @@ const styles: Styles<"crossmintButton" | "crossmintImg" | "crossmintParagraph" |
|
|
|
52
52
|
};
|
|
53
53
|
|
|
54
54
|
export const useStyles: (
|
|
55
|
-
data?:
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
55
|
+
data?: CustomStylingProps & {
|
|
56
|
+
theme?: any;
|
|
57
|
+
}
|
|
59
58
|
) => Classes<"crossmintButton" | "crossmintImg" | "crossmintParagraph" | "@global"> = createUseStyles(styles);
|
package/src/consts/version.ts
CHANGED
package/src/hooks/index.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { fireEvent, render } from "@testing-library/react";
|
|
|
2
2
|
import { useEffect } from "react";
|
|
3
3
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
4
4
|
|
|
5
|
-
import { Crossmint, createCrossmint } from "@crossmint/common-sdk-base";
|
|
5
|
+
import { type Crossmint, createCrossmint } from "@crossmint/common-sdk-base";
|
|
6
6
|
|
|
7
7
|
import { CrossmintProvider, useCrossmint } from "./useCrossmint";
|
|
8
8
|
|
|
@@ -18,6 +18,9 @@ class MockSDK {
|
|
|
18
18
|
somethingThatUpdatesJWT(newJWT: string) {
|
|
19
19
|
this.crossmint.jwt = newJWT;
|
|
20
20
|
}
|
|
21
|
+
somethingThatUpdatesRefreshToken(newRefreshToken: string) {
|
|
22
|
+
this.crossmint.refreshToken = newRefreshToken;
|
|
23
|
+
}
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
function renderCrossmintProvider({ children }: { children: JSX.Element }) {
|
|
@@ -30,16 +33,23 @@ describe("CrossmintProvider", () => {
|
|
|
30
33
|
vi.mocked(createCrossmint).mockImplementation(() => ({
|
|
31
34
|
apiKey: MOCK_API_KEY,
|
|
32
35
|
jwt: "",
|
|
36
|
+
refreshToken: "",
|
|
33
37
|
}));
|
|
34
38
|
});
|
|
35
39
|
|
|
36
|
-
it("provides initial JWT
|
|
40
|
+
it("provides initial JWT and refreshToken values", () => {
|
|
37
41
|
const TestComponent = () => {
|
|
38
42
|
const { crossmint } = useCrossmint();
|
|
39
|
-
return
|
|
43
|
+
return (
|
|
44
|
+
<div>
|
|
45
|
+
<div data-testid="jwt">{crossmint.jwt}</div>
|
|
46
|
+
<div data-testid="refreshToken">{crossmint.refreshToken}</div>
|
|
47
|
+
</div>
|
|
48
|
+
);
|
|
40
49
|
};
|
|
41
50
|
const { getByTestId } = renderCrossmintProvider({ children: <TestComponent /> });
|
|
42
51
|
expect(getByTestId("jwt").textContent).toBe("");
|
|
52
|
+
expect(getByTestId("refreshToken").textContent).toBe("");
|
|
43
53
|
});
|
|
44
54
|
|
|
45
55
|
it("updates JWT using setJwt", () => {
|
|
@@ -57,30 +67,54 @@ describe("CrossmintProvider", () => {
|
|
|
57
67
|
expect(getByTestId("jwt").textContent).toBe("new_jwt");
|
|
58
68
|
});
|
|
59
69
|
|
|
60
|
-
it("updates
|
|
70
|
+
it("updates refreshToken using setRefreshToken", () => {
|
|
71
|
+
const TestComponent = () => {
|
|
72
|
+
const { crossmint, setRefreshToken } = useCrossmint();
|
|
73
|
+
return (
|
|
74
|
+
<div>
|
|
75
|
+
<div data-testid="refreshToken">{crossmint.refreshToken}</div>
|
|
76
|
+
<button onClick={() => setRefreshToken("new_refresh_token")}>Update Refresh Token</button>
|
|
77
|
+
</div>
|
|
78
|
+
);
|
|
79
|
+
};
|
|
80
|
+
const { getByTestId, getByText } = renderCrossmintProvider({ children: <TestComponent /> });
|
|
81
|
+
fireEvent.click(getByText("Update Refresh Token"));
|
|
82
|
+
expect(getByTestId("refreshToken").textContent).toBe("new_refresh_token");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("updates JWT and refreshToken using WalletSDK", () => {
|
|
61
86
|
const TestComponent = () => {
|
|
62
87
|
const { crossmint } = useCrossmint();
|
|
63
88
|
useEffect(() => {
|
|
64
89
|
const wallet = new MockSDK(crossmint);
|
|
65
90
|
wallet.somethingThatUpdatesJWT("sdk_jwt");
|
|
91
|
+
wallet.somethingThatUpdatesRefreshToken("sdk_refresh_token");
|
|
66
92
|
}, []);
|
|
67
|
-
return
|
|
93
|
+
return (
|
|
94
|
+
<div>
|
|
95
|
+
<div data-testid="jwt">{crossmint.jwt}</div>
|
|
96
|
+
<div data-testid="refreshToken">{crossmint.refreshToken}</div>
|
|
97
|
+
</div>
|
|
98
|
+
);
|
|
68
99
|
};
|
|
69
100
|
const { getByTestId } = renderCrossmintProvider({ children: <TestComponent /> });
|
|
70
101
|
expect(getByTestId("jwt").textContent).toBe("sdk_jwt");
|
|
102
|
+
expect(getByTestId("refreshToken").textContent).toBe("sdk_refresh_token");
|
|
71
103
|
});
|
|
72
104
|
|
|
73
|
-
it("triggers re-render on JWT change", () => {
|
|
105
|
+
it("triggers re-render on JWT and refreshToken change", () => {
|
|
74
106
|
const renderCount = vi.fn();
|
|
75
107
|
const TestComponent = () => {
|
|
76
|
-
const { crossmint, setJwt } = useCrossmint();
|
|
108
|
+
const { crossmint, setJwt, setRefreshToken } = useCrossmint();
|
|
77
109
|
useEffect(() => {
|
|
78
110
|
renderCount();
|
|
79
111
|
});
|
|
80
112
|
return (
|
|
81
113
|
<div>
|
|
82
114
|
<div data-testid="jwt">{crossmint.jwt}</div>
|
|
115
|
+
<div data-testid="refreshToken">{crossmint.refreshToken}</div>
|
|
83
116
|
<button onClick={() => setJwt("new_jwt")}>Update JWT</button>
|
|
117
|
+
<button onClick={() => setRefreshToken("new_refresh_token")}>Update Refresh Token</button>
|
|
84
118
|
</div>
|
|
85
119
|
);
|
|
86
120
|
};
|
|
@@ -90,7 +124,9 @@ describe("CrossmintProvider", () => {
|
|
|
90
124
|
expect(renderCount).toHaveBeenCalledTimes(1);
|
|
91
125
|
|
|
92
126
|
fireEvent.click(getByText("Update JWT"));
|
|
93
|
-
|
|
94
127
|
expect(renderCount).toHaveBeenCalledTimes(2);
|
|
128
|
+
|
|
129
|
+
fireEvent.click(getByText("Update Refresh Token"));
|
|
130
|
+
expect(renderCount).toHaveBeenCalledTimes(3);
|
|
95
131
|
});
|
|
96
132
|
});
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { ReactNode, createContext, useCallback, useContext, useMemo, useRef, useState } from "react";
|
|
1
|
+
import { type ReactNode, createContext, useCallback, useContext, useMemo, useRef, useState } from "react";
|
|
2
2
|
|
|
3
|
-
import { Crossmint, createCrossmint } from "@crossmint/common-sdk-base";
|
|
3
|
+
import { type Crossmint, createCrossmint } from "@crossmint/common-sdk-base";
|
|
4
4
|
|
|
5
5
|
export interface CrossmintContext {
|
|
6
6
|
crossmint: Crossmint;
|
|
7
7
|
setJwt: (jwt: string | undefined) => void;
|
|
8
|
+
setRefreshToken: (refreshToken: string | undefined) => void;
|
|
8
9
|
}
|
|
9
10
|
|
|
10
11
|
const CrossmintContext = createContext<CrossmintContext | null>(null);
|
|
@@ -24,6 +25,9 @@ export function CrossmintProvider({
|
|
|
24
25
|
if (prop === "jwt" && target.jwt !== value) {
|
|
25
26
|
setVersion((v) => v + 1);
|
|
26
27
|
}
|
|
28
|
+
if (prop === "refreshToken" && target.refreshToken !== value) {
|
|
29
|
+
setVersion((v) => v + 1);
|
|
30
|
+
}
|
|
27
31
|
return Reflect.set(target, prop, value);
|
|
28
32
|
},
|
|
29
33
|
})
|
|
@@ -35,14 +39,21 @@ export function CrossmintProvider({
|
|
|
35
39
|
}
|
|
36
40
|
}, []);
|
|
37
41
|
|
|
42
|
+
const setRefreshToken = useCallback((refreshToken: string | undefined) => {
|
|
43
|
+
if (refreshToken !== crossmintRef.current.refreshToken) {
|
|
44
|
+
crossmintRef.current.refreshToken = refreshToken;
|
|
45
|
+
}
|
|
46
|
+
}, []);
|
|
47
|
+
|
|
38
48
|
const value = useMemo(
|
|
39
49
|
() => ({
|
|
40
50
|
get crossmint() {
|
|
41
51
|
return crossmintRef.current;
|
|
42
52
|
},
|
|
43
53
|
setJwt,
|
|
54
|
+
setRefreshToken,
|
|
44
55
|
}),
|
|
45
|
-
[setJwt, version]
|
|
56
|
+
[setJwt, setRefreshToken, version]
|
|
46
57
|
);
|
|
47
58
|
|
|
48
59
|
return <CrossmintContext.Provider value={value}>{children}</CrossmintContext.Provider>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import isEqual from "lodash.isequal";
|
|
2
|
-
import { DependencyList, EffectCallback, useEffect, useRef } from "react";
|
|
2
|
+
import { type DependencyList, type EffectCallback, useEffect, useRef } from "react";
|
|
3
3
|
|
|
4
4
|
export default function useDeepEffect(callback: EffectCallback, dependencies: DependencyList): void {
|
|
5
5
|
const dependenciesRef = useRef(dependencies);
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { act, renderHook } from "@testing-library/react";
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { type CrossmintAuthService, getJWTExpiration } from "@crossmint/client-sdk-auth-core/client";
|
|
5
|
+
import { queueTask } from "@crossmint/client-sdk-base";
|
|
6
|
+
|
|
7
|
+
import * as authCookies from "../utils/authCookies";
|
|
8
|
+
import { type AuthMaterial, useRefreshToken } from "./useRefreshToken";
|
|
9
|
+
|
|
10
|
+
vi.mock("@crossmint/client-sdk-auth-core", () => ({
|
|
11
|
+
CrossmintAuthService: vi.fn(),
|
|
12
|
+
getJWTExpiration: vi.fn(),
|
|
13
|
+
}));
|
|
14
|
+
|
|
15
|
+
vi.mock("../utils/authCookies", () => ({
|
|
16
|
+
getCookie: vi.fn(),
|
|
17
|
+
REFRESH_TOKEN_PREFIX: "crossmint-refresh-token",
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
vi.mock("@crossmint/client-sdk-base", () => ({
|
|
21
|
+
queueTask: vi.fn(),
|
|
22
|
+
}));
|
|
23
|
+
|
|
24
|
+
describe("useRefreshToken", () => {
|
|
25
|
+
const mockCrossmintAuthService = {
|
|
26
|
+
refreshAuthMaterial: vi.fn(),
|
|
27
|
+
} as unknown as CrossmintAuthService;
|
|
28
|
+
|
|
29
|
+
const mockSetAuthMaterial = vi.fn();
|
|
30
|
+
|
|
31
|
+
beforeEach(() => {
|
|
32
|
+
vi.useFakeTimers();
|
|
33
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
afterEach(() => {
|
|
37
|
+
vi.restoreAllMocks();
|
|
38
|
+
vi.useRealTimers();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should not refresh token if refresh token is not present", async () => {
|
|
42
|
+
vi.mocked(authCookies.getCookie).mockReturnValue(undefined);
|
|
43
|
+
|
|
44
|
+
renderHook(() =>
|
|
45
|
+
useRefreshToken({
|
|
46
|
+
crossmintAuthService: mockCrossmintAuthService,
|
|
47
|
+
setAuthMaterial: mockSetAuthMaterial,
|
|
48
|
+
logout: vi.fn(),
|
|
49
|
+
})
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
await act(async () => {
|
|
53
|
+
await vi.runAllTimersAsync();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
expect(mockCrossmintAuthService.refreshAuthMaterial).not.toHaveBeenCalled();
|
|
57
|
+
expect(mockSetAuthMaterial).not.toHaveBeenCalled();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("should refresh token if refresh token is present", async () => {
|
|
61
|
+
const mockRefreshToken = "mock-refresh-token";
|
|
62
|
+
const mockAuthMaterial: AuthMaterial = {
|
|
63
|
+
jwtToken: "mock-jwt-token",
|
|
64
|
+
refreshToken: {
|
|
65
|
+
secret: "mock-secret",
|
|
66
|
+
expiresAt: "2023-04-01T00:00:00Z",
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
vi.mocked(authCookies.getCookie).mockReturnValue(mockRefreshToken);
|
|
71
|
+
vi.mocked(mockCrossmintAuthService.refreshAuthMaterial).mockResolvedValue(mockAuthMaterial);
|
|
72
|
+
vi.mocked(getJWTExpiration).mockReturnValue(Date.now() / 1000 + 3600); // 1 hour from now
|
|
73
|
+
|
|
74
|
+
renderHook(() =>
|
|
75
|
+
useRefreshToken({
|
|
76
|
+
crossmintAuthService: mockCrossmintAuthService,
|
|
77
|
+
setAuthMaterial: mockSetAuthMaterial,
|
|
78
|
+
logout: vi.fn(),
|
|
79
|
+
})
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
await act(async () => {
|
|
83
|
+
await vi.runAllTimersAsync();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
expect(mockCrossmintAuthService.refreshAuthMaterial).toHaveBeenCalledWith(mockRefreshToken);
|
|
87
|
+
expect(mockSetAuthMaterial).toHaveBeenCalledWith(mockAuthMaterial);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("should schedule next refresh before token expiration", async () => {
|
|
91
|
+
const mockRefreshToken = "mock-refresh-token";
|
|
92
|
+
const mockAuthMaterial: AuthMaterial = {
|
|
93
|
+
jwtToken: "mock-jwt-token",
|
|
94
|
+
refreshToken: {
|
|
95
|
+
secret: "mock-secret",
|
|
96
|
+
expiresAt: "2023-04-01T00:00:00Z",
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
vi.mocked(authCookies.getCookie).mockReturnValue(mockRefreshToken);
|
|
101
|
+
vi.mocked(mockCrossmintAuthService.refreshAuthMaterial).mockResolvedValue(mockAuthMaterial);
|
|
102
|
+
vi.mocked(getJWTExpiration).mockReturnValue(Date.now() / 1000 + 3600); // 1 hour from now
|
|
103
|
+
|
|
104
|
+
renderHook(() =>
|
|
105
|
+
useRefreshToken({
|
|
106
|
+
crossmintAuthService: mockCrossmintAuthService,
|
|
107
|
+
setAuthMaterial: mockSetAuthMaterial,
|
|
108
|
+
logout: vi.fn(),
|
|
109
|
+
})
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
await act(async () => {
|
|
113
|
+
await vi.runAllTimersAsync();
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
expect(vi.mocked(queueTask)).toHaveBeenCalledTimes(1);
|
|
117
|
+
expect(vi.mocked(queueTask)).toHaveBeenCalledWith(expect.any(Function), expect.any(Number));
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("should handle errors during token refresh", async () => {
|
|
121
|
+
const mockRefreshToken = "mock-refresh-token";
|
|
122
|
+
const mockError = new Error("Refresh failed");
|
|
123
|
+
|
|
124
|
+
vi.mocked(authCookies.getCookie).mockReturnValue(mockRefreshToken);
|
|
125
|
+
vi.mocked(mockCrossmintAuthService.refreshAuthMaterial).mockRejectedValue(mockError);
|
|
126
|
+
|
|
127
|
+
renderHook(() =>
|
|
128
|
+
useRefreshToken({
|
|
129
|
+
crossmintAuthService: mockCrossmintAuthService,
|
|
130
|
+
setAuthMaterial: mockSetAuthMaterial,
|
|
131
|
+
logout: vi.fn(),
|
|
132
|
+
})
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
await act(async () => {
|
|
136
|
+
await vi.runAllTimersAsync();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
expect(console.error).toHaveBeenCalledWith(mockError);
|
|
140
|
+
expect(mockSetAuthMaterial).not.toHaveBeenCalled();
|
|
141
|
+
});
|
|
142
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
import type { CrossmintAuthService } from "@crossmint/client-sdk-auth-core/client";
|
|
4
|
+
import { getJWTExpiration } from "@crossmint/client-sdk-auth-core/client";
|
|
5
|
+
import { queueTask, type CancellableTask } from "@crossmint/client-sdk-base";
|
|
6
|
+
|
|
7
|
+
import { REFRESH_TOKEN_PREFIX, getCookie } from "../utils/authCookies";
|
|
8
|
+
|
|
9
|
+
// 2 minutes before jwt expiration
|
|
10
|
+
const TIME_BEFORE_EXPIRING_JWT_IN_SECONDS = 120;
|
|
11
|
+
|
|
12
|
+
export type AuthMaterial = {
|
|
13
|
+
jwtToken: string;
|
|
14
|
+
refreshToken: {
|
|
15
|
+
secret: string;
|
|
16
|
+
expiresAt: string;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
type UseAuthTokenRefreshProps = {
|
|
21
|
+
crossmintAuthService: CrossmintAuthService;
|
|
22
|
+
setAuthMaterial: (authMaterial: AuthMaterial) => void;
|
|
23
|
+
logout: () => void;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export function useRefreshToken({ crossmintAuthService, setAuthMaterial, logout }: UseAuthTokenRefreshProps) {
|
|
27
|
+
const refreshTaskRef = useRef<CancellableTask | null>(null);
|
|
28
|
+
|
|
29
|
+
const refreshAuthMaterial = useCallback(async () => {
|
|
30
|
+
const refreshToken = getCookie(REFRESH_TOKEN_PREFIX);
|
|
31
|
+
if (refreshToken != null) {
|
|
32
|
+
try {
|
|
33
|
+
const result = await crossmintAuthService.refreshAuthMaterial(refreshToken);
|
|
34
|
+
setAuthMaterial(result);
|
|
35
|
+
const jwtExpiration = getJWTExpiration(result.jwtToken);
|
|
36
|
+
|
|
37
|
+
if (jwtExpiration == null) {
|
|
38
|
+
throw new Error("Invalid JWT");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const currentTime = Date.now() / 1000;
|
|
42
|
+
const timeToExpire = jwtExpiration - currentTime - TIME_BEFORE_EXPIRING_JWT_IN_SECONDS;
|
|
43
|
+
if (timeToExpire > 0) {
|
|
44
|
+
const endTime = Date.now() + timeToExpire * 1000;
|
|
45
|
+
refreshTaskRef.current = queueTask(refreshAuthMaterial, endTime);
|
|
46
|
+
}
|
|
47
|
+
} catch (error) {
|
|
48
|
+
logout();
|
|
49
|
+
console.error(error);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}, []);
|
|
53
|
+
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
refreshAuthMaterial();
|
|
56
|
+
return () => {
|
|
57
|
+
if (refreshTaskRef.current) {
|
|
58
|
+
refreshTaskRef.current.cancel();
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}, []);
|
|
62
|
+
}
|