Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…th-web into feat/passkeys-login
  • Loading branch information
arch1995 committed Apr 15, 2024
2 parents 81a57a3 + 2b9692a commit 4bc4234
Show file tree
Hide file tree
Showing 9 changed files with 2,035 additions and 14,738 deletions.
58 changes: 29 additions & 29 deletions example/react-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 9 additions & 28 deletions example/react-app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useState } from "react";

// Import Single Factor Auth SDK for no redirect flow
import { Web3Auth, ADAPTER_EVENTS } from "@web3auth/single-factor-auth";
import { Web3Auth, ADAPTER_EVENTS, decodeToken } from "@web3auth/single-factor-auth";
import { CHAIN_NAMESPACES } from "@web3auth/base";
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider";

Expand Down Expand Up @@ -43,7 +43,6 @@ const firebaseConfig = {

function App() {
const [web3authSFAuth, setWeb3authSFAuth] = useState<Web3Auth | null>(null);
const [usesSfaSDK, setUsesSfaSDK] = useState(false);
const [provider, setProvider] = useState<IProvider | null>(null);
// const [idToken, setIdToken] = useState<string | null>(null);
const [isLoggingIn, setIsLoggingIn] = useState(false);
Expand All @@ -62,7 +61,6 @@ function App() {
console.log("sfa:connected", data);
console.log("sfa:state", web3authSfa?.state);
setProvider(web3authSfa.provider);
setUsesSfaSDK(true);
});
web3authSfa.on(ADAPTER_EVENTS.DISCONNECTED, () => {
console.log("sfa:disconnected");
Expand Down Expand Up @@ -92,17 +90,6 @@ function App() {
}
};

const parseToken = (token: any) => {
try {
const base64Url = token.split(".")[1];
const base64 = base64Url.replace("-", "+").replace("_", "/");
return JSON.parse(window.atob(base64 || ""));
} catch (err) {
console.error(err);
return null;
}
};

const login = async () => {
setIsLoggingIn(true);
// login with firebase
Expand All @@ -118,19 +105,18 @@ function App() {
return;
}
// get sub value from firebase id token
const { sub } = parseToken(token);
const { payload } = decodeToken(token);

const web3authSfaprovider = await web3authSFAuth.connect({
verifier,
verifierId: sub,
verifierId: payload.sub,
idToken: token,
});
if (web3authSfaprovider) {
setProvider(web3authSfaprovider);
const privKey = await web3authSfaprovider?.request({ method: "eth_private_key" });
console.log(privKey);
}
setUsesSfaSDK(true);
setIsLoggingIn(false);
} catch (err) {
// Single Factor Auth SDK throws an error if the user has already enabled MFA
Expand Down Expand Up @@ -185,18 +171,13 @@ function App() {
};

const logout = async () => {
if (usesSfaSDK) {
if (!web3authSFAuth) {
throw new Error("web3auth sfa auth not initialized.");
}
console.log(
"You are directly using Single Factor Auth SDK to login the user, hence the Web3Auth logout function won't work for you. You can logout the user directly from your login provider, or just clear the provider object."
);
await web3authSFAuth.logout();
const provider = new EthereumPrivateKeyProvider({ config: { chainConfig } });
await web3authSFAuth.init(provider);
return;
if (!web3authSFAuth) {
throw new Error("web3auth sfa auth not initialized.");
}
await web3authSFAuth.logout();
const provider = new EthereumPrivateKeyProvider({ config: { chainConfig } });
await web3authSFAuth.init(provider);
return;
};

const getAccounts = async () => {
Expand Down
Loading

0 comments on commit 4bc4234

Please sign in to comment.