(token);
+ if (payload.name) setName(payload.name);
+ else if (payload.sub) setName(payload.sub);
+ else setName("Mr. I-have-no Token");
+ payload.sub && setUsername(payload.sub);
+ } catch (InvalidTokenError) {
+ setName("Mr. I-have-no-valid Token");
+ }
}
}
}, []);
@@ -42,35 +48,49 @@ export const SetPassword = () => {
e.preventDefault();
if (password === passwordr) {
setLoading(true);
- const req = new Request(`${baseUrl}api/set_password`, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({ token: token, password: password }),
- });
- let resp: Response;
- try {
- resp = await fetch(req);
- } catch (e) {
- throw new Error(`request failed: ${e}`);
- }
- setLoading(false);
-
- if (resp.ok) {
- console.log(resp);
- navigate("/", {
- replace: true,
- state: { username: username, password: password },
+ if (user) {
+ const resp = await apiAuth(
+ "player/change_password",
+ { current_password: currentPassword, new_password: password },
+ "POST"
+ );
+ setLoading(false);
+ if (resp.detail) setError(resp.detail);
+ else {
+ setError(resp);
+ setTimeout(() => navigate("/"), 2000);
+ }
+ } else {
+ const req = new Request(`${baseUrl}api/set_password`, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({ token: token, password: password }),
});
- }
+ let resp: Response;
+ try {
+ resp = await fetch(req);
+ } catch (e) {
+ throw new Error(`request failed: ${e}`);
+ }
+ setLoading(false);
- if (!resp.ok) {
- if (resp.status === 401) {
- const { detail } = await resp.json();
- if (detail) setError(detail);
- else setError("unauthorized");
- throw new Error("Unauthorized");
+ if (resp.ok) {
+ console.log(resp);
+ navigate("/", {
+ replace: true,
+ state: { username: username, password: password },
+ });
+ }
+
+ if (!resp.ok) {
+ if (resp.status === 401) {
+ const { detail } = await resp.json();
+ if (detail) setError(detail);
+ else setError("unauthorized");
+ throw new Error("Unauthorized");
+ }
}
}
} else setError("passwords are not the same");
@@ -78,12 +98,16 @@ export const SetPassword = () => {
return (
<>
-
- set your password,
-
- {name}
-
- {username && (
+ {user ? (
+ change your password
+ ) : (
+
+ set your password,
+
+ {name}
+
+ )}
+ {!user && username && (
your username is: {username}
@@ -104,6 +128,30 @@ export const SetPassword = () => {
flexDirection: "column",
}}
>
+ {user && (
+
+ {
+ setError("");
+ setCurrentPassword(evt.target.value);
+ }}
+ />
+
+
+ )}
{
{error && {error}}
{loading && }
diff --git a/src/api.ts b/src/api.ts
index 1d19690..6074927 100644
--- a/src/api.ts
+++ b/src/api.ts
@@ -29,7 +29,18 @@ export async function apiAuth(
throw new Error("Unauthorized");
}
}
- return resp.json();
+ const contentType = resp.headers.get("Content-Type");
+ switch (contentType) {
+ case "application/json":
+ return resp.json();
+ case "text/plain":
+ case "text/plain; charset=utf-8":
+ return resp.text();
+ case null:
+ return null;
+ default:
+ throw new Error(`Unsupported content type: ${contentType}`);
+ }
}
export type User = {