UNPKG

ic-auth

Version:

A simple to use, modular package for integrating Internet Computer authentication providers into your app.

69 lines (68 loc) 3.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NFID = void 0; const react_1 = require("react"); const principal_1 = require("@dfinity/principal"); const auth_client_1 = require("@dfinity/auth-client"); const agent_1 = require("@dfinity/agent"); const helloIDL = require("../interfaces/hello"); const nfid_png_1 = require("../assets/logos/nfid.png"); require("../assets/index.css"); function NFID(props) { const changeProvider = props.changeProvider; const [nfidButtonText, setnfidButtonText] = (0, react_1.useState)("NFID"); const [loggedIn, setLoggedIn] = (0, react_1.useState)(false); const buttonState = (0, react_1.useRef)(null); const nfidStatus = (0, react_1.useRef)(null); const manageLogin = async () => { let identity; let actor; const appName = "wallet-testing"; const appLogo = "https://nfid.one/icons/favicon-96x96.png"; const authPath = "/authenticate/?applicationName=" + appName + "&applicationLogo=" + appLogo + "#authorize"; const authUrl = "https://nfid.one" + authPath; const authClient = await auth_client_1.AuthClient.create(); await authClient.login({ identityProvider: authUrl, onSuccess: async () => { identity = await authClient.getIdentity(); const theUserPrincipal = principal_1.Principal.from(identity.getPrincipal()).toText(); changeProvider(theUserPrincipal); nfidStatus.current.style.backgroundColor = "#42ff0f"; setnfidButtonText("Connected!"); setLoggedIn(true); buttonState.current.disabled = true; actor = await createInternetIdentityActor(identity); try { const result = await actor.hello_world(); console.log(result); } catch (error) { console.log(error); return; } }, onError: async (error) => { console.log("Login Failed:\n\n" + error); return "Error"; }, }); }; const createInternetIdentityActor = async (user) => { let actor; const identity = user; const host = "https://ic0.app"; const idlFactory = helloIDL.idlFactory; actor = await agent_1.Actor.createActor(idlFactory, { agent: new agent_1.HttpAgent({ identity, host }), canisterId: "oyjva-2yaaa-aaaam-qbaya-cai" }); return actor; }; return (react_1.default.createElement("div", { className: "walletContainer" }, react_1.default.createElement("button", { ref: buttonState, onClick: !loggedIn ? manageLogin : undefined, id: 'nfidMenu' }, react_1.default.createElement("img", { src: nfid_png_1.default }), react_1.default.createElement("p", null, nfidButtonText), react_1.default.createElement("div", { ref: nfidStatus, className: 'statusBubble', id: 'statusBubble' })))); } exports.NFID = NFID;