updated the Google Flow UI part1
This commit is contained in:
parent
9703fa9155
commit
47d26f8c46
@ -1,152 +1,128 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { onMount, tick } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import { isSignInRedirect, getProviderFromUrl } from '@lit-protocol/lit-auth-client';
|
import { isSignInRedirect, getProviderFromUrl } from '@lit-protocol/lit-auth-client';
|
||||||
import type { IRelayPKP } from '@lit-protocol/types';
|
import type { IRelayPKP } from '@lit-protocol/types';
|
||||||
import { ProviderType } from '@lit-protocol/constants';
|
import { ProviderType } from '@lit-protocol/constants';
|
||||||
import { createMessage } from '$lib/services/messages/messages';
|
|
||||||
import { createLitSession } from '$lib/services/createLitSession/createLitSession';
|
|
||||||
import Icon from '@iconify/svelte';
|
import Icon from '@iconify/svelte';
|
||||||
|
import { createLitSession } from '$lib/services/createLitSession/createLitSession';
|
||||||
import Apps from '$lib/components/Apps.svelte';
|
import Apps from '$lib/components/Apps.svelte';
|
||||||
|
|
||||||
const redirectUri = 'http://localhost:5173/';
|
const redirectUri = 'http://localhost:5173/';
|
||||||
|
|
||||||
export let services;
|
export let services;
|
||||||
|
|
||||||
let view = 'sign_in';
|
let sessionSigs, error, currentPKP, authMethod, provider;
|
||||||
let sessionSigs;
|
let log = '';
|
||||||
let error;
|
|
||||||
let isLoading = false;
|
|
||||||
let currentPKP;
|
|
||||||
let pkps: IRelayPKP[] = [];
|
let pkps: IRelayPKP[] = [];
|
||||||
let authMethod;
|
let view = 'SIGN_IN';
|
||||||
let provider;
|
|
||||||
|
|
||||||
function logMessage(text, type = 'SYSTEM') {
|
onMount(() => {
|
||||||
console.log(text);
|
initialize();
|
||||||
createMessage({
|
|
||||||
text,
|
|
||||||
sender: '$lib/components/GoogleAuth.svelte',
|
|
||||||
type
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
onMount(async () => {
|
async function initialize() {
|
||||||
|
addToLog('Initialization...');
|
||||||
try {
|
try {
|
||||||
provider = await services.setupLit.connectProvider();
|
provider = await services.setupLit.connectProvider();
|
||||||
console.log('Provider: ' + provider);
|
addToLog('Connected to Google provider.');
|
||||||
|
if (isSignInRedirect(redirectUri)) {
|
||||||
logMessage('Component mounted.');
|
|
||||||
|
|
||||||
if (!authMethod && isSignInRedirect(redirectUri)) {
|
|
||||||
logMessage('Redirect detected, handling...');
|
|
||||||
const providerName = getProviderFromUrl();
|
const providerName = getProviderFromUrl();
|
||||||
if (providerName) {
|
if (providerName) {
|
||||||
await handleRedirect(providerName);
|
await handleRedirect(providerName);
|
||||||
} else {
|
|
||||||
logMessage('No provider detected in the URL.', 'ERROR');
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
logMessage('No redirect detected.');
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err);
|
setError(err);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
async function authWithGoogle() {
|
async function authWithGoogle() {
|
||||||
try {
|
try {
|
||||||
|
if (!provider) {
|
||||||
|
provider = await services.setupLit.connectProvider();
|
||||||
|
addToLog('Reconnected to Google provider.');
|
||||||
|
}
|
||||||
await provider.signIn();
|
await provider.signIn();
|
||||||
await handleRedirect(ProviderType.Google);
|
addToLog('Signed in with Google.');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err);
|
setError(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleRedirect(providerName: string) {
|
async function handleRedirect(providerName: string) {
|
||||||
logMessage(`Handling redirect for ${providerName}`);
|
try {
|
||||||
|
|
||||||
if (!provider) throw new Error('Invalid provider.');
|
if (!provider) throw new Error('Invalid provider.');
|
||||||
authMethod = await provider.authenticate();
|
authMethod = await provider.authenticate();
|
||||||
await tick();
|
addToLog('Authenticated successfully.');
|
||||||
|
|
||||||
view = 'fetching';
|
|
||||||
pkps = await provider.fetchPKPsThroughRelayer(authMethod);
|
pkps = await provider.fetchPKPsThroughRelayer(authMethod);
|
||||||
view = pkps.length > 0 ? 'fetched' : 'NO_PKPS';
|
addToLog('Fetched PKPs.');
|
||||||
|
if (pkps.length === 0) {
|
||||||
logMessage(`Fetched PKPs: ${pkps.length}`);
|
addToLog('No PKPs found. Minting...');
|
||||||
|
await mint();
|
||||||
|
} else {
|
||||||
|
// Update the view to 'PKP' to show the available PKPs
|
||||||
|
view = 'PKP';
|
||||||
}
|
}
|
||||||
|
|
||||||
async function mint() {
|
|
||||||
try {
|
|
||||||
view = 'MINTING';
|
|
||||||
const newPKP: IRelayPKP = await mintPkp(provider, authMethod);
|
|
||||||
pkps = [...pkps, newPKP];
|
|
||||||
await createSession(newPKP);
|
|
||||||
view = 'MINTED';
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err);
|
setError(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function mint() {
|
||||||
|
const newPKP: IRelayPKP = await mintPkp(provider, authMethod);
|
||||||
|
pkps = [...pkps, newPKP];
|
||||||
|
addToLog('New PKP minted.');
|
||||||
|
await createSession(newPKP);
|
||||||
|
}
|
||||||
|
|
||||||
async function createSession(pkp: IRelayPKP) {
|
async function createSession(pkp: IRelayPKP) {
|
||||||
logMessage('Creating session...');
|
try {
|
||||||
view = 'CREATING_SESSION';
|
currentPKP = pkp; // Assign the selected PKP to currentPKP
|
||||||
|
|
||||||
sessionSigs = await createLitSession(provider, pkp.publicKey, authMethod);
|
sessionSigs = await createLitSession(provider, pkp.publicKey, authMethod);
|
||||||
currentPKP = pkp;
|
addToLog('Session created successfully.');
|
||||||
|
view = 'READY';
|
||||||
logMessage('Session created.');
|
} catch (err) {
|
||||||
view = 'SESSION_CREATED';
|
setError(err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function setError(err) {
|
function setError(err) {
|
||||||
console.error(err);
|
|
||||||
error = err;
|
error = err;
|
||||||
view = 'ERROR';
|
view = 'ERROR';
|
||||||
|
addToLog(`Error: ${err.message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function resetView() {
|
function addToLog(message: string) {
|
||||||
view = authMethod ? 'fetched' : 'sign_in';
|
log += `${message}\n`;
|
||||||
error = null;
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<main class="flex items-center justify-center h-full">
|
<div class="flex items-center justify-center h-screen">
|
||||||
<div class="text-center">
|
<div>
|
||||||
{#if error}
|
{#if error}
|
||||||
|
<div class="mt-4 text-center">
|
||||||
<h1>Error</h1>
|
<h1>Error</h1>
|
||||||
<p>{error.message}</p>
|
<p>{error.message}</p>
|
||||||
<button on:click={resetView}>Got it</button>
|
</div>
|
||||||
{:else if isLoading}
|
{:else if view === 'SIGN_IN'}
|
||||||
<h1>Loading...</h1>
|
|
||||||
{:else if view === 'sign_in'}
|
|
||||||
<button on:click={authWithGoogle} class="btn variant-filled">
|
<button on:click={authWithGoogle} class="btn variant-filled">
|
||||||
<span><Icon icon="flat-color-icons:google" /></span>
|
<span><Icon icon="flat-color-icons:google" /></span>
|
||||||
<span>Sign in with Google</span>
|
<span>Sign in with Google</span>
|
||||||
</button>
|
</button>
|
||||||
{:else if view === 'fetching'}
|
{:else if view === 'PKP'}
|
||||||
<h1>Fetching your PKPs...</h1>
|
<div>
|
||||||
{:else if view === 'NO_PKPS'}
|
|
||||||
<h1>No PKPs found.</h1>
|
|
||||||
<button on:click={mint}>Mint a PKP</button>
|
|
||||||
{:else if view === 'fetched'}
|
|
||||||
<h1>Select a PKP to continue</h1>
|
<h1>Select a PKP to continue</h1>
|
||||||
{#each pkps as pkp}
|
{#each pkps as pkp}
|
||||||
<button on:click={async () => await createSession(pkp)}>{pkp.ethAddress}</button>
|
<button on:click={() => createSession(pkp)}>{pkp.ethAddress}</button>
|
||||||
{/each}
|
{/each}
|
||||||
{:else if view === 'MINTING'}
|
</div>
|
||||||
<h1>Minting your PKP...</h1>
|
{:else if view === 'READY'}
|
||||||
{:else if view === 'MINTED'}
|
|
||||||
<h1>Minted!</h1>
|
|
||||||
{:else if view === 'CREATING_SESSION'}
|
|
||||||
<h1>Creating session...</h1>
|
|
||||||
{:else if view === 'SESSION_CREATED'}
|
|
||||||
<h1>Ready for the open web</h1>
|
|
||||||
<div>
|
<div>
|
||||||
|
<h1>Ready for the open web</h1>
|
||||||
<p>Check out your PKP:</p>
|
<p>Check out your PKP:</p>
|
||||||
<p>{currentPKP.ethAddress}</p>
|
<p>{currentPKP.ethAddress}</p>
|
||||||
</div>
|
|
||||||
<Apps />
|
<Apps />
|
||||||
{/if}
|
|
||||||
</div>
|
</div>
|
||||||
</main>
|
{/if}
|
||||||
|
<pre class="mt-4">{log}</pre>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user