diff --git a/backend/api/controllers/user.js b/backend/api/controllers/user.js index 23e0c6d54..2c23166f6 100644 --- a/backend/api/controllers/user.js +++ b/backend/api/controllers/user.js @@ -35,13 +35,14 @@ class UserController { static async updateName(req, res, next) { try { + const { uid } = req.decodedToken; const { name } = req.body; if (!isUsernameValid(name)) return res.status(400).json({ message: "Username invalid. Name cannot contain special characters or contain more than 14 characters. Can include _ . and -", }); - await UsersDAO.updateName(); + await UsersDAO.updateName(uid, name); return res.sendStatus(200); } catch (e) { return next(e); @@ -89,8 +90,22 @@ class UserController { static async getUser(req, res, next) { try { - const { uid } = req.decodedToken; - const userInfo = await UsersDAO.getUser(uid); + const { email, uid } = req.decodedToken; + let userInfo; + try { + userInfo = await UsersDAO.getUser(uid); + } catch (e) { + if (email && uid) { + userInfo = await UsersDAO.addUser(undefined, email, uid); + } else { + throw new MonkeyError( + 400, + "User not found. Could not recreate user document.", + "Tried to recreate user document but either email or uid is nullish", + uid + ); + } + } return res.status(200).json(userInfo); } catch (e) { return next(e); diff --git a/src/js/account.js b/src/js/account.js index c8f61dfe8..0223aad81 100644 --- a/src/js/account.js +++ b/src/js/account.js @@ -50,7 +50,7 @@ export async function getDataAndInit() { // throw "Missing db snapshot. Client likely could not connect to the backend."; // } let user = firebase.auth().currentUser; - if (snap.name === undefined) { + if (snap.name == undefined) { //verify username if (Misc.isUsernameValid(user.name)) { //valid, just update @@ -60,30 +60,57 @@ export async function getDataAndInit() { } else { //invalid, get new // Notifications.add("Invalid name", 0); - let promptVal = null; - let cdnVal = undefined; + // let promptVal = null; + // let cdnVal = undefined; - while ( - promptVal === null || - cdnVal === undefined || - cdnVal.data.status < 0 - ) { - promptVal = prompt( - "Your name is either invalid or unavailable (you also need to do this if you used Google Sign Up). Please provide a new display name (cannot be longer than 14 characters, can only contain letters, numbers, underscores, dots and dashes):" + // while ( + // promptVal === null || + // cdnVal === undefined || + // cdnVal.data.status < 0 + // ) { + // promptVal = prompt( + // "Your name is either invalid or unavailable (you also need to do this if you used Google Sign Up). Please provide a new display name (cannot be longer than 14 characters, can only contain letters, numbers, underscores, dots and dashes):" + // ); + // //TODO update + // axiosInstance + // .post("/updateName", { + // name: promptVal, + // }) + // .then((cdnVal) => { + // if (cdnVal.data.status === 1) { + // alert("Name updated", 1); + // location.reload(); + // } else if (cdnVal.data.status < 0) { + // alert(cdnVal.data.message, 0); + // } + // }); + // } + let nameGood = false; + let name = ""; + + while (nameGood === false) { + name = await prompt( + "Please provide a new username (cannot be longer than 16 characters, can only contain letters, numbers, underscores, dots and dashes):" ); - //TODO update - axiosInstance - .post("/updateName", { - name: promptVal, - }) - .then((cdnVal) => { - if (cdnVal.data.status === 1) { - alert("Name updated", 1); - location.reload(); - } else if (cdnVal.data.status < 0) { - alert(cdnVal.data.message, 0); - } - }); + + let response; + try { + response = await axiosInstance.post("/user/updateName", { name }); + } catch (e) { + let msg = e?.response?.data?.message ?? e.message; + if (e.response.status >= 500) { + Notifications.add("Failed to update name: " + msg, -1); + throw e; + } else { + alert(msg); + } + } + if (response?.status == 200) { + nameGood = true; + Notifications.add("Name updated", 1); + DB.getSnapshot().name = name; + $("#menu .icon-button.account .text").text(name); + } } } }