Skip to content

Commit 1aebf91

Browse files
refactor(examples/spa): update usage with popup
1 parent 36a429a commit 1aebf91

File tree

3 files changed

+104
-35
lines changed

3 files changed

+104
-35
lines changed

examples/spa/src/components/LoginButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default function LoginButton() {
99
const handleLogin = async () => {
1010
setIsLoading(true);
1111
try {
12-
await client?.login();
12+
client?.login();
1313
} finally {
1414
setIsLoading(false);
1515
}

examples/spa/src/hooks/use-fast-auth-workflow.ts

Lines changed: 75 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,40 +46,65 @@ export const useFastAuthWorkflow = (): WorkflowState & WorkflowActions => {
4646
const [sending, setSending] = useState(false);
4747
const [txHash, setTxHash] = useState<string | null>(null);
4848

49+
// Check for existing login state and signature requests (popup-based, no redirect handling)
4950
useEffect(() => {
50-
if (isClientInitialized) {
51-
client?.getSigner().then((signer: FastAuthSigner<JavascriptProvider>) => {
52-
setSigner(signer);
53-
if (signer) {
54-
signer
55-
.getPublicKey()
56-
.then((publicKey) => {
57-
setPublicKey(publicKey.toString());
58-
setLoggedIn(true);
59-
})
60-
.catch((error) => {
61-
setLoggedIn(false);
62-
console.error(error);
63-
});
51+
if (isClientInitialized && client) {
52+
const checkAuthState = async () => {
53+
try {
54+
const signer = await client.getSigner();
55+
setSigner(signer);
6456

65-
signer
66-
?.getSignatureRequest()
67-
.then((signatureRequest) => {
68-
if ("signPayload" in signatureRequest && signatureRequest.signPayload) {
69-
setSignatureRequest(signatureRequest);
70-
setExpandedStep(3);
71-
}
72-
})
73-
.catch((error) => {
74-
console.error(error);
75-
});
57+
const publicKey = await signer.getPublicKey();
58+
setPublicKey(publicKey.toString());
59+
setLoggedIn(true);
60+
61+
// Check for pending signature request
62+
try {
63+
const signatureRequest = await signer.getSignatureRequest();
64+
if ("signPayload" in signatureRequest && signatureRequest.signPayload) {
65+
setSignatureRequest(signatureRequest);
66+
setExpandedStep(3);
67+
}
68+
} catch (error) {
69+
// No signature request pending
70+
console.debug("No signature request found");
71+
}
72+
} catch (error) {
73+
// User not logged in - this is expected for popup flows
74+
setLoggedIn(false);
75+
setSigner(null);
7676
}
77-
});
77+
};
78+
79+
checkAuthState();
7880
}
79-
}, [isClientInitialized, client, relayer]);
81+
}, [isClientInitialized, client]);
8082

8183
const handleLogin = async () => {
82-
setExpandedStep(1);
84+
if (!client) {
85+
throw new Error("Client not initialized");
86+
}
87+
88+
try {
89+
// Call login with popup (no redirectUri means popup flow)
90+
// The promise resolves when the popup completes
91+
await client.login();
92+
93+
console.log("login successful");
94+
95+
// After popup closes, check if login was successful
96+
// The login promise resolves when authentication is complete
97+
const signer = await client.getSigner();
98+
setSigner(signer);
99+
const publicKey = await signer.getPublicKey();
100+
setPublicKey(publicKey.toString());
101+
setLoggedIn(true);
102+
setExpandedStep(1);
103+
} catch (error) {
104+
console.error("Login failed:", error);
105+
setLoggedIn(false);
106+
throw error;
107+
}
83108
};
84109

85110
const handleCreateAccount = async (accountId: string) => {
@@ -93,17 +118,37 @@ export const useFastAuthWorkflow = (): WorkflowState & WorkflowActions => {
93118
};
94119

95120
const requestTransactionSignature = async (accountId: string, receiverId: string, amount: string) => {
96-
const tx = await relayer?.createTransfer(accountId, PublicKey.fromString(publicKey!), receiverId, amount);
121+
if (!signer || !relayer) {
122+
throw new Error("Signer or relayer not initialized");
123+
}
124+
125+
const tx = await relayer.createTransfer(accountId, PublicKey.fromString(publicKey!), receiverId, amount);
97126
if (!tx) {
98127
throw new Error("Transaction not created");
99128
}
100-
await signer?.requestTransactionSignature({
129+
130+
// Request signature via popup (no redirectUri means popup flow)
131+
// The promise resolves when the popup completes
132+
await signer.requestTransactionSignature({
101133
imageUrl:
102134
"https://media.licdn.com/dms/image/v2/D4D0BAQH5KL-Ge_0iug/company-logo_200_200/company-logo_200_200/0/1696280807541/peersyst_technology_logo?e=2147483647&v=beta&t=uFYvQ5g6HDoIprYhNNV_zC7tzlBkvmPRkWzuLuDpHtc",
103135
name: "Peersyst Technology",
104136
transaction: tx,
105137
});
138+
106139
setTransferRequested(true);
140+
141+
// After popup closes, check for signature request
142+
// The requestTransactionSignature promise resolves when signature is complete
143+
try {
144+
const signatureRequest = await signer.getSignatureRequest();
145+
if ("signPayload" in signatureRequest && signatureRequest.signPayload) {
146+
setSignatureRequest(signatureRequest);
147+
setExpandedStep(3);
148+
}
149+
} catch (error) {
150+
console.error("Failed to get signature request:", error);
151+
}
107152
};
108153

109154
const handleSignTransaction = async () => {

pnpm-lock.yaml

Lines changed: 28 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)