Skip to content

Commit 00f281b

Browse files
committed
Add generate token request logic
1 parent 0399e0c commit 00f281b

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

eira/src/main.css

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
--warning-red: #f37373;
1111
--purple: #8553f3;
1212
--light-purple: #ae89ff;
13+
--green: #45f248;
1314
}
1415

1516
body,
@@ -54,6 +55,10 @@ p {
5455
color: var(--warning-red) !important;
5556
}
5657

58+
.green {
59+
color: var(--green) !important;
60+
}
61+
5762
a {
5863
color: var(--highlight-color);
5964
}

eira/src/pages/css/index.css

+9
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,12 @@ main {
5151
flex-direction: column;
5252
gap: 1rem;
5353
}
54+
55+
.status {
56+
display: flex;
57+
gap: 2rem;
58+
padding: 1rem;
59+
align-items: center;
60+
background-color: var(--highlight-dark-color2);
61+
border-radius: 0.5rem;
62+
}

eira/src/pages/index.tsx

+57-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useCookies } from "react-cookie";
66

77
function Index() {
88
const [user, setUser] = useState<UserDataT>();
9+
const [token, setToken] = useState<string | null>(null);
910
const [, navigate] = useLocation();
1011
const [cookies, , removeCookie] = useCookies(["session"]);
1112
const [showForm, setShowForm] = useState<boolean>(false);
@@ -160,6 +161,52 @@ function Index() {
160161
await fetchUser();
161162
};
162163

164+
const getToken = async () => {
165+
let resp: Response;
166+
167+
if (!cookies.session) {
168+
return navigate("/login");
169+
}
170+
171+
try {
172+
resp = await fetch("/users/token", { credentials: "include" });
173+
} catch (error) {
174+
console.error(error);
175+
return navigate("/login");
176+
}
177+
178+
if (!resp.ok) {
179+
throw new Error(`Unable to fetch user data from API: ${resp.status}`);
180+
}
181+
182+
const data: UserDataT = await resp.json();
183+
if (!data) {
184+
removeCookie("session", cookies.session);
185+
return navigate("/login");
186+
}
187+
188+
setToken(data.token);
189+
};
190+
191+
const getStatus = () => {
192+
if (!user) {
193+
return "No Application";
194+
}
195+
196+
if (user.applications.length === 0) {
197+
return "No Application";
198+
}
199+
200+
const app = user.applications[0];
201+
const status = user.status ? <b className="green">Connected</b> : <b className="warningRed">Not Connected</b>;
202+
return (
203+
<div className="status">
204+
<b style={{ fontSize: "1.1em" }}>{app.application_name}</b>
205+
{status}
206+
</div>
207+
);
208+
};
209+
163210
useEffect(() => {
164211
fetchUser();
165212
}, []);
@@ -183,9 +230,17 @@ function Index() {
183230
<b className="lightPurple">API Token</b>
184231
<span>Your access token to TwitchIO Token Relay.</span>
185232
<span>To view this token you must generate a new one. You must keep this token confidential.</span>
186-
<button type="button" className="simpleButton">
233+
<button type="button" className="simpleButton" onClick={getToken}>
187234
Generate Token
188235
</button>
236+
{token ? (
237+
<div className="appDetails">
238+
<span>
239+
<b>API Token</b>
240+
<span className="highlight">{token}</span>
241+
</span>
242+
</div>
243+
) : null}
189244
</div>
190245
</div>
191246

@@ -220,6 +275,7 @@ function Index() {
220275

221276
<div className="details">
222277
<h3>Status</h3>
278+
<div>{user ? getStatus() : null}</div>
223279
<hr className="hrW" />
224280
</div>
225281
</main>

eira/src/types/responses.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ export interface UserDataT {
1515
name: string;
1616
token: string | null;
1717
applications: ApplicationDataT[];
18+
status?: boolean | null;
1819
}

0 commit comments

Comments
 (0)