creating a user document if none is found

fixed update name
This commit is contained in:
Miodec 2021-08-18 02:03:41 +01:00
parent 7106318b0d
commit 28646b3b6f
2 changed files with 68 additions and 26 deletions

View file

@ -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);

View file

@ -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);
}
}
}
}