Skip to content

Commit

Permalink
revert
Browse files Browse the repository at this point in the history
  • Loading branch information
hcote committed Nov 27, 2024
1 parent 0d5ec49 commit 009c1d0
Showing 1 changed file with 40 additions and 48 deletions.
88 changes: 40 additions & 48 deletions packages/@magic-ext/farcaster/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,60 +76,52 @@ export class FarcasterExtension extends Extension.Internal<'farcaster'> {
config = {};

public login = (params?: LoginParams) => {
const fetchChannel = () =>
fetch(`${FARCASTER_RELAY_URL}/v1/channel`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
domain: window.location.host,
siweUri: window.location.origin,
}),
})
.then(response => {
if (!response.ok) {
throw new Error('Error creating channel.');
}
return response.json();
})
.then(channel => {
if (!channel) {
throw new Error('Channel not created yet.');
}
return channel;
const promiEvent = this.utils.createPromiEvent<string, FarcasterLoginEventHandlers>(async (resolve, reject) => {
try {
const response = await fetch(`${FARCASTER_RELAY_URL}/v1/channel`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
domain: window.location.host,
siweUri: window.location.origin,
}),
});

try {
const handle = fetchChannel()
.then((channel: CreateChannelAPIResponse) => {
const payload = this.utils.createJsonRpcRequestPayload(FarcasterPayloadMethod.FarcasterShowQR, [
{
data: {
showUI: params?.showUI ?? DEFAULT_SHOW_UI,
domain: window.location.origin,
isMobile: isMobile(),
channel,
},
},
]);
if (!response.ok) {
throw new Error('Error creating channel.');
}

const handle = this.request<string, FarcasterLoginEventHandlers>(payload);
const channel = (await response.json()) as CreateChannelAPIResponse;

if (isMobile() && isMainFrame()) {
window.location.href = channel.url;
}
if (!channel) {
throw new Error('Channel not created yet.');
}

return handle;
})
.catch(error => {
throw new Error(error);
});
const payload = this.utils.createJsonRpcRequestPayload(FarcasterPayloadMethod.FarcasterShowQR, [
{
data: {
showUI: params?.showUI ?? DEFAULT_SHOW_UI,
domain: window.location.origin,
isMobile: isMobile(),
channel,
},
},
]);

const handle = this.request<string, FarcasterLoginEventHandlers>(payload);

if (isMobile() && isMainFrame()) {
window.location.href = channel.url;
}

return handle;
} catch (error) {
throw new Error((error as Error).message);
}
resolve(handle);
} catch (error) {
reject(error);
}
});
return promiEvent;
};
}

Expand Down

0 comments on commit 009c1d0

Please sign in to comment.