-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.tsx
150 lines (136 loc) · 4.39 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { useEffect, useState } from "react";
import { toast } from "react-toastify";
import { CollectButton } from "../components/CollectButton";
import { RegisterButton } from "../components/RegisterSessionButton";
import { ParticleNetwork, UserInfo } from "@particle-network/auth";
import { ParticleProvider } from "@particle-network/provider";
import "react-toastify/dist/ReactToastify.css";
// You might need to adjust these types based on the actual API and structures you're working with
interface PlayerInfo {
id: string;
}
function App() {
const [particle, setParticle] = useState<ParticleNetwork | null>(null);
const [provider, setProvider] = useState<ParticleProvider | null>(null);
const [playerId, setPlayerId] = useState<string | null>(null);
const [showPopup, setShowPopup] = useState<string | false>(false);
useEffect(() => {
const init = async () => {
const p = new ParticleNetwork({
projectId: process.env.NEXT_PUBLIC_PROJECT_ID!,
clientKey: process.env.NEXT_PUBLIC_CLIENT_KEY!,
appId: process.env.NEXT_PUBLIC_APP_ID!,
});
setParticle(p);
};
init();
}, []);
const login = async () => {
if (!particle) return toast.error("Not initialized");
await particle.auth.login({ preferredAuthType: "google" });
setProvider(new ParticleProvider(particle.auth));
validateToken();
};
const validateToken = async () => {
if (!particle) return;
const userInfo: UserInfo | null = particle.auth.getUserInfo();
if (!userInfo) return toast.error("User info not found");
const toastId = toast.loading("Validating...");
const res = await fetch("/api/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${userInfo.token}`,
},
body: JSON.stringify({ user_uuid: userInfo.uuid }),
});
toast.dismiss(toastId);
if (res.status === 200) {
const data: { player: PlayerInfo } = await res.json();
setPlayerId(data.player.id);
toast.success("JWT Verified");
} else {
setProvider(null);
toast.error("JWT Validation Failed");
}
};
const uiConsole = (...args: any[]): void => {
const content = JSON.stringify(args.length > 1 ? args : args[0], null, 2);
setShowPopup(content);
};
const logout = async () => {
if (!particle) return;
setProvider(null);
};
return (
<div className="App">
<div className="logos-section">
<a target="_blank" href="https://particle.network" rel="noreferrer">
<img
src="https://i.imgur.com/2btL79J.png"
alt="Particle Network"
className="particle-logo"
/>
</a>
<a target="_blank" href="https://openfort.xyz" rel="noreferrer">
<img
src="https://i.imgur.com/b9dAZXs.png"
alt="Openfort"
className="openfort-logo"
/>
</a>
</div>
{!provider ? (
<div className="login-section">
<button className="sign-button" onClick={login}>
Sign in with Google
</button>
</div>
) : (
<div className="profile-card">
<h2>{particle?.auth?.getUserInfo()?.name ?? "Default Name"}</h2>
<div className="action-buttons">
{playerId && particle && (
<RegisterButton
playerId={playerId}
particle={particle}
provider={provider}
logout={logout}
uiConsole={uiConsole}
/>
)}
<button onClick={() => uiConsole(particle?.auth.getUserInfo())}>
Get User Info
</button>
{playerId && particle && (
<CollectButton
playerId={playerId}
particle={particle}
provider={provider}
logout={logout}
uiConsole={uiConsole}
/>
)}
</div>
</div>
)}
<div id="console">
<p></p>
</div>
{showPopup && (
<div className="popup-overlay">
<div className="popup-content">
<pre>{showPopup}</pre>
<button
className="popup-close-btn"
onClick={() => setShowPopup(false)}
>
×
</button>
</div>
</div>
)}
</div>
);
}
export default App;