UNPKG

@salad-labs/loopz-typescript

Version:
101 lines 4.43 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { useLogin, usePrivy } from "@privy-io/react-auth"; import { Auth } from "../../auth"; import { useEffect, useRef } from "react"; import { useFundWallet } from "@privy-io/react-auth"; export const usePrivyLogin = () => { const auth = Auth.getInstance(); const initialized = useRef(false); const { ready, authenticated, getAccessToken } = usePrivy(); const disableLogin = !ready || (ready && authenticated); useFundWallet({ onUserExited: (fundInfo) => { var _a; (_a = auth.getCurrentAccount()) === null || _a === void 0 ? void 0 : _a._emit("onFundExit", fundInfo); }, }); const { login } = useLogin({ onComplete: (_a) => __awaiter(void 0, [_a], void 0, function* ({ user, isNewUser, wasAlreadyAuthenticated, loginMethod, loginAccount, }) { const authToken = yield getAccessToken(); //need to try farcaster and telegram. LOO-37 if (loginMethod === "apple" || loginMethod === "discord" || loginMethod === "github" || loginMethod === "google" || loginMethod === "instagram" || loginMethod === "linkedin" || loginMethod === "spotify" || loginMethod === "tiktok" || loginMethod === "twitter" || loginMethod === "farcaster" || loginMethod === "telegram") //these services brings the user out of the current web page, so we should listen this event when the Auth object boots Auth._emit("__onOAuthAuthenticatedDesktop", { user, isNewUser, wasAlreadyAuthenticated, loginMethod, linkedAccount: loginAccount, authToken, }); else Auth._emit("__onLoginComplete", { user, isNewUser, wasAlreadyAuthenticated, loginMethod, linkedAccount: loginAccount, authToken, }); }), onError: (error) => { Auth._emit("__onLoginError", error); }, }); useEffect(() => { if (!initialized.current && ready && !disableLogin) { initialized.current = true; //__authenticate fires after the __onLoginComplete & __onLoginError are added in events queue. //__authenticate fires in authenticate() method of the auth object. auth.on("__authenticate", () => { login(); }); Auth._emit("__onPrivyReady"); } console.log(authenticated, ready); auth.on("__onAccountReady", () => { console.log("emitting auth..."); Auth._emit("auth"); }, true); //account is setup when the client did the login or after the refresh of the page it has rebuilt the account //object (and it can do it only if it has a jwt token valid) if (authenticated && ready) { Auth._emit("__tryRebuildAccountOnRefresh"); } else if (!authenticated && ready) { auth.logout(); /** * //to prevent loop (async () => { if (auth.isAuthenticated()) { console.log("client logout..."); await auth.logout(); } })(); * */ } else if (!ready) { console.log("client is not ready at all!"); } }, [ready, disableLogin, authenticated]); }; //# sourceMappingURL=usePrivyLogin.js.map