mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2025-12-16 02:08:47 +00:00
Pulled from master
This commit is contained in:
parent
51d45c0736
commit
6d67dcb485
@ -26,3 +26,20 @@ exports.putPost = (req, res) => {
|
||||
});
|
||||
};
|
||||
|
||||
exports.getallPostsforUser = (req, res) => {
|
||||
|
||||
admin.firestore().collection('posts').where('userHandle', '==', 'user' ).get()
|
||||
.then((data) => {
|
||||
let posts = [];
|
||||
data.forEach(function(doc) {
|
||||
posts.push(doc.data());
|
||||
});
|
||||
return res.status(200).json(posts);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
return res.status(500).json({error: 'Failed to fetch all posts written by specific user.'})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -1,7 +1,138 @@
|
||||
/* eslint-disable promise/catch-or-return */
|
||||
const {db} = require('../util/admin');
|
||||
const {admin, db} = require('../util/admin');
|
||||
const config = require('../util/config');
|
||||
|
||||
const {validateUpdateProfileInfo} = require('../util/validator');
|
||||
|
||||
const firebase = require('firebase');
|
||||
firebase.initializeApp(config);
|
||||
|
||||
|
||||
|
||||
exports.signup = (req, res) => {
|
||||
const newUser = {
|
||||
email: req.body.email,
|
||||
handle: req.body.handle,
|
||||
password: req.body.password,
|
||||
confirmPassword: req.body.confirmPassword,
|
||||
createdAt: new Date().toISOString()
|
||||
};
|
||||
|
||||
// console.log(newUser);
|
||||
|
||||
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,}))$/;
|
||||
|
||||
//Email check
|
||||
if(newUser.email.trim() === '') {
|
||||
errors.email = 'Email must not be blank.';
|
||||
}
|
||||
else if(!newUser.email.match(emailRegEx)) {
|
||||
errors.email = 'Email is invalid.';
|
||||
}
|
||||
|
||||
//handle check
|
||||
if(newUser.handle.trim() === '') {
|
||||
errors.handle = 'Username must not be blank.';
|
||||
}
|
||||
else if(newUser.handle.length < 4 || newUser.handle.length > 30) {
|
||||
errors.handle = 'Username must be between 4-30 characters long.';
|
||||
}
|
||||
|
||||
//Password check
|
||||
if(newUser.password.trim() === '') {
|
||||
errors.password = 'Password must not be blank.';
|
||||
}
|
||||
else if(newUser.password.length < 8 || newUser.password.length > 20) {
|
||||
errors.password = 'Password must be between 8-20 characters long.';
|
||||
}
|
||||
|
||||
//Confirm password check
|
||||
if(newUser.confirmPassword !== newUser.password) {
|
||||
errors.confirmPassword = 'Passwords must match.';
|
||||
}
|
||||
|
||||
//Overall check
|
||||
if(Object.keys(errors).length > 0) {
|
||||
return res.status(400).json(errors);
|
||||
}
|
||||
|
||||
let idToken, userId;
|
||||
|
||||
db.doc(`/users/${newUser.handle}`).get()
|
||||
.then(doc => {
|
||||
if(doc.exists) {
|
||||
return res.status(400).json({ handle: 'This username is already taken.' });
|
||||
}
|
||||
return firebase.auth().createUserWithEmailAndPassword(newUser.email, newUser.password);
|
||||
})
|
||||
.then(data => {
|
||||
userId = data.user.uid;
|
||||
return data.user.getIdToken();
|
||||
})
|
||||
.then(token => {
|
||||
idToken = token;
|
||||
const userCred = {
|
||||
email: req.body.email,
|
||||
handle: newUser.handle,
|
||||
createdAt: newUser.createdAt,
|
||||
userId
|
||||
}
|
||||
return db.doc(`/users/${newUser.handle}`).set(userCred);
|
||||
})
|
||||
.then(() => {
|
||||
return res.status(201).json({ idToken });
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
if(err.code === 'auth/email-already-in-use') {
|
||||
return res.status(500).json({ email: 'This email is already taken.' });
|
||||
}
|
||||
return res.status(500).json({ error: err.code });
|
||||
});
|
||||
};
|
||||
|
||||
exports.login = (req, res) => {
|
||||
const user = {
|
||||
email: req.body.email,
|
||||
password: req.body.password
|
||||
}
|
||||
|
||||
//Auth validation
|
||||
let errors = {};
|
||||
|
||||
//Email check
|
||||
if(user.email.trim() === '') {
|
||||
errors.email = 'Email must not be blank.';
|
||||
}
|
||||
|
||||
//Password check
|
||||
if(user.password.trim() === '') {
|
||||
errors.password = 'Password must not be blank.';
|
||||
}
|
||||
|
||||
//Overall check
|
||||
if(Object.keys(errors).length > 0) {
|
||||
return res.status(400).json(errors);
|
||||
}
|
||||
|
||||
firebase.auth().signInWithEmailAndPassword(user.email, user.password)
|
||||
.then(data => {
|
||||
return data.user.getIdToken();
|
||||
})
|
||||
.then(token => {
|
||||
return res.json({token});
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
if(err.code === 'auth/wrong-password') {
|
||||
return res.status(403).json({ general: 'Invalid credentials. Please try again.' });
|
||||
}
|
||||
return res.status(500).json({ error: err.code });
|
||||
});
|
||||
};
|
||||
|
||||
exports.getProfileInfo = (req, res) => {
|
||||
// FIXME: Delete this after login is implemented
|
||||
req.user = {};
|
||||
|
||||
@ -4,236 +4,26 @@ const app = require('express')();
|
||||
const cors = require('cors');
|
||||
app.use(cors());
|
||||
|
||||
var config = {
|
||||
apiKey: "AIzaSyCvsWetg4qFdsPGfJ3LCw_QaaYzoan7Q34",
|
||||
authDomain: "twistter-e4649.firebaseapp.com",
|
||||
databaseURL: "https://twistter-e4649.firebaseio.com",
|
||||
projectId: "twistter-e4649",
|
||||
storageBucket: "twistter-e4649.appspot.com",
|
||||
messagingSenderId: "20131817365",
|
||||
appId: "1:20131817365:web:633c95fb08b16d4526b89c"
|
||||
};
|
||||
const fbAuth = require('./util/fbAuth');
|
||||
|
||||
|
||||
const firebase = require('firebase');
|
||||
firebase.initializeApp(config);
|
||||
const {db} = require('./util/admin');
|
||||
|
||||
// Acts as a middleman between the client and any function that you use it with
|
||||
// The function will only execute if the user is logged in, or rather, they have
|
||||
// a valid token
|
||||
const firebaseAuth = (req, res, next) => {
|
||||
let idToken;
|
||||
// const firebase = require('firebase');
|
||||
// firebase.initializeApp(config);
|
||||
|
||||
if (req.headers.authorization && req.headers.authorization.startsWith('Bearer ')) {
|
||||
idToken = req.headers.authorization.split('Bearer ')[1];
|
||||
} else {
|
||||
console.error('No token found');
|
||||
return res.status(403).json({ error: 'Unauthorized' });
|
||||
}
|
||||
|
||||
admin.auth().verifyIdToken(idToken)
|
||||
.then(decodedToken => {
|
||||
req.user = decodedToken;
|
||||
console.log(decodedToken);
|
||||
return db.collection('users')
|
||||
.where('userId', '==', req.user.uid)
|
||||
.limit(1)
|
||||
.get();
|
||||
})
|
||||
.then(data => {
|
||||
req.user.username = data.docs[0].data().username;
|
||||
return next();
|
||||
})
|
||||
.catch(err => {
|
||||
console.error("Token verfication failed.", err);
|
||||
return res.status(403).json(err);
|
||||
});
|
||||
};
|
||||
|
||||
app.post('/scream', firebaseAuth, (req, res) => {
|
||||
const newScream = {
|
||||
username: req.user.username,
|
||||
body: req.body.body,
|
||||
numLikes: 0,
|
||||
numComments: 0,
|
||||
time: new Date().toISOString()
|
||||
};
|
||||
|
||||
let invalidCred = {};
|
||||
|
||||
//Body check
|
||||
if(req.body.body.trim() === '') {
|
||||
invalidCred.body = 'Body must not be blank';
|
||||
}
|
||||
|
||||
//Overall check
|
||||
if(Object.keys(invalidCred).length > 0) {
|
||||
return res.status(400).json(errors);
|
||||
}
|
||||
|
||||
db
|
||||
.collection('screams')
|
||||
.add(newScream)
|
||||
.then(doc => {
|
||||
res.json({ message: `Document ${doc.id} created successfully!` });
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: 'Someting went wrong.' });
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/screams', (req, res) => {
|
||||
db
|
||||
.collection('screams')
|
||||
.orderBy('time', 'desc')
|
||||
.get()
|
||||
.then(data => {
|
||||
let screams = [];
|
||||
data.forEach(doc => {
|
||||
screams.push({
|
||||
username: doc.data().username,
|
||||
body: doc.data().body,
|
||||
numLikes: doc.data().numLikes,
|
||||
numComments: doc.data().numComments,
|
||||
time: doc.data().time,
|
||||
screamId: doc.id
|
||||
});
|
||||
});
|
||||
return res.json(screams);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: err.code });
|
||||
});
|
||||
});
|
||||
|
||||
app.post('/signup', (req, res) => {
|
||||
const newUser = {
|
||||
email: req.body.email,
|
||||
username: req.body.username,
|
||||
password: req.body.password,
|
||||
confirmPassword: req.body.confirmPassword,
|
||||
time: new Date().toISOString()
|
||||
};
|
||||
|
||||
let invalidCred = {};
|
||||
|
||||
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
|
||||
if(newUser.email.trim() === '') {
|
||||
invalidCred.email = 'Email must not be blank.';
|
||||
}
|
||||
else if(!newUser.email.match(emailRegEx)) {
|
||||
invalidCred.email = 'Email is invalid.';
|
||||
}
|
||||
|
||||
//Username check
|
||||
if(newUser.username.trim() === '') {
|
||||
invalidCred.username = 'Username must not be blank.';
|
||||
}
|
||||
else if(newUser.username.length < 4 || newUser.username.length > 30) {
|
||||
invalidCred.username = 'Username must be between 4-30 characters long.';
|
||||
}
|
||||
|
||||
//Password check
|
||||
if(newUser.password.trim() === '') {
|
||||
invalidCred.password = 'Password must not be blank.';
|
||||
}
|
||||
else if(newUser.password.length < 8 || newUser.password.length > 20) {
|
||||
invalidCred.password = 'Password must be between 8-20 characters long.';
|
||||
}
|
||||
|
||||
//Confirm password check
|
||||
if(newUser.confirmPassword !== newUser.password) {
|
||||
invalidCred.confirmPassword = 'Passwords must match.';
|
||||
}
|
||||
|
||||
//Overall check
|
||||
if(Object.keys(invalidCred).length > 0) {
|
||||
return res.status(400).json(errors);
|
||||
}
|
||||
|
||||
let idToken, userId;
|
||||
|
||||
db.doc(`/users/${newUser.username}`).get()
|
||||
.then(doc => {
|
||||
if(doc.exists) {
|
||||
return res.status(400).json({ username: 'This username is already taken.' });
|
||||
}
|
||||
return firebase.auth().createUserWithEmailAndPassword(newUser.email, newUser.password);
|
||||
})
|
||||
.then(data => {
|
||||
userId = data.user.uid;
|
||||
return data.user.getIdToken();
|
||||
})
|
||||
.then(token => {
|
||||
idToken = token;
|
||||
const userCred = {
|
||||
email: req.body.email,
|
||||
username: newUser.username,
|
||||
time: newUser.time,
|
||||
userId
|
||||
}
|
||||
return db.doc(`/users/${newUser.username}`).set(userCred);
|
||||
})
|
||||
.then(() => {
|
||||
return res.status(201).json({ idToken });
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
if(err.code === 'auth/email-already-in-use') {
|
||||
return res.status(500).json({ email: 'This email is already taken.' });
|
||||
}
|
||||
return res.status(500).json({ error: err.code });
|
||||
});
|
||||
});
|
||||
|
||||
app.post('/login', (req, res) => {
|
||||
const user = {
|
||||
email: req.body.email,
|
||||
password: req.body.password
|
||||
}
|
||||
|
||||
//Auth validation
|
||||
let invalidCred = {};
|
||||
|
||||
//Email check
|
||||
if(user.email.trim() === '') {
|
||||
invalidCred.email = 'Email must not be blank.';
|
||||
}
|
||||
|
||||
//Password check
|
||||
if(user.password.trim() === '') {
|
||||
invalidCred.password = 'Password must not be blank.';
|
||||
}
|
||||
|
||||
//Overall check
|
||||
if(Object.keys(invalidCred).length > 0) {
|
||||
return res.status(400).json(errors);
|
||||
}
|
||||
|
||||
firebase.auth().signInWithEmailAndPassword(user.email, user.password)
|
||||
.then(data => {
|
||||
return data.user.getIdToken();
|
||||
})
|
||||
.then(token => {
|
||||
return res.json({token});
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
if(err.code === 'auth/wrong-password') {
|
||||
return res.status(403).json({ general: 'Invalid credentials. Please try again.' });
|
||||
}
|
||||
return res.status(500).json({ error: err.code });
|
||||
});
|
||||
});
|
||||
|
||||
/*------------------------------------------------------------------*
|
||||
* handlers/users.js *
|
||||
*------------------------------------------------------------------*/
|
||||
const {getUserDetails, getProfileInfo, updateProfileInfo} = require('./handlers/users');
|
||||
* handlers/users.js *
|
||||
*------------------------------------------------------------------*/
|
||||
const {getUserDetails, getProfileInfo, updateProfileInfo, signup, login} = require('./handlers/users');
|
||||
|
||||
app.post('/signup', signup);
|
||||
|
||||
app.post('/login', login);
|
||||
|
||||
app.get('/getUser/:handle', getUserDetails);
|
||||
|
||||
@ -248,8 +38,9 @@ app.post('/updateProfileInfo', updateProfileInfo);
|
||||
/*------------------------------------------------------------------*
|
||||
* handlers/post.js *
|
||||
*------------------------------------------------------------------*/
|
||||
const {putPost} = require('./handlers/post');
|
||||
const {putPost, getallPostsforUser} = require('./handlers/post');
|
||||
|
||||
app.get('/getallPostsforUser', getallPostsforUser);
|
||||
|
||||
// Adds one post to the database
|
||||
app.post('/putPost', fbAuth, putPost);
|
||||
|
||||
@ -7,8 +7,8 @@ module.exports = (req, res, next) => {
|
||||
let idToken;
|
||||
|
||||
// Checking that the token exists in the header of the request
|
||||
if (req.headers.authorization) {
|
||||
idToken = req.headers.authorization;
|
||||
if (req.headers.authorization && req.headers.authorization.startsWith('Bearer ')) {
|
||||
idToken = req.headers.authorization.split('Bearer ')[1];
|
||||
} else {
|
||||
console.error('No token found');
|
||||
return res.status(403).json({ error: 'Unauthorized'});
|
||||
|
||||
23
socialape-client/.gitignore
vendored
Normal file
23
socialape-client/.gitignore
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
/.pnp
|
||||
.pnp.js
|
||||
|
||||
# testing
|
||||
/coverage
|
||||
|
||||
# production
|
||||
/build
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
68
socialape-client/README.md
Normal file
68
socialape-client/README.md
Normal file
@ -0,0 +1,68 @@
|
||||
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
|
||||
|
||||
## Available Scripts
|
||||
|
||||
In the project directory, you can run:
|
||||
|
||||
### `npm start`
|
||||
|
||||
Runs the app in the development mode.<br>
|
||||
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
|
||||
|
||||
The page will reload if you make edits.<br>
|
||||
You will also see any lint errors in the console.
|
||||
|
||||
### `npm test`
|
||||
|
||||
Launches the test runner in the interactive watch mode.<br>
|
||||
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
|
||||
|
||||
### `npm run build`
|
||||
|
||||
Builds the app for production to the `build` folder.<br>
|
||||
It correctly bundles React in production mode and optimizes the build for the best performance.
|
||||
|
||||
The build is minified and the filenames include the hashes.<br>
|
||||
Your app is ready to be deployed!
|
||||
|
||||
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
|
||||
|
||||
### `npm run eject`
|
||||
|
||||
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
|
||||
|
||||
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
|
||||
|
||||
Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
|
||||
|
||||
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
|
||||
|
||||
## Learn More
|
||||
|
||||
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
|
||||
|
||||
To learn React, check out the [React documentation](https://reactjs.org/).
|
||||
|
||||
### Code Splitting
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
|
||||
|
||||
### Analyzing the Bundle Size
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
|
||||
|
||||
### Making a Progressive Web App
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
|
||||
|
||||
### Advanced Configuration
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
|
||||
|
||||
### Deployment
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
|
||||
|
||||
### `npm run build` fails to minify
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
|
||||
13515
socialape-client/package-lock.json
generated
Normal file
13515
socialape-client/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
35
socialape-client/package.json
Normal file
35
socialape-client/package.json
Normal file
@ -0,0 +1,35 @@
|
||||
{
|
||||
"name": "socialape-client",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"@material-ui/core": "^4.4.3",
|
||||
"axios": "^0.19.0",
|
||||
"react": "^16.9.0",
|
||||
"react-dom": "^16.9.0",
|
||||
"react-router-dom": "^5.1.0",
|
||||
"react-scripts": "3.1.2"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "react-scripts start",
|
||||
"build": "react-scripts build",
|
||||
"test": "react-scripts test",
|
||||
"eject": "react-scripts eject"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"extends": "react-app"
|
||||
},
|
||||
"browserslist": {
|
||||
"production": [
|
||||
">0.2%",
|
||||
"not dead",
|
||||
"not op_mini all"
|
||||
],
|
||||
"development": [
|
||||
"last 1 chrome version",
|
||||
"last 1 firefox version",
|
||||
"last 1 safari version"
|
||||
]
|
||||
},
|
||||
"proxy": "https://us-central1-socialape-2619.cloudfunctions.net/api"
|
||||
}
|
||||
10
socialape-client/public/index.html
Normal file
10
socialape-client/public/index.html
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>React App</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
</body>
|
||||
</html>
|
||||
13
socialape-client/src/App.css
Normal file
13
socialape-client/src/App.css
Normal file
@ -0,0 +1,13 @@
|
||||
html,
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
|
||||
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
|
||||
sans-serif;
|
||||
}
|
||||
.container {
|
||||
margin: 80px auto 0px auto;
|
||||
max-Width: 1200px;
|
||||
}
|
||||
.nav-container {
|
||||
margin: auto;
|
||||
}
|
||||
55
socialape-client/src/App.js
Normal file
55
socialape-client/src/App.js
Normal file
@ -0,0 +1,55 @@
|
||||
import React, { Component } from 'react';
|
||||
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
|
||||
|
||||
import MuiThemeProvider from '@material-ui/core/styles/MuiThemeProvider';
|
||||
import createMuiTheme from '@material-ui/core/styles/createMuiTheme';
|
||||
|
||||
import './App.css';
|
||||
import Navbar from './components/Navbar';
|
||||
import home from './pages/home';
|
||||
import login from './pages/login';
|
||||
import signup from './pages/signup';
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
const theme = createMuiTheme({
|
||||
palette: {
|
||||
primary: {
|
||||
light: '#33c9dc',
|
||||
main: '#00bcd4',
|
||||
dark: '#008394',
|
||||
contrastText: "#fff"
|
||||
},
|
||||
secondary: {
|
||||
light: '#ff6333',
|
||||
main: '#ff3d00',
|
||||
dark: '#b22a00',
|
||||
contrastText: '#fff'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
class App extends Component {
|
||||
render() {
|
||||
return (
|
||||
<MuiThemeProvider theme={theme}>
|
||||
<div className="App">
|
||||
<Router>
|
||||
<Navbar />
|
||||
<div className="container">
|
||||
<Switch>
|
||||
<Route exact path="/" component={home} />
|
||||
<Route exact path="/login" component={login} />
|
||||
<Route exact path="/signup" component={signup} />
|
||||
</Switch>
|
||||
</div>
|
||||
</Router>
|
||||
</div>
|
||||
</MuiThemeProvider>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default App;
|
||||
9
socialape-client/src/App.test.js
Normal file
9
socialape-client/src/App.test.js
Normal file
@ -0,0 +1,9 @@
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import App from './App';
|
||||
|
||||
it('renders without crashing', () => {
|
||||
const div = document.createElement('div');
|
||||
ReactDOM.render(<App />, div);
|
||||
ReactDOM.unmountComponentAtNode(div);
|
||||
});
|
||||
27
socialape-client/src/components/Navbar.js
Normal file
27
socialape-client/src/components/Navbar.js
Normal file
@ -0,0 +1,27 @@
|
||||
import React, { Component } from 'react';
|
||||
|
||||
|
||||
import AppBar from '@material-ui/core/AppBar';
|
||||
import ToolBar from '@material-ui/core/ToolBar';
|
||||
import Button from '@material-ui/core/Button';
|
||||
import Link from 'react-router-dom/Link';
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class Navbar extends Component {
|
||||
render() {
|
||||
return (
|
||||
<AppBar>
|
||||
<ToolBar className="nav-container">
|
||||
<Button color="inherit" component={Link} to="/">Home</Button>
|
||||
<Button color="inherit" component={Link} to="/login">Login</Button>
|
||||
<Button color="inherit" component={Link} to="/signup">Sign up</Button>
|
||||
</ToolBar>
|
||||
</AppBar>
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
export default Navbar;
|
||||
BIN
socialape-client/src/images/twistter-logo.png
Normal file
BIN
socialape-client/src/images/twistter-logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 60 KiB |
8
socialape-client/src/index.js
Normal file
8
socialape-client/src/index.js
Normal file
@ -0,0 +1,8 @@
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import App from './App';
|
||||
import * as serviceWorker from './serviceWorker';
|
||||
|
||||
ReactDOM.render(<App />, document.getElementById('root'));
|
||||
|
||||
serviceWorker.unregister();
|
||||
41
socialape-client/src/pages/home.js
Normal file
41
socialape-client/src/pages/home.js
Normal file
@ -0,0 +1,41 @@
|
||||
import React, { Component } from 'react';
|
||||
import axios from 'axios';
|
||||
|
||||
import Grid from '@material-ui/core/Grid';
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class home extends Component {
|
||||
state = {
|
||||
screams: null
|
||||
}
|
||||
componentDidMount() {
|
||||
axios.get('/screams')
|
||||
.then(res => {
|
||||
console.log(res.data);
|
||||
this.setState({
|
||||
screams: res.data
|
||||
});
|
||||
})
|
||||
.catch(err => console.log(err));
|
||||
}
|
||||
render() {
|
||||
let recentScreamsMarkup = this.state.screams ? (
|
||||
this.state.screams.map(scream => <p>{scream.body}</p>)
|
||||
) : (<p>Loading...</p>)
|
||||
return (
|
||||
<Grid container spacing={16}>
|
||||
<Grid item sm={8} xs={12}>
|
||||
{recentScreamsMarkup}
|
||||
</Grid>
|
||||
<Grid item sm={4} xs={12}>
|
||||
<p>Profile</p>
|
||||
</Grid>
|
||||
</Grid>
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
export default home;
|
||||
108
socialape-client/src/pages/login.js
Normal file
108
socialape-client/src/pages/login.js
Normal file
@ -0,0 +1,108 @@
|
||||
import React, { Component } from 'react';
|
||||
import axios from 'axios';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
import Grid from '@material-ui/core/Grid';
|
||||
import Typography from '@material-ui/core/Typography';
|
||||
import TextField from '@material-ui/core/TextField';
|
||||
import Button from '@material-ui/core/Button';
|
||||
import withStyles from '@material-ui/core/styles/withStyles';
|
||||
|
||||
import logo from '../images/twistter-logo.png';
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
const styles = {
|
||||
form: {
|
||||
textAlign: 'center'
|
||||
},
|
||||
image: {
|
||||
margin: '20px auto 20px auto'
|
||||
},
|
||||
pageTitle: {
|
||||
margin: '20px auto 20px auto'
|
||||
},
|
||||
textField: {
|
||||
margin: '20px auto 20px auto'
|
||||
},
|
||||
button: {
|
||||
margin: '20px auto 20px auto'
|
||||
},
|
||||
customError: {
|
||||
color: 'red',
|
||||
fontSize: '0.8rem'
|
||||
}
|
||||
};
|
||||
|
||||
class login extends Component {
|
||||
constructor() {
|
||||
super();
|
||||
this.state = {
|
||||
email: '',
|
||||
password: '',
|
||||
errors: {}
|
||||
};
|
||||
};
|
||||
handleSubmit = (event) => {
|
||||
event.preventDefault();
|
||||
const userData = {
|
||||
email: this.state.email,
|
||||
password: this.state.password
|
||||
};
|
||||
axios.post('/login', userData)
|
||||
.then(res => {
|
||||
console.log(res.data);
|
||||
localStorage.setItem('firebaseIdToken', `Bearer ${res.data.token}`);
|
||||
this.props.history.push('/');
|
||||
})
|
||||
.catch(err => {
|
||||
this.setState({
|
||||
errors: err.response.data
|
||||
});
|
||||
});
|
||||
};
|
||||
handleChange = (event) => {
|
||||
this.setState({
|
||||
[event.target.name]: event.target.value
|
||||
});
|
||||
};
|
||||
render() {
|
||||
const { classes } = this.props;
|
||||
const { errors } = this.state;
|
||||
return (
|
||||
<Grid container className={classes.form}>
|
||||
<Grid item sm>
|
||||
<img src={logo} alt="logo" className={classes.image} height="200" />
|
||||
<Typography variant="h4" className={classes.pageTitle}>
|
||||
Log in
|
||||
</Typography>
|
||||
<form noValidate onSubmit={this.handleSubmit}>
|
||||
<TextField id="email" name="email" type="email" label="Email" className={classes.textField}
|
||||
helperText={errors.email} error={errors.email ? true : false}
|
||||
value={this.state.email} onChange={this.handleChange} />
|
||||
<br />
|
||||
<TextField id="password" name="password" type="password" label="Password" className={classes.textField}
|
||||
helperText={errors.password} error={errors.password ? true : false}
|
||||
value={this.state.password} onChange={this.handleChange} />
|
||||
<br />
|
||||
{
|
||||
errors.general &&
|
||||
(<Typography variant="body2" className={classes.customError}>
|
||||
{errors.general}
|
||||
</Typography>)
|
||||
}
|
||||
<Button type="submit" variant="contained" color="primary" className={classes.button}>Log in</Button>
|
||||
</form>
|
||||
</Grid>
|
||||
</Grid>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
login.propTypes = {
|
||||
classes: PropTypes.object.isRequired
|
||||
};
|
||||
|
||||
export default withStyles(styles)(login);
|
||||
120
socialape-client/src/pages/signup.js
Normal file
120
socialape-client/src/pages/signup.js
Normal file
@ -0,0 +1,120 @@
|
||||
import React, { Component } from 'react';
|
||||
import axios from 'axios';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
import Grid from '@material-ui/core/Grid';
|
||||
import Typography from '@material-ui/core/Typography';
|
||||
import TextField from '@material-ui/core/TextField';
|
||||
import Button from '@material-ui/core/Button';
|
||||
import withStyles from '@material-ui/core/styles/withStyles';
|
||||
|
||||
import logo from '../images/twistter-logo.png';
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
const styles = {
|
||||
form: {
|
||||
textAlign: 'center'
|
||||
},
|
||||
image: {
|
||||
margin: '20px auto 20px auto'
|
||||
},
|
||||
pageTitle: {
|
||||
margin: '20px auto 20px auto'
|
||||
},
|
||||
textField: {
|
||||
margin: '20px auto 20px auto'
|
||||
},
|
||||
button: {
|
||||
margin: '20px auto 20px auto'
|
||||
},
|
||||
customError: {
|
||||
color: 'red',
|
||||
fontSize: '0.8rem'
|
||||
}
|
||||
};
|
||||
|
||||
class signup extends Component {
|
||||
constructor() {
|
||||
super();
|
||||
this.state = {
|
||||
email: '',
|
||||
username: '',
|
||||
password: '',
|
||||
confirmPassword: '',
|
||||
errors: {}
|
||||
};
|
||||
};
|
||||
handleSubmit = (event) => {
|
||||
event.preventDefault();
|
||||
const newUserData = {
|
||||
email: this.state.email,
|
||||
username: this.state.username,
|
||||
password: this.state.password,
|
||||
confirmPassword: this.state.confirmPassword
|
||||
};
|
||||
axios.post('/signup', newUserData)
|
||||
.then(res => {
|
||||
console.log(res.data);
|
||||
localStorage.setItem('firebaseIdToken', `Bearer ${res.data.token}`);
|
||||
this.props.history.push('/');
|
||||
})
|
||||
.catch(err => {
|
||||
this.setState({
|
||||
errors: err.response.data
|
||||
});
|
||||
});
|
||||
};
|
||||
handleChange = (event) => {
|
||||
this.setState({
|
||||
[event.target.name]: event.target.value
|
||||
});
|
||||
};
|
||||
render() {
|
||||
const { classes } = this.props;
|
||||
const { errors } = this.state;
|
||||
return (
|
||||
<Grid container className={classes.form}>
|
||||
<Grid item sm>
|
||||
<img src={logo} alt="logo" className={classes.image} height="200" />
|
||||
<Typography variant="h4" className={classes.pageTitle}>
|
||||
Sign up
|
||||
</Typography>
|
||||
<form noValidate onSubmit={this.handleSubmit}>
|
||||
<TextField id="email" name="email" type="email" label="Email" className={classes.textField}
|
||||
helperText={errors.email} error={errors.email ? true : false}
|
||||
value={this.state.email} onChange={this.handleChange} />
|
||||
<br />
|
||||
<TextField id="username" name="username" type="text" label="Username" className={classes.textField}
|
||||
helperText={errors.username} error={errors.username ? true : false}
|
||||
value={this.state.username} onChange={this.handleChange} />
|
||||
<br />
|
||||
<TextField id="password" name="password" type="password" label="Password" className={classes.textField}
|
||||
helperText={errors.password} error={errors.password ? true : false}
|
||||
value={this.state.password} onChange={this.handleChange} />
|
||||
<br />
|
||||
<TextField id="confirmPassword" name="confirmPassword" type="password" label="Confirm Password" className={classes.textField}
|
||||
helperText={errors.confirmPassword} error={errors.confirmPassword ? true : false}
|
||||
value={this.state.confirmPassword} onChange={this.handleChange} />
|
||||
<br />
|
||||
{
|
||||
errors.general &&
|
||||
(<Typography variant="body2" className={classes.customError}>
|
||||
{errors.general}
|
||||
</Typography>)
|
||||
}
|
||||
<Button type="submit" variant="contained" color="primary" className={classes.button}>Sign up</Button>
|
||||
</form>
|
||||
</Grid>
|
||||
</Grid>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
signup.propTypes = {
|
||||
classes: PropTypes.object.isRequired
|
||||
};
|
||||
|
||||
export default withStyles(styles)(signup);
|
||||
135
socialape-client/src/serviceWorker.js
Normal file
135
socialape-client/src/serviceWorker.js
Normal file
@ -0,0 +1,135 @@
|
||||
// This optional code is used to register a service worker.
|
||||
// register() is not called by default.
|
||||
|
||||
// This lets the app load faster on subsequent visits in production, and gives
|
||||
// it offline capabilities. However, it also means that developers (and users)
|
||||
// will only see deployed updates on subsequent visits to a page, after all the
|
||||
// existing tabs open on the page have been closed, since previously cached
|
||||
// resources are updated in the background.
|
||||
|
||||
// To learn more about the benefits of this model and instructions on how to
|
||||
// opt-in, read https://bit.ly/CRA-PWA
|
||||
|
||||
const isLocalhost = Boolean(
|
||||
window.location.hostname === 'localhost' ||
|
||||
// [::1] is the IPv6 localhost address.
|
||||
window.location.hostname === '[::1]' ||
|
||||
// 127.0.0.1/8 is considered localhost for IPv4.
|
||||
window.location.hostname.match(
|
||||
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
|
||||
)
|
||||
);
|
||||
|
||||
export function register(config) {
|
||||
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
|
||||
// The URL constructor is available in all browsers that support SW.
|
||||
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
|
||||
if (publicUrl.origin !== window.location.origin) {
|
||||
// Our service worker won't work if PUBLIC_URL is on a different origin
|
||||
// from what our page is served on. This might happen if a CDN is used to
|
||||
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
|
||||
return;
|
||||
}
|
||||
|
||||
window.addEventListener('load', () => {
|
||||
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
|
||||
|
||||
if (isLocalhost) {
|
||||
// This is running on localhost. Let's check if a service worker still exists or not.
|
||||
checkValidServiceWorker(swUrl, config);
|
||||
|
||||
// Add some additional logging to localhost, pointing developers to the
|
||||
// service worker/PWA documentation.
|
||||
navigator.serviceWorker.ready.then(() => {
|
||||
console.log(
|
||||
'This web app is being served cache-first by a service ' +
|
||||
'worker. To learn more, visit https://bit.ly/CRA-PWA'
|
||||
);
|
||||
});
|
||||
} else {
|
||||
// Is not localhost. Just register service worker
|
||||
registerValidSW(swUrl, config);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function registerValidSW(swUrl, config) {
|
||||
navigator.serviceWorker
|
||||
.register(swUrl)
|
||||
.then(registration => {
|
||||
registration.onupdatefound = () => {
|
||||
const installingWorker = registration.installing;
|
||||
if (installingWorker == null) {
|
||||
return;
|
||||
}
|
||||
installingWorker.onstatechange = () => {
|
||||
if (installingWorker.state === 'installed') {
|
||||
if (navigator.serviceWorker.controller) {
|
||||
// At this point, the updated precached content has been fetched,
|
||||
// but the previous service worker will still serve the older
|
||||
// content until all client tabs are closed.
|
||||
console.log(
|
||||
'New content is available and will be used when all ' +
|
||||
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
|
||||
);
|
||||
|
||||
// Execute callback
|
||||
if (config && config.onUpdate) {
|
||||
config.onUpdate(registration);
|
||||
}
|
||||
} else {
|
||||
// At this point, everything has been precached.
|
||||
// It's the perfect time to display a
|
||||
// "Content is cached for offline use." message.
|
||||
console.log('Content is cached for offline use.');
|
||||
|
||||
// Execute callback
|
||||
if (config && config.onSuccess) {
|
||||
config.onSuccess(registration);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error during service worker registration:', error);
|
||||
});
|
||||
}
|
||||
|
||||
function checkValidServiceWorker(swUrl, config) {
|
||||
// Check if the service worker can be found. If it can't reload the page.
|
||||
fetch(swUrl)
|
||||
.then(response => {
|
||||
// Ensure service worker exists, and that we really are getting a JS file.
|
||||
const contentType = response.headers.get('content-type');
|
||||
if (
|
||||
response.status === 404 ||
|
||||
(contentType != null && contentType.indexOf('javascript') === -1)
|
||||
) {
|
||||
// No service worker found. Probably a different app. Reload the page.
|
||||
navigator.serviceWorker.ready.then(registration => {
|
||||
registration.unregister().then(() => {
|
||||
window.location.reload();
|
||||
});
|
||||
});
|
||||
} else {
|
||||
// Service worker found. Proceed as normal.
|
||||
registerValidSW(swUrl, config);
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
console.log(
|
||||
'No internet connection found. App is running in offline mode.'
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export function unregister() {
|
||||
if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker.ready.then(registration => {
|
||||
registration.unregister();
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
Subproject commit cd5e13b6dd189450d2bc086c2ae8ebc2318369a2
|
||||
Loading…
Reference in New Issue
Block a user