refactoring step1 GoogleAuth
This commit is contained in:
@ -25,11 +25,70 @@
|
||||
let sessionSigs;
|
||||
let isLoading = false;
|
||||
|
||||
let provider: BaseProvider | undefined;
|
||||
|
||||
onMount(async () => {
|
||||
isLoading = true;
|
||||
console.log('Component mounted.');
|
||||
createMessage({
|
||||
text: 'Component mounted.',
|
||||
sender: '$lib/components/GoogleAuth.svelte',
|
||||
type: 'SYSTEM'
|
||||
});
|
||||
|
||||
try {
|
||||
litNodeClient = new LitNodeClient({
|
||||
litNetwork: 'serrano',
|
||||
debug: false
|
||||
});
|
||||
await litNodeClient.connect();
|
||||
|
||||
litAuthClient = new LitAuthClient({
|
||||
litRelayConfig: {
|
||||
relayApiKey: 'test-api-key'
|
||||
},
|
||||
litNodeClient
|
||||
});
|
||||
|
||||
// Globally set the provider during the component's mount
|
||||
provider = litAuthClient.initProvider<GoogleProvider>(ProviderType.Google);
|
||||
|
||||
console.log('Checking if isSignInRedirect...');
|
||||
if (!authMethod && isSignInRedirect(redirectUri)) {
|
||||
console.log('Redirect detected, handling...');
|
||||
createMessage({
|
||||
text: 'Redirect detected, handling...',
|
||||
sender: '$lib/components/GoogleAuth.svelte',
|
||||
type: 'SYSTEM'
|
||||
});
|
||||
|
||||
const providerName = getProviderFromUrl();
|
||||
if (providerName) {
|
||||
await handleRedirect(providerName);
|
||||
} else {
|
||||
console.error('No provider detected in the URL.');
|
||||
}
|
||||
} else {
|
||||
console.log('No redirect detected.');
|
||||
createMessage({
|
||||
text: 'No redirect detected.',
|
||||
sender: '$lib/components/GoogleAuth.svelte',
|
||||
type: 'SYSTEM'
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
error = err;
|
||||
view = 'ERROR';
|
||||
} finally {
|
||||
isLoading = false;
|
||||
}
|
||||
});
|
||||
|
||||
async function authWithGoogle() {
|
||||
isLoading = true;
|
||||
error = null;
|
||||
try {
|
||||
const provider = litAuthClient.initProvider<GoogleProvider>(ProviderType.Google);
|
||||
await provider.signIn();
|
||||
await handleRedirect(ProviderType.Google);
|
||||
} catch (err) {
|
||||
@ -47,20 +106,17 @@
|
||||
sender: '$lib/components/GoogleAuth.svelte',
|
||||
type: 'SYSTEM'
|
||||
});
|
||||
|
||||
// As we're now using the global provider, we no longer need to fetch it again based on the providerName
|
||||
currentProviderType = providerName as ProviderType;
|
||||
|
||||
try {
|
||||
console.log('Handling redirect...');
|
||||
let provider: BaseProvider | undefined;
|
||||
|
||||
if (providerName === ProviderType.Google) {
|
||||
provider = litAuthClient.getProvider(ProviderType.Google);
|
||||
}
|
||||
|
||||
if (!provider) {
|
||||
throw new Error('Invalid provider.');
|
||||
}
|
||||
|
||||
currentProviderType = providerName as ProviderType;
|
||||
|
||||
// Get auth method object that has the OAuth token from redirect callback
|
||||
authMethod = await provider.authenticate();
|
||||
await tick(); // Allow Svelte to update the view
|
||||
@ -91,6 +147,7 @@
|
||||
type: 'SYSTEM'
|
||||
});
|
||||
}
|
||||
|
||||
async function mint() {
|
||||
isLoading = true;
|
||||
error = null;
|
||||
@ -103,7 +160,7 @@
|
||||
});
|
||||
view = 'MINTING';
|
||||
|
||||
const provider = litAuthClient.getProvider(currentProviderType);
|
||||
// Using the global provider variable, so no need to call getProvider again
|
||||
const txHash: string = await provider.mintPKPThroughRelayer(authMethod);
|
||||
const response = await provider.relay.pollRequestUntilTerminalState(txHash);
|
||||
if (response.status !== 'Succeeded') {
|
||||
@ -180,60 +237,6 @@
|
||||
view = 'SESSION_CREATED';
|
||||
}
|
||||
|
||||
onMount(async () => {
|
||||
isLoading = true;
|
||||
console.log('Component mounted.');
|
||||
createMessage({
|
||||
text: 'Component mounted.',
|
||||
sender: '$lib/components/GoogleAuth.svelte',
|
||||
type: 'SYSTEM'
|
||||
});
|
||||
try {
|
||||
litNodeClient = new LitNodeClient({
|
||||
litNetwork: 'serrano',
|
||||
debug: false
|
||||
});
|
||||
await litNodeClient.connect();
|
||||
|
||||
litAuthClient = new LitAuthClient({
|
||||
litRelayConfig: {
|
||||
relayApiKey: 'test-api-key'
|
||||
},
|
||||
litNodeClient
|
||||
});
|
||||
|
||||
litAuthClient.initProvider<GoogleProvider>(ProviderType.Google);
|
||||
|
||||
console.log('Checking if isSignInRedirect...');
|
||||
if (!authMethod && isSignInRedirect(redirectUri)) {
|
||||
console.log('Redirect detected, handling...');
|
||||
createMessage({
|
||||
text: 'Redirect detected, handling...',
|
||||
sender: '$lib/components/GoogleAuth.svelte',
|
||||
type: 'SYSTEM'
|
||||
});
|
||||
const providerName = getProviderFromUrl();
|
||||
if (providerName) {
|
||||
await handleRedirect(providerName);
|
||||
} else {
|
||||
console.error('No provider detected in the URL.');
|
||||
}
|
||||
} else {
|
||||
console.log('No redirect detected.');
|
||||
createMessage({
|
||||
text: 'No redirect detected.',
|
||||
sender: '$lib/components/GoogleAuth.svelte',
|
||||
type: 'SYSTEM'
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
error = err;
|
||||
view = 'ERROR';
|
||||
} finally {
|
||||
isLoading = false;
|
||||
}
|
||||
});
|
||||
function resetView() {
|
||||
view = authMethod ? 'fetched' : 'sign_in';
|
||||
error = null;
|
||||
|
Reference in New Issue
Block a user