further reducing google auth example

This commit is contained in:
Samuel Andert 2023-07-21 10:11:29 +02:00
parent c71f37d221
commit 8d9980c59e

View File

@ -12,9 +12,6 @@ import { IRelayPKP, AuthMethod, SessionSigs } from '@lit-protocol/types';
import { ProviderType } from '@lit-protocol/constants'; import { ProviderType } from '@lit-protocol/constants';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import {LitAccessControlConditionResource, LitAbility} from '@lit-protocol/auth-helpers'; import {LitAccessControlConditionResource, LitAbility} from '@lit-protocol/auth-helpers';
import { useConnect, useAccount, useDisconnect, Connector } from 'wagmi';
enum Views { enum Views {
SIGN_IN = 'sign_in', SIGN_IN = 'sign_in',
@ -29,49 +26,31 @@ enum Views {
ERROR = 'error', ERROR = 'error',
} }
export default function Dashboard() { export default function Dashboard() {
const redirectUri = 'http://localhost:3000'; const redirectUri = 'http://localhost:3000';
const router = useRouter(); const router = useRouter();
const [view, setView] = useState<Views>(Views.SIGN_IN); const [view, setView] = useState<Views>(Views.SIGN_IN);
const [error, setError] = useState<any>(); const [error, setError] = useState<any>();
const [litAuthClient, setLitAuthClient] = useState<LitAuthClient>(); const [litAuthClient, setLitAuthClient] = useState<LitAuthClient>();
const [litNodeClient, setLitNodeClient] = useState<LitNodeClient>(); const [litNodeClient, setLitNodeClient] = useState<LitNodeClient>();
const [currentProviderType, setCurrentProviderType] = const [currentProviderType, setCurrentProviderType] = useState<ProviderType>();
useState<ProviderType>();
const [authMethod, setAuthMethod] = useState<AuthMethod>(); const [authMethod, setAuthMethod] = useState<AuthMethod>();
const [pkps, setPKPs] = useState<IRelayPKP[]>([]); const [pkps, setPKPs] = useState<IRelayPKP[]>([]);
const [currentPKP, setCurrentPKP] = useState<IRelayPKP>(); const [currentPKP, setCurrentPKP] = useState<IRelayPKP>();
const [sessionSigs, setSessionSigs] = useState<SessionSigs>(); const [sessionSigs, setSessionSigs] = useState<SessionSigs>();
const { connectAsync, connectors } = useConnect({
onError(error) {
console.error(error);
setError(error);
},
});
const { isConnected, connector, address } = useAccount();
const { disconnectAsync } = useDisconnect();
/**
* Begin auth flow with Google
*/
async function authWithGoogle() { async function authWithGoogle() {
setCurrentProviderType(ProviderType.Google); setCurrentProviderType(ProviderType.Google);
const provider = litAuthClient.initProvider<GoogleProvider>( const provider = litAuthClient.initProvider<GoogleProvider>(ProviderType.Google);
ProviderType.Google
);
await provider.signIn(); await provider.signIn();
} }
/** /**
* Handle redirect from Lit login server * Handle redirect from Lit login server
*/ */
const handleRedirect = useCallback( const handleRedirect = useCallback(async (providerName: string) => {
async (providerName: string) => {
setView(Views.HANDLE_REDIRECT); setView(Views.HANDLE_REDIRECT);
try { try {
// Get relevant provider // Get relevant provider
@ -228,7 +207,6 @@ export default function Dashboard() {
if (!litNodeClient) { if (!litNodeClient) {
return null; return null;
} }
return ( return (
<> <>
<Head> <Head>
@ -266,26 +244,7 @@ export default function Dashboard() {
{view === Views.SIGN_IN && ( {view === Views.SIGN_IN && (
<> <>
<h1>Sign in with Lit</h1> <h1>Sign in with Lit</h1>
{/* Since eth wallet is connected, prompt user to sign a message or disconnect their wallet */} <button onClick={authWithGoogle}>Google</button>
<>
{isConnected ? (
<>
<button
onClick={async () => {
setError(null);
await disconnectAsync();
}}
>
Disconnect wallet
</button>
</>
) : (
<>
<button onClick={authWithGoogle}>Google</button>
</>
)}
</>
</> </>
)} )}
@ -340,12 +299,10 @@ export default function Dashboard() {
<div> <div>
<p>Check out your PKP:</p> <p>Check out your PKP:</p>
<p>{currentPKP.ethAddress}</p> <p>{currentPKP.ethAddress}</p>
</div> </div>
<hr></hr>
</> </>
)} )}
</main> </main>
</> </>
); );
} }