mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2025-12-16 18:28:47 +00:00
Merge branch 'master' into fix-delete
This commit is contained in:
commit
acd33d6a96
@ -1,4 +1,3 @@
|
|||||||
/* eslint-disable promise/always-return */
|
|
||||||
const { admin, db } = require("../util/admin");
|
const { admin, db } = require("../util/admin");
|
||||||
exports.putTopic = (req, res) => {
|
exports.putTopic = (req, res) => {
|
||||||
const newTopic = {
|
const newTopic = {
|
||||||
@ -52,7 +51,7 @@ exports.deleteTopic = (req, res) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
res.json({ message: "Topic successfully deleted!" });
|
return res.json({ message: "Topic successfully deleted!" });
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|||||||
@ -54,7 +54,7 @@ exports.signup = (req, res) => {
|
|||||||
|
|
||||||
db.doc(`/users/${newUser.handle}`)
|
db.doc(`/users/${newUser.handle}`)
|
||||||
.get()
|
.get()
|
||||||
.then((doc) => {
|
.then(doc => {
|
||||||
if (doc.exists) {
|
if (doc.exists) {
|
||||||
return res
|
return res
|
||||||
.status(400)
|
.status(400)
|
||||||
@ -64,11 +64,11 @@ exports.signup = (req, res) => {
|
|||||||
.auth()
|
.auth()
|
||||||
.createUserWithEmailAndPassword(newUser.email, newUser.password);
|
.createUserWithEmailAndPassword(newUser.email, newUser.password);
|
||||||
})
|
})
|
||||||
.then((data) => {
|
.then(data => {
|
||||||
userId = data.user.uid;
|
userId = data.user.uid;
|
||||||
return data.user.getIdToken();
|
return data.user.getIdToken();
|
||||||
})
|
})
|
||||||
.then((idToken) => {
|
.then(idToken => {
|
||||||
token = idToken;
|
token = idToken;
|
||||||
const defaultImageUrl = `https://firebasestorage.googleapis.com/v0/b/${config.storageBucket}/o/no-img.png?alt=media`;
|
const defaultImageUrl = `https://firebasestorage.googleapis.com/v0/b/${config.storageBucket}/o/no-img.png?alt=media`;
|
||||||
const userCred = {
|
const userCred = {
|
||||||
@ -84,7 +84,7 @@ exports.signup = (req, res) => {
|
|||||||
.then(() => {
|
.then(() => {
|
||||||
return res.status(201).json({ token });
|
return res.status(201).json({ token });
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch(err => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
if (err.code === "auth/email-already-in-use") {
|
if (err.code === "auth/email-already-in-use") {
|
||||||
return res.status(500).json({ email: "This email is already taken." });
|
return res.status(500).json({ email: "This email is already taken." });
|
||||||
@ -122,62 +122,76 @@ exports.login = (req, res) => {
|
|||||||
// Email/username field is username since it's not in email format
|
// Email/username field is username since it's not in email format
|
||||||
if (!user.email.match(emailRegEx)) {
|
if (!user.email.match(emailRegEx)) {
|
||||||
var userDoc = db.collection("users").doc(`${user.email}`);
|
var userDoc = db.collection("users").doc(`${user.email}`);
|
||||||
userDoc.get()
|
userDoc
|
||||||
.then(function(doc) {
|
.get()
|
||||||
|
.then(function(doc) {
|
||||||
if (doc.exists) {
|
if (doc.exists) {
|
||||||
user.email = doc.data().email;
|
user.email = doc.data().email;
|
||||||
}
|
} else {
|
||||||
else {
|
return res
|
||||||
return res.status(403).json({ general: "Invalid credentials. Please try again." });
|
.status(403)
|
||||||
|
.json({ general: "Invalid credentials. Please try again." });
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
})
|
|
||||||
.then(function() {
|
|
||||||
firebase
|
|
||||||
.auth()
|
|
||||||
.signInWithEmailAndPassword(user.email, user.password)
|
|
||||||
.then((data) => {
|
|
||||||
return data.user.getIdToken();
|
|
||||||
})
|
})
|
||||||
.then((token) => {
|
.then(function() {
|
||||||
return res.status(200).json({ token });
|
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) => {
|
.catch(function(err) {
|
||||||
console.error(err);
|
if (!doc.exists) {
|
||||||
if (err.code === "auth/user-not-found" || err.code === "auth/invalid-email" || err.code === "auth/wrong-password") {
|
return res
|
||||||
return res.status(403).json({ general: "Invalid credentials. Please try again." });
|
.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
|
// Email/username field is username
|
||||||
else {
|
else {
|
||||||
firebase
|
firebase
|
||||||
.auth()
|
.auth()
|
||||||
.signInWithEmailAndPassword(user.email, user.password)
|
.signInWithEmailAndPassword(user.email, user.password)
|
||||||
.then((data) => {
|
.then(data => {
|
||||||
return data.user.getIdToken();
|
return data.user.getIdToken();
|
||||||
})
|
})
|
||||||
.then((token) => {
|
.then(token => {
|
||||||
return res.status(200).json({ token });
|
return res.status(200).json({ token });
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch(err => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
if (err.code === "auth/user-not-found" || err.code === "auth/invalid-email" || err.code === "auth/wrong-password") {
|
if (
|
||||||
return res
|
err.code === "auth/user-not-found" ||
|
||||||
.status(403)
|
err.code === "auth/invalid-email" ||
|
||||||
.json({ general: "Invalid credentials. Please try again." });
|
err.code === "auth/wrong-password"
|
||||||
}
|
) {
|
||||||
return res.status(500).json({ error: err.code });
|
return res
|
||||||
});
|
.status(403)
|
||||||
|
.json({ general: "Invalid credentials. Please try again." });
|
||||||
|
}
|
||||||
|
return res.status(500).json({ error: err.code });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -262,10 +276,10 @@ exports.getProfileInfo = (req, res) => {
|
|||||||
db.collection("users")
|
db.collection("users")
|
||||||
.doc(req.user.handle)
|
.doc(req.user.handle)
|
||||||
.get()
|
.get()
|
||||||
.then((data) => {
|
.then(data => {
|
||||||
return res.status(200).json(data.data());
|
return res.status(200).json(data.data());
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch(err => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
return res.status(500).json(err);
|
return res.status(500).json(err);
|
||||||
});
|
});
|
||||||
@ -283,13 +297,11 @@ exports.updateProfileInfo = (req, res) => {
|
|||||||
.set(profileData, { merge: true })
|
.set(profileData, { merge: true })
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log(`${req.user.handle}'s profile info has been updated.`);
|
console.log(`${req.user.handle}'s profile info has been updated.`);
|
||||||
return res
|
return res.status(201).json({
|
||||||
.status(201)
|
general: `${req.user.handle}'s profile info has been updated.`
|
||||||
.json({
|
});
|
||||||
general: `${req.user.handle}'s profile info has been updated.`
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch(err => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
error: "Error updating profile data"
|
error: "Error updating profile data"
|
||||||
@ -301,14 +313,15 @@ exports.getUserDetails = (req, res) => {
|
|||||||
let userData = {};
|
let userData = {};
|
||||||
db.doc(`/users/${req.body.handle}`)
|
db.doc(`/users/${req.body.handle}`)
|
||||||
.get()
|
.get()
|
||||||
.then((doc) => {
|
.then(doc => {
|
||||||
if (doc.exists) {
|
if (doc.exists) {
|
||||||
userData = doc.data();
|
userData = doc.data();
|
||||||
return res.status(200).json({userData});
|
return res.status(200).json({ userData });
|
||||||
} else {
|
} else {
|
||||||
return res.status(400).json({error: "User not found."})
|
return res.status(400).json({ error: "User not found." });
|
||||||
}})
|
}
|
||||||
.catch((err) => {
|
})
|
||||||
|
.catch(err => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
return res.status(500).json({ error: err.code });
|
return res.status(500).json({ error: err.code });
|
||||||
});
|
});
|
||||||
@ -318,17 +331,34 @@ exports.getAuthenticatedUser = (req, res) => {
|
|||||||
let credentials = {};
|
let credentials = {};
|
||||||
db.doc(`/users/${req.user.handle}`)
|
db.doc(`/users/${req.user.handle}`)
|
||||||
.get()
|
.get()
|
||||||
.then((doc) => {
|
.then(doc => {
|
||||||
if (doc.exists) {
|
if (doc.exists) {
|
||||||
credentials = doc.data();
|
credentials = doc.data();
|
||||||
return res.status(200).json({credentials});
|
return res.status(200).json({ credentials });
|
||||||
} else {
|
} else {
|
||||||
return res.status(400).json({error: "User not found."})
|
return res.status(400).json({ error: "User not found." });
|
||||||
}})
|
}
|
||||||
.catch((err) => {
|
})
|
||||||
|
.catch(err => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
return res.status(500).json({ error: err.code });
|
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,
|
login,
|
||||||
signup,
|
signup,
|
||||||
deleteUser,
|
deleteUser,
|
||||||
updateProfileInfo
|
updateProfileInfo,
|
||||||
|
getUserHandles
|
||||||
} = require("./handlers/users");
|
} = require("./handlers/users");
|
||||||
|
|
||||||
// Adds a user to the database and registers them in firebase with
|
// 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);
|
app.get("/user", fbAuth, getAuthenticatedUser);
|
||||||
|
|
||||||
|
// get user handles with search phase
|
||||||
|
app.get("/getUserHandles", fbAuth, getUserHandles);
|
||||||
|
|
||||||
/*------------------------------------------------------------------*
|
/*------------------------------------------------------------------*
|
||||||
* handlers/post.js *
|
* handlers/post.js *
|
||||||
*------------------------------------------------------------------*/
|
*------------------------------------------------------------------*/
|
||||||
|
|||||||
@ -31,6 +31,7 @@ import Delete from "./pages/Delete";
|
|||||||
import writeMicroblog from "./Writing_Microblogs.js";
|
import writeMicroblog from "./Writing_Microblogs.js";
|
||||||
import editProfile from "./pages/editProfile";
|
import editProfile from "./pages/editProfile";
|
||||||
import userLine from "./Userline.js";
|
import userLine from "./Userline.js";
|
||||||
|
import Search from "./pages/Search.js";
|
||||||
|
|
||||||
const theme = createMuiTheme(themeObject);
|
const theme = createMuiTheme(themeObject);
|
||||||
|
|
||||||
@ -75,6 +76,10 @@ class App extends Component {
|
|||||||
<Route exact path="/home" component={home} />
|
<Route exact path="/home" component={home} />
|
||||||
<Route exact path="/user" component={user} />
|
<Route exact path="/user" component={user} />
|
||||||
<Route exact path="/edit" component={editProfile} />
|
<Route exact path="/edit" component={editProfile} />
|
||||||
|
<Route exact path="/search" component={Search} />
|
||||||
|
|
||||||
|
<AuthRoute exact path="/" component={home} />
|
||||||
|
|
||||||
</Switch>
|
</Switch>
|
||||||
</div>
|
</div>
|
||||||
</Router>
|
</Router>
|
||||||
|
|||||||
74
twistter-frontend/src/pages/Search.js
Normal file
74
twistter-frontend/src/pages/Search.js
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
import React, { Component } from "react";
|
||||||
|
// import props
|
||||||
|
import { TextField, Paper } from "@material-ui/core";
|
||||||
|
import Grid from "@material-ui/core/Grid";
|
||||||
|
import Axios from "axios";
|
||||||
|
import user from "./user.js";
|
||||||
|
|
||||||
|
import {
|
||||||
|
BrowserRouter as Router,
|
||||||
|
Switch,
|
||||||
|
Route,
|
||||||
|
Link,
|
||||||
|
useRouteMatch
|
||||||
|
} from "react-router-dom";
|
||||||
|
|
||||||
|
export class Search extends Component {
|
||||||
|
state = {
|
||||||
|
searchPhase: null,
|
||||||
|
searchResult: null
|
||||||
|
};
|
||||||
|
|
||||||
|
handleSearch(event) {
|
||||||
|
Axios.get("/getUserHandles").then(res => {
|
||||||
|
this.setState({
|
||||||
|
searchResult: res.data
|
||||||
|
});
|
||||||
|
});
|
||||||
|
console.log(this.state.searchPhase);
|
||||||
|
}
|
||||||
|
|
||||||
|
handleInput(event) {
|
||||||
|
this.setState({
|
||||||
|
searchPhase: event.target.value
|
||||||
|
});
|
||||||
|
this.handleSearch();
|
||||||
|
}
|
||||||
|
|
||||||
|
handleRedirect() {
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
let resultMarkup = this.state.searchResult ? (
|
||||||
|
this.state.searchResult.map(result => (
|
||||||
|
<Router>
|
||||||
|
<div>
|
||||||
|
<Link to={`/user`}>{result}</Link>
|
||||||
|
</div>
|
||||||
|
</Router>
|
||||||
|
))
|
||||||
|
) : (
|
||||||
|
// console.log(this.state.searchResult)
|
||||||
|
<p> searching... </p>
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Grid>
|
||||||
|
<Grid>
|
||||||
|
<TextField
|
||||||
|
id="standard-required"
|
||||||
|
label="Search"
|
||||||
|
defaultValue="username"
|
||||||
|
margin="normal"
|
||||||
|
value={this.state.searchPhase}
|
||||||
|
onChange={event => this.handleInput(event)}
|
||||||
|
/>
|
||||||
|
</Grid>
|
||||||
|
<Grid>{resultMarkup}</Grid>
|
||||||
|
</Grid>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Search;
|
||||||
Loading…
Reference in New Issue
Block a user