mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2026-03-10 13:15:05 +00:00
added search page
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
/* eslint-disable promise/always-return */
|
||||
const { admin, db } = require("../util/admin");
|
||||
exports.putTopic = (req, res) => {
|
||||
const newTopic = {
|
||||
@@ -52,7 +51,7 @@ exports.deleteTopic = (req, res) => {
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
res.json({ message: "Topic successfully deleted!" });
|
||||
return res.json({ message: "Topic successfully deleted!" });
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
|
||||
@@ -55,7 +55,7 @@ exports.signup = (req, res) => {
|
||||
|
||||
db.doc(`/users/${newUser.handle}`)
|
||||
.get()
|
||||
.then((doc) => {
|
||||
.then(doc => {
|
||||
if (doc.exists) {
|
||||
return res
|
||||
.status(400)
|
||||
@@ -65,11 +65,11 @@ exports.signup = (req, res) => {
|
||||
.auth()
|
||||
.createUserWithEmailAndPassword(newUser.email, newUser.password);
|
||||
})
|
||||
.then((data) => {
|
||||
.then(data => {
|
||||
userId = data.user.uid;
|
||||
return data.user.getIdToken();
|
||||
})
|
||||
.then((idToken) => {
|
||||
.then(idToken => {
|
||||
token = idToken;
|
||||
const userCred = {
|
||||
email: newUser.email,
|
||||
@@ -83,7 +83,7 @@ exports.signup = (req, res) => {
|
||||
.then(() => {
|
||||
return res.status(201).json({ token });
|
||||
})
|
||||
.catch((err) => {
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
if (err.code === "auth/email-already-in-use") {
|
||||
return res.status(500).json({ email: "This email is already taken." });
|
||||
@@ -121,62 +121,76 @@ exports.login = (req, res) => {
|
||||
// Email/username field is username since it's not in email format
|
||||
if (!user.email.match(emailRegEx)) {
|
||||
var userDoc = db.collection("users").doc(`${user.email}`);
|
||||
userDoc.get()
|
||||
.then(function(doc) {
|
||||
userDoc
|
||||
.get()
|
||||
.then(function(doc) {
|
||||
if (doc.exists) {
|
||||
user.email = doc.data().email;
|
||||
}
|
||||
else {
|
||||
return res.status(403).json({ general: "Invalid credentials. Please try again." });
|
||||
} else {
|
||||
return res
|
||||
.status(403)
|
||||
.json({ general: "Invalid credentials. Please try again." });
|
||||
}
|
||||
return;
|
||||
})
|
||||
.then(function() {
|
||||
firebase
|
||||
.auth()
|
||||
.signInWithEmailAndPassword(user.email, user.password)
|
||||
.then((data) => {
|
||||
return data.user.getIdToken();
|
||||
})
|
||||
.then((token) => {
|
||||
return res.status(200).json({ token });
|
||||
.then(function() {
|
||||
firebase
|
||||
.auth()
|
||||
.signInWithEmailAndPassword(user.email, user.password)
|
||||
.then(data => {
|
||||
return data.user.getIdToken();
|
||||
})
|
||||
.then(token => {
|
||||
return res.status(200).json({ token });
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
if (
|
||||
err.code === "auth/user-not-found" ||
|
||||
err.code === "auth/invalid-email" ||
|
||||
err.code === "auth/wrong-password"
|
||||
) {
|
||||
return res
|
||||
.status(403)
|
||||
.json({ general: "Invalid credentials. Please try again." });
|
||||
}
|
||||
return res.status(500).json({ error: err.code });
|
||||
});
|
||||
return;
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
if (err.code === "auth/user-not-found" || err.code === "auth/invalid-email" || err.code === "auth/wrong-password") {
|
||||
return res.status(403).json({ general: "Invalid credentials. Please try again." });
|
||||
.catch(function(err) {
|
||||
if (!doc.exists) {
|
||||
return res
|
||||
.status(403)
|
||||
.json({ general: "Invalid credentials. Please try again." });
|
||||
}
|
||||
return res.status(500).json({ error: err.code });
|
||||
return res.status(500).send(err);
|
||||
});
|
||||
return;
|
||||
})
|
||||
.catch(function(err) {
|
||||
if(!doc.exists) {
|
||||
return res.status(403).json({ general: "Invalid credentials. Please try again." });
|
||||
}
|
||||
return res.status(500).send(err);
|
||||
});
|
||||
}
|
||||
// Email/username field is username
|
||||
else {
|
||||
firebase
|
||||
.auth()
|
||||
.signInWithEmailAndPassword(user.email, user.password)
|
||||
.then((data) => {
|
||||
return data.user.getIdToken();
|
||||
})
|
||||
.then((token) => {
|
||||
return res.status(200).json({ token });
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
if (err.code === "auth/user-not-found" || err.code === "auth/invalid-email" || err.code === "auth/wrong-password") {
|
||||
return res
|
||||
.status(403)
|
||||
.json({ general: "Invalid credentials. Please try again." });
|
||||
}
|
||||
return res.status(500).json({ error: err.code });
|
||||
});
|
||||
.auth()
|
||||
.signInWithEmailAndPassword(user.email, user.password)
|
||||
.then(data => {
|
||||
return data.user.getIdToken();
|
||||
})
|
||||
.then(token => {
|
||||
return res.status(200).json({ token });
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
if (
|
||||
err.code === "auth/user-not-found" ||
|
||||
err.code === "auth/invalid-email" ||
|
||||
err.code === "auth/wrong-password"
|
||||
) {
|
||||
return res
|
||||
.status(403)
|
||||
.json({ general: "Invalid credentials. Please try again." });
|
||||
}
|
||||
return res.status(500).json({ error: err.code });
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -186,47 +200,52 @@ exports.deleteUser = (req, res) => {
|
||||
firebase.auth().onAuthStateChanged(function(user) {
|
||||
currentUser = user;
|
||||
if (currentUser) {
|
||||
var post_query = db.collection("posts").where("userHandle", "==", req.user.handle);
|
||||
post_query.get()
|
||||
.then(function(myPosts) {
|
||||
myPosts.forEach(function(doc) {
|
||||
doc.ref.delete();
|
||||
var post_query = db
|
||||
.collection("posts")
|
||||
.where("userHandle", "==", req.user.handle);
|
||||
post_query
|
||||
.get()
|
||||
.then(function(myPosts) {
|
||||
myPosts.forEach(function(doc) {
|
||||
doc.ref.delete();
|
||||
});
|
||||
return;
|
||||
})
|
||||
.then(function() {
|
||||
res
|
||||
.status(200)
|
||||
.send("Successfully removed all user's posts from database.");
|
||||
return;
|
||||
})
|
||||
.catch(function(err) {
|
||||
res
|
||||
.status(500)
|
||||
.send("Failed to remove all user's posts from database.", err);
|
||||
});
|
||||
return;
|
||||
})
|
||||
.then(function() {
|
||||
res.status(200).send("Successfully removed all user's posts from database.");
|
||||
return;
|
||||
})
|
||||
.catch(function(err) {
|
||||
res.status(500).send("Failed to remove all user's posts from database.", err);
|
||||
});
|
||||
|
||||
db.collection("users")
|
||||
.doc(`${req.user.handle}`)
|
||||
.delete()
|
||||
.then(function() {
|
||||
res.status(200).send("Sucessfully removed user from database.");
|
||||
return;
|
||||
})
|
||||
.catch(function(err) {
|
||||
res.status(500).send("Failed to remove user from database.", err);
|
||||
});
|
||||
|
||||
|
||||
db.collection("users").doc(`${req.user.handle}`).delete()
|
||||
.then(function() {
|
||||
res.status(200).send("Sucessfully removed user from database.");
|
||||
return;
|
||||
})
|
||||
.catch(function(err) {
|
||||
res.status(500).send("Failed to remove user from database.", err);
|
||||
});
|
||||
|
||||
|
||||
|
||||
currentUser.delete()
|
||||
.then(function() {
|
||||
console.log("Successfully deleted user.");
|
||||
res.status(200).send("Sucessfully deleted user.");
|
||||
return;
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log("Failed to delete user.", err);
|
||||
res.status(500).send("Failed to delete user.");
|
||||
});
|
||||
}
|
||||
else {
|
||||
currentUser
|
||||
.delete()
|
||||
.then(function() {
|
||||
console.log("Successfully deleted user.");
|
||||
res.status(200).send("Sucessfully deleted user.");
|
||||
return;
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log("Failed to delete user.", err);
|
||||
res.status(500).send("Failed to delete user.");
|
||||
});
|
||||
} else {
|
||||
console.log("Failed to deleter user or cannot get user.");
|
||||
res.status(500).send("Failed to deleter user or cannot get user.");
|
||||
}
|
||||
@@ -238,10 +257,10 @@ exports.getProfileInfo = (req, res) => {
|
||||
db.collection("users")
|
||||
.doc(req.user.handle)
|
||||
.get()
|
||||
.then((data) => {
|
||||
.then(data => {
|
||||
return res.status(200).json(data.data());
|
||||
})
|
||||
.catch((err) => {
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
return res.status(500).json(err);
|
||||
});
|
||||
@@ -259,13 +278,11 @@ exports.updateProfileInfo = (req, res) => {
|
||||
.set(profileData, { merge: true })
|
||||
.then(() => {
|
||||
console.log(`${req.user.handle}'s profile info has been updated.`);
|
||||
return res
|
||||
.status(201)
|
||||
.json({
|
||||
general: `${req.user.handle}'s profile info has been updated.`
|
||||
});
|
||||
return res.status(201).json({
|
||||
general: `${req.user.handle}'s profile info has been updated.`
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
return res.status(500).json({
|
||||
error: "Error updating profile data"
|
||||
@@ -277,14 +294,15 @@ exports.getUserDetails = (req, res) => {
|
||||
let userData = {};
|
||||
db.doc(`/users/${req.body.handle}`)
|
||||
.get()
|
||||
.then((doc) => {
|
||||
.then(doc => {
|
||||
if (doc.exists) {
|
||||
userData = doc.data();
|
||||
return res.status(200).json({userData});
|
||||
} else {
|
||||
return res.status(400).json({error: "User not found."})
|
||||
}})
|
||||
.catch((err) => {
|
||||
return res.status(200).json({ userData });
|
||||
} else {
|
||||
return res.status(400).json({ error: "User not found." });
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: err.code });
|
||||
});
|
||||
@@ -294,17 +312,34 @@ exports.getAuthenticatedUser = (req, res) => {
|
||||
let credentials = {};
|
||||
db.doc(`/users/${req.user.handle}`)
|
||||
.get()
|
||||
.then((doc) => {
|
||||
.then(doc => {
|
||||
if (doc.exists) {
|
||||
credentials = doc.data();
|
||||
return res.status(200).json({credentials});
|
||||
} else {
|
||||
return res.status(400).json({error: "User not found."})
|
||||
}})
|
||||
.catch((err) => {
|
||||
return res.status(200).json({ credentials });
|
||||
} else {
|
||||
return res.status(400).json({ error: "User not found." });
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: err.code });
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
exports.getUserHandles = (req, res) => {
|
||||
admin
|
||||
.firestore()
|
||||
.collection("users")
|
||||
.get()
|
||||
.then(data => {
|
||||
let users = [];
|
||||
data.forEach(function(doc) {
|
||||
users.push(doc.data().handle);
|
||||
});
|
||||
return res.status(200).json(users);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: "Failed to get all user handles." });
|
||||
});
|
||||
};
|
||||
|
||||
@@ -16,7 +16,8 @@ const {
|
||||
login,
|
||||
signup,
|
||||
deleteUser,
|
||||
updateProfileInfo
|
||||
updateProfileInfo,
|
||||
getUserHandles
|
||||
} = require("./handlers/users");
|
||||
|
||||
// Adds a user to the database and registers them in firebase with
|
||||
@@ -41,6 +42,9 @@ app.post("/updateProfileInfo", fbAuth, updateProfileInfo);
|
||||
|
||||
app.get("/user", fbAuth, getAuthenticatedUser);
|
||||
|
||||
// get user handles with search phase
|
||||
app.get("/getUserHandles", fbAuth, getUserHandles);
|
||||
|
||||
/*------------------------------------------------------------------*
|
||||
* handlers/post.js *
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
Reference in New Issue
Block a user