diff --git a/.gitignore b/.gitignore index 028e2a6..9c301b7 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ public/bundle.js package-lock.json *hot-update.js *hot-update.json +env.sh diff --git a/backend/auth.js b/backend/auth.js new file mode 100644 index 0000000..e9768fd --- /dev/null +++ b/backend/auth.js @@ -0,0 +1,30 @@ +const express = require('express'); +const router = express.Router(); +const User = require('../root/models').User; + +module.exports = (passport) => { + router.post('/register', (req, res) => { + console.log('here', req.body); + User.create({ + username: req.body.username, + password: req.body.password + }) + .then(user => { + res.json({success: true, user: user}); + }) + .catch(err => { + res.json({success: false, err: err}); + }); + }); + + router.post('/login', passport.authenticate('local'), (req, res) => { + res.json({success: true, user: req.user}); + }); + + router.get('/logout', (req, res) => { + req.logout(); + res.json({success: true}); + }); + + return router; +}; diff --git a/backend/routes.js b/backend/routes.js index 9dd2215..ab1af14 100644 --- a/backend/routes.js +++ b/backend/routes.js @@ -1,11 +1,66 @@ const express = require('express'); const router = express.Router(); +const User = require('../root/models').User; +const Post = require('../root/models').Post; // YOUR API ROUTES HERE +router.get('/:username', (req, res) => { + User.findOne({where: {username: req.params.username}}) + .then(user => { + console.log('user when finding by username: ', user); + res.json({success: true, user: user}); + }) + .catch(err => { + res.json({success: false, err: err}); + }); +}); + +router.get('/', (req, res) => { + console.log('req user is: ************************', req.user); + User.findById(req.user.id) + .then(user => { + console.log('user when finding user on /: ', user); + res.json({success: true, user: user}); + }) + .catch(err => { + res.json({success: false, err: err}); + }); +}); + +router.post('/post/new', (req, res) => { + console.log('req user is ', req.user); + Post.create({ + title: req.body.title, + content: req.body.content, + postId: req.body.postId, + userId: req.user.id + }) + .then(post => { + res.json({success: true, post: post}); + }) + .catch(err => { + res.json({success: false, err: err}); + }); +}); + +router.get('/post/all', (req, res) => { + Post.findAll({where: {postId: null}}) + .then(posts => { + res.json({success: true, posts: posts}); + }) + .catch(err => { + res.json({success: false, err: err}); + }); +}); -// SAMPLE ROUTE -router.use('/users', (req, res) => { - res.json({ success: true }); +router.get('/post/:id', (req, res) => { + Post.findById(req.params.id) + .then(post => { + res.json({success: true, post: post}); + }) + .catch(err => { + res.json({success: false, err: err}); + }); }); module.exports = router; diff --git a/frontend/actions/index.js b/frontend/actions/index.js index 2bba168..c6a45f0 100644 --- a/frontend/actions/index.js +++ b/frontend/actions/index.js @@ -1,3 +1,19 @@ // Action Creators -// import * as types from './types'; +import * as types from './types'; + +export function toggleLoginModal() { + return {type: types.TOGGLE_LOGIN_MODAL}; +} + +export function logout() { + return {type: types.LOGOUT}; +} + +export function login(user) { + return {type: types.LOGIN, user: user}; +} + +export function getPosts(posts) { + return {type: types.GET_POSTS, posts: posts}; +} diff --git a/frontend/actions/types.js b/frontend/actions/types.js index 9836597..5115571 100644 --- a/frontend/actions/types.js +++ b/frontend/actions/types.js @@ -1 +1,8 @@ /* Action types */ +export const TOGGLE_LOGIN_MODAL = 'TOGGLE_LOGIN_MODAL'; + +export const LOGIN = 'LOGIN'; + +export const LOGOUT = 'LOGOUT'; + +export const GET_POSTS = 'GET_POSTS'; diff --git a/frontend/assets/stylesheets/base.scss b/frontend/assets/stylesheets/base.scss index 1ddbba3..706b3c5 100644 --- a/frontend/assets/stylesheets/base.scss +++ b/frontend/assets/stylesheets/base.scss @@ -1,4 +1,12 @@ h1 { font-family: helvetica; font-weight: 200; -} \ No newline at end of file +} + +.modal-container { + position: relative; +} + +.modal-container .modal, .modal-container .modal-backdrop { + position: absolute; +} diff --git a/frontend/components/AuthModal.js b/frontend/components/AuthModal.js new file mode 100644 index 0000000..f05fd0c --- /dev/null +++ b/frontend/components/AuthModal.js @@ -0,0 +1,76 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import { Modal, Button } from 'react-bootstrap'; +import axios from 'axios'; + +class AuthModal extends React.Component { + constructor(props) { + super(props); + } + + handleRegister() { + axios.post('http://localhost:3000/register', {username: this.username.value, password: this.password.value}, {withCredentials: true}) + .then((response) => { + // console.log("response", response); + if(response.data.success) { + this.props.login(response.data.user); + this.props.toggleLoginModal(); + } else { + console.log("register redirect error", response.data.err); + } + }) + .catch((err) => { + console.log("register post request error", err); + }); + } + + handleLogin() { + axios.post('http://localhost:3000/login', {username: this.username.value, password: this.password.value}, {withCredentials: true}) + .then((response) => { + // console.log("response", response.data.user); + if(response.data.success) { + this.props.login(response.data.user); + this.props.toggleLoginModal(); + } else { + console.log("login redirect error", response.data.err); + } + }) + .catch((err) => { + console.log("login post request error", err); + }); + } + + render() { + // console.log('this.props', this.props); + return( + + + Contained Modal + + + { this.username = input; }}/> + { this.password = input; }}/> + + + + + + + ); + } +} + +AuthModal.propTypes = { + isModalOpen: PropTypes.bool, + toggleLoginModal: PropTypes.func, + // username: PropTypes.string, + // password: PropTypes.string, + login: PropTypes.func, +}; + +export default AuthModal; diff --git a/frontend/components/Feed.js b/frontend/components/Feed.js new file mode 100644 index 0000000..8da990c --- /dev/null +++ b/frontend/components/Feed.js @@ -0,0 +1,82 @@ +// import React from 'react'; +// import PropTypes from 'prop-types'; +// import axios from 'axios'; +// +// const Feed = ( { getPosts } ) => { +// const componentDidMount = () => { +// axios.get('http://localhost:3000/post/all', {withCredentials: true}) +// .then((response) => { +// console.log("response", response); +// if(response.data.success) { +// getPosts(response.data.posts); +// } else { +// console.log("posts error", response.data.err); +// } +// }) +// .catch((err) => { +// console.log("get posts error", err); +// }); +// }; +// +// return ( +//
+//
Post
+//
Post
+//
Post
+//
Post
+//
Post
+//
Post
+//
Post
+//
+// ); +// }; +// +// Feed.propTypes = { +// getPosts: PropTypes.func, +// }; +// +// +// export default Feed; + +import React from 'react'; +import PropTypes from 'prop-types'; +import axios from 'axios'; +import { Link }from 'react-router-dom'; + +class Feed extends React.Component { + constructor(props) { + super(props); + } + + componentDidMount() { + axios.get('http://localhost:3000/post/all', {withCredentials: true}) + .then((response) => { + console.log("response", response); + if(response.data.success) { + this.props.getPosts(response.data.posts); + } else { + console.log("posts error", response.data.err); + } + }) + .catch((err) => { + console.log("get posts error", err); + }); + } + + render() { + console.log(this.props.posts, 'here in feed with the posts!!!!!!!!'); + return( +
+ Hello Posts + {this.props.posts.map((elem) =>
  • {elem.title} - {elem.content} by {elem.userId} at {elem.createdAt}
  • )} +
    + ); + } +} + +Feed.propTypes = { + getPosts: PropTypes.func, + posts: PropTypes.array, +}; + +export default Feed; diff --git a/frontend/components/Header.js b/frontend/components/Header.js new file mode 100644 index 0000000..4504552 --- /dev/null +++ b/frontend/components/Header.js @@ -0,0 +1,16 @@ +import React from 'react'; +import PropTypes from 'prop-types'; + +const Header = ( { } ) => { + return ( +
    + +
    + ); +}; + +Header.propTypes = { +}; + + +export default Header; diff --git a/frontend/components/NewPost.js b/frontend/components/NewPost.js new file mode 100644 index 0000000..f934fe4 --- /dev/null +++ b/frontend/components/NewPost.js @@ -0,0 +1,44 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import { Button } from 'react-bootstrap'; +import axios from 'axios'; + +class NewPost extends React.Component { + constructor(props) { + super(props); + } + + submitPost() { + axios.post('http://localhost:3000/post/new', {title: this.title.value, content: this.content.value}, {withCredentials: true}) + .then((response) => { + // console.log("response", response.data.user); + if(response.data.success) { + console.log('hey'); + this.props.history.push('/'); + } else { + console.log("post error", response.data.err); + } + }) + .catch((err) => { + console.log("submit post request error", err); + }); + } + + render() { + return( +
    +
    + { this.title = input; }}/>
    +
    +