mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2025-12-17 02:38:47 +00:00
Pulled most up-to-date version from master
This commit is contained in:
parent
585a0c1022
commit
eed67326f1
@ -1,8 +1,7 @@
|
|||||||
const admin = require('firebase-admin');
|
|
||||||
/* eslint-disable promise/always-return */
|
/* eslint-disable promise/always-return */
|
||||||
|
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.body.userHandle,
|
userHandle: req.body.userHandle,
|
||||||
@ -11,6 +10,7 @@ exports.putPost = (req, res) => {
|
|||||||
createdAt: new Date().toISOString(),
|
createdAt: new Date().toISOString(),
|
||||||
likeCount: 0,
|
likeCount: 0,
|
||||||
commentCount: 0,
|
commentCount: 0,
|
||||||
|
microBlogTopics: req.body.microBlogTopics
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -28,7 +28,7 @@ exports.putPost = (req, res) => {
|
|||||||
|
|
||||||
exports.getallPostsforUser = (req, res) => {
|
exports.getallPostsforUser = (req, res) => {
|
||||||
|
|
||||||
admin.firestore().collection('posts').where('userHandle', '==', 'user' ).get()
|
admin.firestore().collection('posts').where('userHandle', '==', 'new user' ).get()
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
let posts = [];
|
let posts = [];
|
||||||
data.forEach(function(doc) {
|
data.forEach(function(doc) {
|
||||||
@ -41,5 +41,3 @@ exports.getallPostsforUser = (req, res) => {
|
|||||||
return res.status(500).json({error: 'Failed to fetch all posts written by specific user.'})
|
return res.status(500).json({error: 'Failed to fetch all posts written by specific user.'})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,14 +1,11 @@
|
|||||||
/* eslint-disable promise/catch-or-return */
|
/* eslint-disable promise/catch-or-return */
|
||||||
const {admin, db} = require('../util/admin');
|
const { admin, db } = require("../util/admin");
|
||||||
const config = require('../util/config');
|
const config = require("../util/config");
|
||||||
|
const { validateUpdateProfileInfo } = require("../util/validator");
|
||||||
|
|
||||||
const {validateUpdateProfileInfo} = require('../util/validator');
|
const firebase = require("firebase");
|
||||||
|
|
||||||
const firebase = require('firebase');
|
|
||||||
firebase.initializeApp(config);
|
firebase.initializeApp(config);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
exports.signup = (req, res) => {
|
exports.signup = (req, res) => {
|
||||||
const newUser = {
|
const newUser = {
|
||||||
email: req.body.email,
|
email: req.body.email,
|
||||||
@ -18,39 +15,34 @@ exports.signup = (req, res) => {
|
|||||||
createdAt: new Date().toISOString()
|
createdAt: new Date().toISOString()
|
||||||
};
|
};
|
||||||
|
|
||||||
// console.log(newUser);
|
|
||||||
|
|
||||||
let errors = {};
|
let errors = {};
|
||||||
|
|
||||||
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
|
// Email check
|
||||||
if(newUser.email.trim() === '') {
|
if (newUser.email.trim() === "") {
|
||||||
errors.email = 'Email must not be blank.';
|
errors.email = "Email must not be blank.";
|
||||||
}
|
} else if (!newUser.email.match(emailRegEx)) {
|
||||||
else if(!newUser.email.match(emailRegEx)) {
|
errors.email = "Email is invalid.";
|
||||||
errors.email = 'Email is invalid.';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle check
|
// handle check
|
||||||
if(newUser.handle.trim() === '') {
|
if (newUser.handle.trim() === "") {
|
||||||
errors.handle = 'Username must not be blank.';
|
errors.handle = "Username must not be blank.";
|
||||||
}
|
} else if (newUser.handle.length < 4 || newUser.handle.length > 30) {
|
||||||
else if(newUser.handle.length < 4 || newUser.handle.length > 30) {
|
errors.handle = "Username must be between 4-30 characters long.";
|
||||||
errors.handle = 'Username must be between 4-30 characters long.';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Password check
|
// Password check
|
||||||
if(newUser.password.trim() === '') {
|
if (newUser.password.trim() === "") {
|
||||||
errors.password = 'Password must not be blank.';
|
errors.password = "Password must not be blank.";
|
||||||
}
|
} else if (newUser.password.length < 8 || newUser.password.length > 20) {
|
||||||
else if(newUser.password.length < 8 || newUser.password.length > 20) {
|
errors.password = "Password must be between 8-20 characters long.";
|
||||||
errors.password = 'Password must be between 8-20 characters long.';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Confirm password check
|
// Confirm password check
|
||||||
if (newUser.confirmPassword !== newUser.password) {
|
if (newUser.confirmPassword !== newUser.password) {
|
||||||
errors.confirmPassword = 'Passwords must match.';
|
errors.confirmPassword = "Passwords must match.";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Overall check
|
// Overall check
|
||||||
@ -60,34 +52,39 @@ exports.signup = (req, res) => {
|
|||||||
|
|
||||||
let idToken, userId;
|
let idToken, userId;
|
||||||
|
|
||||||
db.doc(`/users/${newUser.handle}`).get()
|
db.doc(`/users/${newUser.handle}`)
|
||||||
.then(doc => {
|
.get()
|
||||||
|
.then((doc) => {
|
||||||
if (doc.exists) {
|
if (doc.exists) {
|
||||||
return res.status(400).json({ handle: 'This username is already taken.' });
|
return res
|
||||||
|
.status(400)
|
||||||
|
.json({ handle: "This username is already taken." });
|
||||||
}
|
}
|
||||||
return firebase.auth().createUserWithEmailAndPassword(newUser.email, newUser.password);
|
return firebase
|
||||||
|
.auth()
|
||||||
|
.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(token => {
|
.then((token) => {
|
||||||
idToken = token;
|
idToken = token;
|
||||||
const userCred = {
|
const userCred = {
|
||||||
email: req.body.email,
|
email: req.body.email,
|
||||||
handle: newUser.handle,
|
handle: newUser.handle,
|
||||||
createdAt: newUser.createdAt,
|
createdAt: newUser.createdAt,
|
||||||
userId
|
userId
|
||||||
}
|
};
|
||||||
return db.doc(`/users/${newUser.handle}`).set(userCred);
|
return db.doc(`/users/${newUser.handle}`).set(userCred);
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
return res.status(201).json({ idToken });
|
return res.status(201).json({ idToken });
|
||||||
})
|
})
|
||||||
.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." });
|
||||||
}
|
}
|
||||||
return res.status(500).json({ error: err.code });
|
return res.status(500).json({ error: err.code });
|
||||||
});
|
});
|
||||||
@ -97,48 +94,51 @@ exports.login = (req, res) => {
|
|||||||
const user = {
|
const user = {
|
||||||
email: req.body.email,
|
email: req.body.email,
|
||||||
password: req.body.password
|
password: req.body.password
|
||||||
}
|
};
|
||||||
|
|
||||||
// Auth validation
|
// Auth validation
|
||||||
let errors = {};
|
let errors = {};
|
||||||
|
|
||||||
// Email check
|
// Email check
|
||||||
if(user.email.trim() === '') {
|
if (user.email.trim() === "") {
|
||||||
errors.email = 'Email must not be blank.';
|
errors.email = "Email must not be blank.";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Password check
|
// Password check
|
||||||
if(user.password.trim() === '') {
|
if (user.password.trim() === "") {
|
||||||
errors.password = 'Password must not be blank.';
|
errors.password = "Password must not be blank.";
|
||||||
}
|
}
|
||||||
|
|
||||||
//Overall check
|
// Checking if any errors have been raised
|
||||||
if (Object.keys(errors).length > 0) {
|
if (Object.keys(errors).length > 0) {
|
||||||
return res.status(400).json(errors);
|
return res.status(400).json(errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
firebase.auth().signInWithEmailAndPassword(user.email, user.password)
|
firebase
|
||||||
.then(data => {
|
.auth()
|
||||||
|
.signInWithEmailAndPassword(user.email, user.password)
|
||||||
|
.then((data) => {
|
||||||
return data.user.getIdToken();
|
return data.user.getIdToken();
|
||||||
})
|
})
|
||||||
.then(token => {
|
.then((token) => {
|
||||||
return res.json({ token });
|
return res.json({ token });
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch((err) => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
if(err.code === 'auth/wrong-password') {
|
if (err.code === "auth/wrong-password") {
|
||||||
return res.status(403).json({ general: 'Invalid credentials. Please try again.' });
|
return res
|
||||||
|
.status(403)
|
||||||
|
.json({ general: "Invalid credentials. Please try again." });
|
||||||
}
|
}
|
||||||
return res.status(500).json({ error: err.code });
|
return res.status(500).json({ error: err.code });
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Returns all data in the database for the user who is currently signed in
|
||||||
exports.getProfileInfo = (req, res) => {
|
exports.getProfileInfo = (req, res) => {
|
||||||
// FIXME: Delete this after login is implemented
|
db.collection("users")
|
||||||
req.user = {};
|
.doc(req.user.handle)
|
||||||
req.user.handle = 'itsjimmy';
|
.get()
|
||||||
|
|
||||||
db.collection('users').doc(req.user.handle).get()
|
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
return res.status(200).json(data.data());
|
return res.status(200).json(data.data());
|
||||||
})
|
})
|
||||||
@ -148,43 +148,49 @@ exports.getProfileInfo = (req, res) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Updates the data in the database of the user who is currently logged in
|
||||||
exports.updateProfileInfo = (req, res) => {
|
exports.updateProfileInfo = (req, res) => {
|
||||||
// FIXME: Delete this after login is implemented
|
|
||||||
req.user = {};
|
|
||||||
req.user.handle = 'itsjimmy';
|
|
||||||
|
|
||||||
// TODO: Add functionality for adding/updating profile images
|
// TODO: Add functionality for adding/updating profile images
|
||||||
|
|
||||||
|
|
||||||
// Data validation
|
// Data validation
|
||||||
const { valid, errors, profileData } = validateUpdateProfileInfo(req.body);
|
const { valid, errors, profileData } = validateUpdateProfileInfo(req.body);
|
||||||
if (!valid) return res.status(400).json(errors);
|
if (!valid) return res.status(400).json(errors);
|
||||||
|
|
||||||
|
|
||||||
// Update the database entry for this user
|
// Update the database entry for this user
|
||||||
db.collection('users').doc(req.user.handle).set(profileData, {merge: true})
|
db.collection("users")
|
||||||
|
.doc(req.user.handle)
|
||||||
|
.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.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);
|
console.error(err);
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
error: 'Error updating profile data'
|
error: "Error updating profile data"
|
||||||
|
});
|
||||||
});
|
});
|
||||||
})
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.getUserDetails = (req, res) => {
|
exports.getUserDetails = (req, res) => {
|
||||||
let userData = {};
|
let userData = {};
|
||||||
db.doc(`/users/${req.params.handle}`).get().then((doc) => {
|
db.doc(`/users/${req.params.handle}`)
|
||||||
|
.get()
|
||||||
|
.then((doc) => {
|
||||||
if (doc.exists) {
|
if (doc.exists) {
|
||||||
userData.user = doc.data();
|
userData.user = doc.data();
|
||||||
return db.collection('post').where('userHandle', '==', req.params.handle)
|
return db
|
||||||
.orderBy('createdAt', 'desc').get();
|
.collection("post")
|
||||||
|
.where("userHandle", "==", req.params.handle)
|
||||||
|
.orderBy("createdAt", "desc")
|
||||||
|
.get();
|
||||||
} else {
|
} else {
|
||||||
return res.status(404).json({
|
return res.status(404).json({
|
||||||
error: 'User not found'
|
error: "User not found"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,49 +1,47 @@
|
|||||||
/* eslint-disable promise/always-return */
|
/* eslint-disable promise/always-return */
|
||||||
const functions = require('firebase-functions');
|
const app = require("express")();
|
||||||
const app = require('express')();
|
const cors = require("cors");
|
||||||
const cors = require('cors');
|
const { db } = require("./util/admin");
|
||||||
|
const fbAuth = require("./util/fbAuth");
|
||||||
|
const functions = require("firebase-functions");
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
|
|
||||||
const fbAuth = require('./util/fbAuth');
|
|
||||||
|
|
||||||
|
|
||||||
const {db} = require('./util/admin');
|
|
||||||
|
|
||||||
// const firebase = require('firebase');
|
|
||||||
// firebase.initializeApp(config);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*------------------------------------------------------------------*
|
/*------------------------------------------------------------------*
|
||||||
* handlers/users.js *
|
* handlers/users.js *
|
||||||
*------------------------------------------------------------------*/
|
*------------------------------------------------------------------*/
|
||||||
const {getUserDetails, getProfileInfo, updateProfileInfo, signup, login} = require('./handlers/users');
|
const {
|
||||||
|
getUserDetails,
|
||||||
|
getProfileInfo,
|
||||||
|
login,
|
||||||
|
signup,
|
||||||
|
updateProfileInfo
|
||||||
|
} = require("./handlers/users");
|
||||||
|
|
||||||
app.post('/signup', signup);
|
// Adds a user to the database and registers them in firebase with
|
||||||
|
// an email and password pair
|
||||||
|
// Returns a token for the new user
|
||||||
|
app.post("/signup", signup);
|
||||||
|
|
||||||
app.post('/login', login);
|
// Returns a token for the user that matches the provided username
|
||||||
|
// and password
|
||||||
|
app.post("/login", login);
|
||||||
|
|
||||||
app.get('/getUser/:handle', getUserDetails);
|
app.get("/getUser/:handle", getUserDetails);
|
||||||
|
|
||||||
// Returns all profile data of the currently logged in user
|
// Returns all profile data of the currently logged in user
|
||||||
// TODO: Add fbAuth
|
app.get("/getProfileInfo", fbAuth, getProfileInfo);
|
||||||
app.get('/getProfileInfo', getProfileInfo);
|
|
||||||
|
|
||||||
// Updates the currently logged in user's profile information
|
// Updates the currently logged in user's profile information
|
||||||
// TODO: Add fbAuth
|
app.post("/updateProfileInfo", fbAuth, updateProfileInfo);
|
||||||
app.post('/updateProfileInfo', updateProfileInfo);
|
|
||||||
|
|
||||||
/*------------------------------------------------------------------*
|
/*------------------------------------------------------------------*
|
||||||
* handlers/post.js *
|
* handlers/post.js *
|
||||||
*------------------------------------------------------------------*/
|
*------------------------------------------------------------------*/
|
||||||
const {putPost, getallPostsforUser} = require('./handlers/post');
|
const { getallPostsforUser, putPost } = require("./handlers/post");
|
||||||
|
|
||||||
app.get('/getallPostsforUser', getallPostsforUser);
|
app.get("/getallPostsforUser", getallPostsforUser);
|
||||||
|
|
||||||
// Adds one post to the database
|
// Adds one post to the database
|
||||||
app.post('/putPost', fbAuth, putPost);
|
app.post("/putPost", fbAuth, putPost);
|
||||||
|
|
||||||
|
|
||||||
exports.api = functions.https.onRequest(app);
|
exports.api = functions.https.onRequest(app);
|
||||||
@ -1,5 +1,4 @@
|
|||||||
const admin = require('firebase-admin');
|
const admin = require('firebase-admin');
|
||||||
|
|
||||||
admin.initializeApp();
|
admin.initializeApp();
|
||||||
|
|
||||||
const db = admin.firestore();
|
const db = admin.firestore();
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
const isEmpty = (str) => {
|
|
||||||
if (str.trim() === '') return true;
|
|
||||||
else return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
const isEmail = (str) => {
|
const isEmail = (str) => {
|
||||||
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,}))$/;
|
||||||
if (str.match(emailRegEx)) return true;
|
if (str.match(emailRegEx)) return true;
|
||||||
else return false;
|
else return false;
|
||||||
}
|
};
|
||||||
|
|
||||||
|
const isEmpty = (str) => {
|
||||||
|
if (str.trim() === "") return true;
|
||||||
|
else return false;
|
||||||
|
};
|
||||||
|
|
||||||
exports.validateUpdateProfileInfo = (data) => {
|
exports.validateUpdateProfileInfo = (data) => {
|
||||||
let errors = {};
|
let errors = {};
|
||||||
@ -15,7 +15,7 @@ exports.validateUpdateProfileInfo = (data) => {
|
|||||||
|
|
||||||
// ?: Should users be able to change their handles and emails?
|
// ?: Should users be able to change their handles and emails?
|
||||||
|
|
||||||
// Only adds the key to the DB if the values are not empty
|
// Only adds the key to the database if the values are not empty
|
||||||
if (!isEmpty(data.firstName)) profileData.firstName = data.firstName.trim();
|
if (!isEmpty(data.firstName)) profileData.firstName = data.firstName.trim();
|
||||||
if (!isEmpty(data.lastName)) profileData.lastName = data.lastName.trim();
|
if (!isEmpty(data.lastName)) profileData.lastName = data.lastName.trim();
|
||||||
if (!isEmpty(data.bio)) profileData.bio = data.bio.trim();
|
if (!isEmpty(data.bio)) profileData.bio = data.bio.trim();
|
||||||
@ -23,7 +23,7 @@ exports.validateUpdateProfileInfo = (data) => {
|
|||||||
if (isEmpty(data.email)) {
|
if (isEmpty(data.email)) {
|
||||||
errors.email = "Must not be empty.";
|
errors.email = "Must not be empty.";
|
||||||
} else if (!isEmail(data.email)) {
|
} else if (!isEmail(data.email)) {
|
||||||
errors.email = "Must be a valid email."
|
errors.email = "Must be a valid email.";
|
||||||
} else {
|
} else {
|
||||||
profileData.email = data.email;
|
profileData.email = data.email;
|
||||||
}
|
}
|
||||||
@ -32,5 +32,5 @@ exports.validateUpdateProfileInfo = (data) => {
|
|||||||
errors,
|
errors,
|
||||||
valid: Object.keys(errors).length === 0 ? true : false,
|
valid: Object.keys(errors).length === 0 ? true : false,
|
||||||
profileData
|
profileData
|
||||||
}
|
};
|
||||||
};
|
};
|
||||||
Loading…
Reference in New Issue
Block a user