mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2025-12-16 18:28:47 +00:00
Merge pull request #63 from ClaytonWWilson/auth-backend-3
Auth backend 3
This commit is contained in:
commit
16567e2373
@ -1,19 +1,18 @@
|
|||||||
/* eslint-disable prefer-arrow-callback */
|
/* eslint-disable prefer-arrow-callback */
|
||||||
/* eslint-disable promise/always-return */
|
/* eslint-disable promise/always-return */
|
||||||
const admin = require('firebase-admin');
|
const admin = require('firebase-admin');
|
||||||
exports.putPost = (req, res) => {
|
|
||||||
|
|
||||||
|
exports.putPost = (req, res) => {
|
||||||
const newPost = {
|
const newPost = {
|
||||||
body: req.body.body,
|
body: req.body.body,
|
||||||
userHandle: req.userData.handle,
|
userHandle: req.user.handle,
|
||||||
userImage: req.body.userImage,
|
userImage: req.body.userImage,
|
||||||
userID: req.userData.userId,
|
userID: req.user.uid,
|
||||||
microBlogTitle: req.body.microBlogTitle,
|
microBlogTitle: req.body.microBlogTitle,
|
||||||
createdAt: new Date().toISOString(),
|
createdAt: new Date().toISOString(),
|
||||||
likeCount: 0,
|
likeCount: 0,
|
||||||
commentCount: 0,
|
commentCount: 0,
|
||||||
microBlogTopics: req.body.microBlogTopics
|
microBlogTopics: req.body.microBlogTopics
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
admin.firestore().collection('posts').add(newPost)
|
admin.firestore().collection('posts').add(newPost)
|
||||||
|
|||||||
@ -7,8 +7,6 @@ const { validateUpdateProfileInfo } = require("../util/validator");
|
|||||||
const firebase = require("firebase");
|
const firebase = require("firebase");
|
||||||
firebase.initializeApp(config);
|
firebase.initializeApp(config);
|
||||||
|
|
||||||
var handle2Email = new Map();
|
|
||||||
|
|
||||||
exports.signup = (req, res) => {
|
exports.signup = (req, res) => {
|
||||||
const newUser = {
|
const newUser = {
|
||||||
email: req.body.email,
|
email: req.body.email,
|
||||||
@ -80,7 +78,6 @@ exports.signup = (req, res) => {
|
|||||||
userId,
|
userId,
|
||||||
followedTopics: []
|
followedTopics: []
|
||||||
};
|
};
|
||||||
handle2Email.set(userCred.handle, userCred.email);
|
|
||||||
return db.doc(`/users/${newUser.handle}`).set(userCred);
|
return db.doc(`/users/${newUser.handle}`).set(userCred);
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
@ -98,7 +95,6 @@ exports.signup = (req, res) => {
|
|||||||
exports.login = (req, res) => {
|
exports.login = (req, res) => {
|
||||||
const user = {
|
const user = {
|
||||||
email: req.body.email,
|
email: req.body.email,
|
||||||
handle: req.body.handle,
|
|
||||||
password: req.body.password
|
password: req.body.password
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -107,24 +103,35 @@ exports.login = (req, res) => {
|
|||||||
|
|
||||||
const emailRegEx = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
const emailRegEx = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
||||||
|
|
||||||
// Email check
|
// Checks if email/username field is empty
|
||||||
if (user.email.trim() === "") {
|
if (user.email.trim() === "") {
|
||||||
errors.email = "Email must not be blank.";
|
errors.email = "Email must not be blank.";
|
||||||
}
|
}
|
||||||
else if (!user.email.match(emailRegEx)) {
|
|
||||||
user.email = handle2Email.get(user.email);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Password check
|
// Checks if password field is empty
|
||||||
if (user.password.trim() === "") {
|
if (user.password.trim() === "") {
|
||||||
errors.password = "Password must not be blank.";
|
errors.password = "Password must not be blank.";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checking if any errors have been raised
|
// Checks if any of the above two errors were found
|
||||||
if (Object.keys(errors).length > 0) {
|
if (Object.keys(errors).length > 0) {
|
||||||
return res.status(400).json(errors);
|
return res.status(400).json(errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
if (doc.exists) {
|
||||||
|
user.email = doc.data().email;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return res.status(403).json({ general: "Invalid credentials. Please try again." });
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
})
|
||||||
|
.then(function() {
|
||||||
firebase
|
firebase
|
||||||
.auth()
|
.auth()
|
||||||
.signInWithEmailAndPassword(user.email, user.password)
|
.signInWithEmailAndPassword(user.email, user.password)
|
||||||
@ -136,49 +143,92 @@ exports.login = (req, res) => {
|
|||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
if (err.code === "auth/wrong-password" || err.code === "auth/invalid-email" || err.code === "auth/user-not-found") {
|
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(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
|
return res
|
||||||
.status(403)
|
.status(403)
|
||||||
.json({ general: "Invalid credentials. Please try again." });
|
.json({ general: "Invalid credentials. Please try again." });
|
||||||
}
|
}
|
||||||
return res.status(500).json({ error: err.code });
|
return res.status(500).json({ error: err.code });
|
||||||
});
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//Deletes user account
|
//Deletes user account
|
||||||
exports.deleteUser = (req, res) => {
|
exports.deleteUser = (req, res) => {
|
||||||
var currentUser;
|
var currentUser;
|
||||||
|
|
||||||
firebase.auth().onAuthStateChanged(function(user) {
|
firebase.auth().onAuthStateChanged(function(user) {
|
||||||
currentUser = user;
|
currentUser = user;
|
||||||
if (currentUser) {
|
if (currentUser) {
|
||||||
/*db.collection("users").doc(`${currentUser.handle}`).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() {
|
.then(function() {
|
||||||
res.status(200).send("Removed user from database.");
|
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;
|
return;
|
||||||
})
|
})
|
||||||
.catch(function(err) {
|
.catch(function(err) {
|
||||||
res.status(500).send("Failed to remove user from database.", err);
|
res.status(500).send("Failed to remove user from database.", err);
|
||||||
});*/
|
});
|
||||||
|
|
||||||
|
|
||||||
//let ref = db.collection('users');
|
|
||||||
//let userDoc = ref.where('userId', '==', currentUser.uid).get();
|
|
||||||
//userDoc.ref.delete();
|
|
||||||
|
|
||||||
currentUser.delete()
|
currentUser.delete()
|
||||||
.then(function() {
|
.then(function() {
|
||||||
console.log("User successfully deleted.");
|
console.log("Successfully deleted user.");
|
||||||
res.status(200).send("Deleted user.");
|
res.status(200).send("Sucessfully deleted user.");
|
||||||
return;
|
return;
|
||||||
})
|
})
|
||||||
.catch(function(err) {
|
.catch(function(err) {
|
||||||
console.log("Error deleting user.", err);
|
console.log("Failed to delete user.", err);
|
||||||
res.status(500).send("Failed to delete user.");
|
res.status(500).send("Failed to delete user.");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
console.log("Cannot get user.");
|
console.log("Failed to deleter user or cannot get user.");
|
||||||
res.status(500).send("Cannot get user.");
|
res.status(500).send("Failed to deleter user or cannot get user.");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -199,8 +249,6 @@ exports.getProfileInfo = (req, res) => {
|
|||||||
|
|
||||||
// Updates the data in the database of the user who is currently logged in
|
// Updates the data in the database of the user who is currently logged in
|
||||||
exports.updateProfileInfo = (req, res) => {
|
exports.updateProfileInfo = (req, res) => {
|
||||||
// TODO: Add functionality for adding/updating profile images
|
|
||||||
|
|
||||||
// Data validation
|
// Data validation
|
||||||
const { valid, errors, profileData } = validateUpdateProfileInfo(req);
|
const { valid, errors, profileData } = validateUpdateProfileInfo(req);
|
||||||
if (!valid) return res.status(400).json(errors);
|
if (!valid) return res.status(400).json(errors);
|
||||||
|
|||||||
@ -29,7 +29,7 @@ app.post("/signup", signup);
|
|||||||
app.post("/login", login);
|
app.post("/login", login);
|
||||||
|
|
||||||
//Deletes user account
|
//Deletes user account
|
||||||
app.delete("/delete", deleteUser);
|
app.delete("/delete", fbAuth, deleteUser);
|
||||||
|
|
||||||
app.get("/getUser", fbAuth, getUserDetails);
|
app.get("/getUser", fbAuth, getUserDetails);
|
||||||
|
|
||||||
|
|||||||
@ -75,7 +75,6 @@ class App extends Component {
|
|||||||
<Route exact path="/user" component={user} />
|
<Route exact path="/user" component={user} />
|
||||||
<Route exact path="/home" component={writeMicroblog} />
|
<Route exact path="/home" component={writeMicroblog} />
|
||||||
<Route exact path="/edit" component={editProfile} />
|
<Route exact path="/edit" component={editProfile} />
|
||||||
{/* <Route exact path="/user" component={userLine} /> */}
|
|
||||||
|
|
||||||
<AuthRoute exact path="/" component={home}/>
|
<AuthRoute exact path="/" component={home}/>
|
||||||
</Switch>
|
</Switch>
|
||||||
|
|||||||
@ -33,7 +33,7 @@ class Writing_Microblogs extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
handleSubmit(event) {
|
handleSubmit(event) {
|
||||||
// alert('A title for the microblog was inputted: ' + this.state.title + '\nA microblog was posted: ' + this.state.value);
|
|
||||||
const postData = {
|
const postData = {
|
||||||
body: this.state.value,
|
body: this.state.value,
|
||||||
userImage: "bing-url",
|
userImage: "bing-url",
|
||||||
|
|||||||
@ -32,10 +32,6 @@ const styles = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export class Navbar extends Component {
|
export class Navbar extends Component {
|
||||||
render() {
|
render() {
|
||||||
const authenticated = this.props.user.authenticated;
|
const authenticated = this.props.user.authenticated;
|
||||||
@ -54,11 +50,9 @@ const styles = {
|
|||||||
{authenticated && <Button component={ Link } to='/logout'>
|
{authenticated && <Button component={ Link } to='/logout'>
|
||||||
Logout
|
Logout
|
||||||
</Button>}
|
</Button>}
|
||||||
{/* Commented out the delete button, because it should probably go on
|
{authenticated && <Button component={ Link } to='/delete'>
|
||||||
the profile or editProfile page instead of the NavBar */}
|
|
||||||
{/* <Button component={ Link } to='/delete'>
|
|
||||||
Delete Account
|
Delete Account
|
||||||
</Button> */}
|
</Button>}
|
||||||
</ToolBar>
|
</ToolBar>
|
||||||
</AppBar>
|
</AppBar>
|
||||||
)
|
)
|
||||||
|
|||||||
@ -7,7 +7,8 @@ import Button from "@material-ui/core/Button";
|
|||||||
import withStyles from "@material-ui/core/styles/withStyles";
|
import withStyles from "@material-ui/core/styles/withStyles";
|
||||||
|
|
||||||
// Redux stuff
|
// Redux stuff
|
||||||
import { logoutUser } from "../redux/actions/userActions";
|
//import { logoutUser } from "../redux/actions/userActions";
|
||||||
|
import { deleteUser } from "../redux/actions/userActions";
|
||||||
import { connect } from "react-redux";
|
import { connect } from "react-redux";
|
||||||
|
|
||||||
const styles = {
|
const styles = {
|
||||||
@ -32,7 +33,8 @@ const styles = {
|
|||||||
export class Delete extends Component {
|
export class Delete extends Component {
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
this.props.logoutUser();
|
//this.props.logoutUser();
|
||||||
|
this.props.deleteUser();
|
||||||
this.props.history.push('/');
|
this.props.history.push('/');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,10 +47,12 @@ const mapStateToProps = (state) => ({
|
|||||||
user: state.user
|
user: state.user
|
||||||
});
|
});
|
||||||
|
|
||||||
const mapActionsToProps = { logoutUser };
|
//const mapActionsToProps = { logoutUser };
|
||||||
|
const mapActionsToProps = { deleteUser };
|
||||||
|
|
||||||
Delete.propTypes = {
|
Delete.propTypes = {
|
||||||
logoutUser: PropTypes.func.isRequired,
|
//logoutUser: PropTypes.func.isRequired,
|
||||||
|
deleteUser: PropTypes.func.isRequired,
|
||||||
user: PropTypes.object.isRequired,
|
user: PropTypes.object.isRequired,
|
||||||
classes: PropTypes.object.isRequired
|
classes: PropTypes.object.isRequired
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user