terry-staging #16
@@ -6,7 +6,7 @@ import {
|
|||||||
} from "./injected";
|
} from "./injected";
|
||||||
import { localWalletToken, useWallet } from "./WalletProvider";
|
import { localWalletToken, useWallet } from "./WalletProvider";
|
||||||
|
|
||||||
const AUTO_LOGIN_PARAM = "autoLogin";
|
const AUTO_LOGIN_PARAMS = ["autoLogin", "autologin"];
|
||||||
const ETHEREUM_WAIT_MS = 8000;
|
const ETHEREUM_WAIT_MS = 8000;
|
||||||
const ETHEREUM_POLL_MS = 200;
|
const ETHEREUM_POLL_MS = 200;
|
||||||
|
|
||||||
@@ -17,9 +17,17 @@ function parseKind(value: string | null): WalletKind | null {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function autoLoginKindFromParams(params: URLSearchParams): WalletKind | null {
|
||||||
|
for (const key of AUTO_LOGIN_PARAMS) {
|
||||||
|
const kind = parseKind(params.get(key));
|
||||||
|
if (kind) return kind;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
function stripAutoLoginParam(): void {
|
function stripAutoLoginParam(): void {
|
||||||
const url = new URL(window.location.href);
|
const url = new URL(window.location.href);
|
||||||
url.searchParams.delete(AUTO_LOGIN_PARAM);
|
for (const key of AUTO_LOGIN_PARAMS) url.searchParams.delete(key);
|
||||||
const qs = url.searchParams.toString();
|
const qs = url.searchParams.toString();
|
||||||
const next = url.pathname + (qs ? `?${qs}` : "") + url.hash;
|
const next = url.pathname + (qs ? `?${qs}` : "") + url.hash;
|
||||||
window.history.replaceState({}, "", next);
|
window.history.replaceState({}, "", next);
|
||||||
@@ -49,7 +57,7 @@ export function AutoInjectedLogin() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (typeof window === "undefined") return;
|
if (typeof window === "undefined") return;
|
||||||
const params = new URLSearchParams(window.location.search);
|
const params = new URLSearchParams(window.location.search);
|
||||||
const kind = parseKind(params.get(AUTO_LOGIN_PARAM));
|
const kind = autoLoginKindFromParams(params);
|
||||||
if (!kind) return;
|
if (!kind) return;
|
||||||
|
|
||||||
stripAutoLoginParam();
|
stripAutoLoginParam();
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import {
|
|||||||
import { localWalletToken, useWallet } from "./WalletProvider";
|
import { localWalletToken, useWallet } from "./WalletProvider";
|
||||||
import { WalletBrandIcon } from "./WalletBrandIcon";
|
import { WalletBrandIcon } from "./WalletBrandIcon";
|
||||||
|
|
||||||
const AUTO_LOGIN_PARAM = "autoLogin";
|
const AUTO_LOGIN_PARAM = "autologin";
|
||||||
|
|
||||||
function supportsDirectPull(kind: WalletKind): boolean {
|
function supportsDirectPull(kind: WalletKind): boolean {
|
||||||
return kind === "tokenPocket" || kind === "imToken";
|
return kind === "tokenPocket" || kind === "imToken";
|
||||||
@@ -97,6 +97,10 @@ export function WalletLoginModal() {
|
|||||||
setPendingLogin(null);
|
setPendingLogin(null);
|
||||||
try {
|
try {
|
||||||
const address = await connectInjectedWallet(kind);
|
const address = await connectInjectedWallet(kind);
|
||||||
|
if (mobileDevice) {
|
||||||
|
completeLogin(localWalletToken(address), address);
|
||||||
|
return;
|
||||||
|
}
|
||||||
setPendingLogin({ kind, address });
|
setPendingLogin({ kind, address });
|
||||||
setState("idle");
|
setState("idle");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -98,20 +98,23 @@ export function WalletProvider({ children }: { children: ReactNode }) {
|
|||||||
};
|
};
|
||||||
}, [token]);
|
}, [token]);
|
||||||
|
|
||||||
const completeLogin = useCallback((nextToken: string, wallet: string) => {
|
const completeLogin = useCallback(
|
||||||
writeWalletToken(nextToken);
|
(nextToken: string, wallet: string) => {
|
||||||
setToken(nextToken);
|
writeWalletToken(nextToken);
|
||||||
setAddress(wallet);
|
setToken(nextToken);
|
||||||
setStatus("loggedIn");
|
setAddress(wallet);
|
||||||
setLoginModalOpen(false);
|
setStatus("loggedIn");
|
||||||
}, []);
|
setLoginModalOpen(false);
|
||||||
|
showToast(t("walletLoginSuccess"));
|
||||||
|
},
|
||||||
|
[showToast, t],
|
||||||
|
);
|
||||||
|
|
||||||
const signInInjected = useCallback(
|
const signInInjected = useCallback(
|
||||||
async (kind?: WalletKind) => {
|
async (kind?: WalletKind) => {
|
||||||
try {
|
try {
|
||||||
const res = await signInWithInjectedWallet(kind);
|
const res = await signInWithInjectedWallet(kind);
|
||||||
completeLogin(res.token, res.wallet);
|
completeLogin(res.token, res.wallet);
|
||||||
showToast(t("walletLoginSuccess"));
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
showToast(walletErrorMessage(error, t), "error");
|
showToast(walletErrorMessage(error, t), "error");
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
@@ -137,6 +137,11 @@ export function getInjectedEthereum(): EthereumProvider | null {
|
|||||||
return maybeWindow.ethereum ?? null;
|
return maybeWindow.ethereum ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isTokenPocketBrowser(): boolean {
|
||||||
|
if (typeof navigator === "undefined") return false;
|
||||||
|
return /tokenpocket|tpwallet/i.test(navigator.userAgent || "");
|
||||||
|
}
|
||||||
|
|
||||||
export function isImTokenBrowser(): boolean {
|
export function isImTokenBrowser(): boolean {
|
||||||
if (typeof navigator === "undefined") return false;
|
if (typeof navigator === "undefined") return false;
|
||||||
return /imtoken/i.test(navigator.userAgent || "");
|
return /imtoken/i.test(navigator.userAgent || "");
|
||||||
@@ -156,6 +161,10 @@ export function getInjectedWallet(kind?: WalletKind): EthereumProvider | null {
|
|||||||
});
|
});
|
||||||
if (match) return match;
|
if (match) return match;
|
||||||
|
|
||||||
|
if (kind === "tokenPocket" && isTokenPocketBrowser()) {
|
||||||
|
return providers[0] ?? ethereum;
|
||||||
|
}
|
||||||
|
|
||||||
if (kind === "imToken" && isImTokenBrowser()) return providers[0] ?? ethereum;
|
if (kind === "imToken" && isImTokenBrowser()) return providers[0] ?? ethereum;
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user