Skip to content

Commit c92a047

Browse files
committed
Fix issue with NULL UUIDs in user records
1 parent f6f176b commit c92a047

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

backend/index.js

+44-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,45 @@ const seed = async () => {
4040
};
4141
seed();
4242

43+
// recheck all uuid that is NULL or 'NULL'
44+
const recheck = async () => {
45+
let conn;
46+
try {
47+
conn = await pool.getConnection();
48+
const sql = "SELECT * FROM users WHERE uuid IS NULL OR uuid = 'NULL'";
49+
const rows = await conn.query(sql);
50+
console.log(`Rechecking ${rows.length} users`);
51+
for (const row of rows) {
52+
const response = await fetch(
53+
`https://api.mojang.com/users/profiles/minecraft/${row.username}`,
54+
);
55+
if (response.status === 404) {
56+
console.log(`Failed to get uuid for ${row.username}`);
57+
continue;
58+
}
59+
if (!response.ok) {
60+
console.log(`Failed to get uuid for ${row.username}`);
61+
continue;
62+
}
63+
const uuid = (await response.json()).id;
64+
if (!uuid || uuid == null) {
65+
console.log(`Failed to get uuid for ${row.username}: ${uuid}`);
66+
continue;
67+
}
68+
const updateSql = "UPDATE users SET uuid = ? WHERE username = ?";
69+
await conn.query(updateSql, [uuid, row.username]);
70+
console.log(`Updated UUID for ${row.username} to ${uuid}`);
71+
}
72+
} catch (err) {
73+
console.error(err);
74+
console.log("Retrying in 30 minutes");
75+
setTimeout(recheck, 30 * 60 * 1000);
76+
} finally {
77+
if (conn) conn.end();
78+
}
79+
}
80+
recheck();
81+
4382
const app = express();
4483
app.use(express.static("../frontend/dist")); // use public
4584
app.use(express.json()); // to support JSON-encoded bodies
@@ -94,7 +133,11 @@ app.post("/register", async (req, res) => {
94133
return;
95134
}
96135

97-
const uuid = await response.json().id;
136+
const json = await response.json();
137+
console.log(json);
138+
const uuid = json.id;
139+
140+
console.log(`UUID for ${username} is ${uuid}`);
98141

99142
if (!uuid || uuid == null) {
100143
console.log(`Failed to get uuid for ${username}: ${uuid}`);

0 commit comments

Comments
 (0)