Skip to content

Commit 3f2c876

Browse files
author
Nico Vincent
committed
User Account Handling with Telegram Integration: Code example with Splash modal
1 parent f158896 commit 3f2c876

File tree

4 files changed

+725
-253
lines changed

4 files changed

+725
-253
lines changed

Diff for: app/AccountModal.tsx

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from "react";
2+
3+
interface AccountModalProps {
4+
onResponse: (hasAccount: boolean) => void;
5+
}
6+
7+
const AccountModal: React.FC<AccountModalProps> = ({ onResponse }) => {
8+
return (
9+
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
10+
<div className="bg-white rounded-lg p-4 max-w-sm w-full">
11+
<h2 className="text-2xl font-bold mb-4 text-gray-800">Account Check</h2>
12+
<p className="mb-6 text-gray-600">
13+
Do you have an account on <span className="font-semibold">XYZ</span>?
14+
</p>
15+
<div className="flex justify-center space-x-4">
16+
<button
17+
onClick={() => onResponse(false)}
18+
className="px-4 py-2 bg-gray-200 text-gray-800 rounded hover:bg-gray-300 transition-colors"
19+
>
20+
No
21+
</button>
22+
<button
23+
onClick={() => onResponse(true)}
24+
className="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 transition-colors"
25+
>
26+
Yes
27+
</button>
28+
</div>
29+
</div>
30+
</div>
31+
);
32+
};
33+
34+
export default AccountModal;

Diff for: app/page.tsx

+55-12
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,54 @@ import {
77
} from "../lib/dynamic";
88

99
import Spinner from "./Spinner";
10+
import AccountModal from "./AccountModal";
1011

1112
export default function Main() {
1213
const { sdkHasLoaded, user } = useDynamicContext();
13-
const { telegramSignIn } = useTelegramLogin();
14+
const { telegramSignIn, isAuthWithTelegram } = useTelegramLogin();
1415
const [isLoading, setIsLoading] = useState<boolean>(true);
16+
const [showModal, setShowModal] = useState(false);
1517

1618
useEffect(() => {
1719
if (!sdkHasLoaded) return;
1820

19-
const signIn = async () => {
20-
if (!user) {
21-
await telegramSignIn({ forceCreateUser: true });
21+
const checkTelegramConnection = async () => {
22+
setIsLoading(true);
23+
const isLinkedWithTelegram = await isAuthWithTelegram();
24+
25+
if (isLinkedWithTelegram) {
26+
// Auto login if Telegram is connected
27+
await telegramSignIn();
28+
} else {
29+
// Show modal splash page
30+
setShowModal(true);
2231
}
2332
setIsLoading(false);
2433
};
2534

26-
signIn();
27-
}, [sdkHasLoaded]);
35+
if (user) {
36+
setIsLoading(false);
37+
return;
38+
}
39+
checkTelegramConnection();
40+
}, [sdkHasLoaded, isLoading]);
41+
42+
const handleModalResponse = async (hasAccount: boolean) => {
43+
setShowModal(false);
44+
if (hasAccount) {
45+
// Prompt developer's login
46+
console.log("User already has an account on XYZ");
47+
} else {
48+
// Call signIn with autoCreate: true
49+
console.log(
50+
"User does not have an account on XYZ => Auto Create + Auto Login"
51+
);
52+
await telegramSignIn({ forceCreateUser: true });
53+
}
54+
};
2855

2956
return (
30-
<div className="min-h-screen flex flex-col items-center justify-center text-black" style={{backgroundColor: "#f9f9fb", backgroundImage: "url('/background-pattern.svg')", backgroundBlendMode: "overlay", backgroundRepeat: "repeat"}}>
57+
<div className="min-h-screen flex flex-col items-center justify-center text-black bg-[#f9f9fb] bg-[url('/background-pattern.svg')] bg-repeat bg-blend-overlay">
3158
<div className="flex flex-col items-center justify-center text-center max-w-3xl px-4">
3259
<div className="pt-8">
3360
<div className="inline-flex items-center justify-center">
@@ -36,21 +63,37 @@ export default function Main() {
3663
</div>
3764

3865
<div className="bg-white p-5 rounded-lg shadow-sm mb-7 mt-7 text-sm">
39-
<h2 className="text-xl font-semibold mb-3">You got an auto-wallet!</h2>
66+
{user && (
67+
<h2 className="text-xl font-semibold mb-3">
68+
You got an auto-wallet!
69+
</h2>
70+
)}
4071
<div className="flex justify-center py-4">
4172
{isLoading ? <Spinner /> : <DynamicWidget />}
73+
{showModal && <AccountModal onResponse={handleModalResponse} />}
4274
</div>
4375
<p className="mb-3">
44-
Zero clicks, one multi-chain wallet. We automatically created an embedded wallet for you.
76+
Zero clicks, one multi-chain wallet. We automatically created an
77+
embedded wallet for you.
4578
</p>
4679
<h3 className="text-lg font-semibold mb-2">How This works</h3>
4780
<ul className="list-disc list-inside mb-3 flex flex-col items-start">
4881
<li>We utilize the Telegram authentication token</li>
4982
<li>Token is verified and used to create the end user wallet</li>
50-
<li>The same wallet is accessible on desktop and mobile platforms</li>
51-
<li>If the end user logs in with Telegram later on your site, your wallet is still available</li>
83+
<li>
84+
The same wallet is accessible on desktop and mobile platforms
85+
</li>
86+
<li>
87+
If the end user logs in with Telegram later on your site, your
88+
wallet is still available
89+
</li>
5290
</ul>
53-
<a href="https://docs.dynamic.xyz/guides/integrations/telegram/telegram-auto-wallets" target="_blank" rel="noopener noreferrer" className="mt-3 inline-block bg-blue-600 text-white font-semibold py-1.5 px-3 rounded hover:bg-blue-700 transition duration-300 text-sm">
91+
<a
92+
href="https://docs.dynamic.xyz/guides/integrations/telegram/telegram-auto-wallets"
93+
target="_blank"
94+
rel="noopener noreferrer"
95+
className="mt-3 inline-block bg-blue-600 text-white font-semibold py-1.5 px-3 rounded hover:bg-blue-700 transition duration-300 text-sm"
96+
>
5497
Learn More in Our Docs
5598
</a>
5699
</div>

0 commit comments

Comments
 (0)