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