Skip to content

Commit f778d30

Browse files
committed
[React] Feature: Export web getLastAuthProvider (#5697)
<!-- start pr-codex --> ## PR-Codex overview This PR introduces a new utility function `getLastAuthProvider` to retrieve the most recently used authentication provider from local storage. It also exports the `LAST_AUTH_PROVIDER_STORAGE_KEY` for consistent access. ### Detailed summary - Added `getLastAuthProvider` function in `packages/thirdweb/src/react/web/utils/storage.ts`. - Exported `LAST_AUTH_PROVIDER_STORAGE_KEY` from `packages/thirdweb/src/react/core/utils/storage.ts`. - Created tests for `getLastAuthProvider` in `packages/thirdweb/src/react/web/utils/storage.test.ts`. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent 4831926 commit f778d30

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

.changeset/warm-students-breathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": minor
3+
---
4+
5+
Adds `getLastAuthProvider` to get the most recently used auth provider for login

packages/thirdweb/src/exports/react.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,6 @@ export {
269269
ChainIcon,
270270
type ChainIconProps,
271271
} from "../react/web/ui/prebuilt/Chain/icon.js";
272+
273+
// Utils
274+
export { getLastAuthProvider } from "../react/web/utils/storage.js";

packages/thirdweb/src/react/core/utils/storage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { AsyncStorage } from "../../../utils/storage/AsyncStorage.js";
22
import type { AuthArgsType } from "../../../wallets/in-app/core/authentication/types.js";
33

4-
const LAST_AUTH_PROVIDER_STORAGE_KEY = "lastAuthProvider";
4+
export const LAST_AUTH_PROVIDER_STORAGE_KEY = "lastAuthProvider";
55

66
export async function setLastAuthProvider(
77
authProvider: AuthArgsType["strategy"],
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { webLocalStorage } from "../../../utils/storage/webStorage.js";
3+
import { LAST_AUTH_PROVIDER_STORAGE_KEY } from "../../core/utils/storage.js";
4+
import { getLastAuthProvider } from "./storage.js";
5+
6+
vi.mock("../../../utils/storage/webStorage.js", () => ({
7+
webLocalStorage: {
8+
getItem: vi.fn().mockResolvedValueOnce("mockStrategy"),
9+
},
10+
}));
11+
12+
describe("getLastAuthProvider", () => {
13+
it("should return the last authentication provider strategy if it exists", async () => {
14+
const mockStrategy = "mockStrategy";
15+
webLocalStorage.getItem = vi.fn().mockResolvedValueOnce(mockStrategy);
16+
17+
const result = await getLastAuthProvider();
18+
expect(result).toBe(mockStrategy);
19+
});
20+
21+
it("should return null if no authentication provider strategy is found", async () => {
22+
webLocalStorage.getItem = vi.fn().mockResolvedValueOnce(null);
23+
24+
const result = await getLastAuthProvider();
25+
expect(result).toBeNull();
26+
});
27+
28+
it("should call webLocalStorage.getItem with the correct key", async () => {
29+
await getLastAuthProvider();
30+
expect(webLocalStorage.getItem).toHaveBeenCalledWith(
31+
LAST_AUTH_PROVIDER_STORAGE_KEY,
32+
);
33+
});
34+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { webLocalStorage } from "../../../utils/storage/webStorage.js";
2+
import { getLastAuthProvider as getLastAuthProviderCore } from "../../core/utils/storage.js";
3+
4+
/**
5+
* Retrieves the last authentication provider used from local storage.
6+
*
7+
* This function is designed to work only in a browser environment.
8+
*
9+
* @returns {Promise<AuthArgsType["strategy"] | null>} A promise that resolves to the last
10+
* authentication provider strategy used, or `null` if none is found.
11+
*
12+
* @example
13+
* ```typescript
14+
* import { getLastAuthProvider } from "thirdweb/react";
15+
*
16+
* const lastAuthProvider = await getLastAuthProvider();
17+
* ```
18+
*
19+
* @utils
20+
*/
21+
export async function getLastAuthProvider() {
22+
return getLastAuthProviderCore(webLocalStorage);
23+
}

0 commit comments

Comments
 (0)