Permalink
Browse files

Adds auth, sessions, posts, and user posts

  • Loading branch information...
kbbushman
kbbushman committed Feb 9, 2019
1 parent 5f43ee2 commit 130de693857c2378817ccc8dfd81f0969306ecd5
Showing with 272 additions and 51 deletions.
  1. +42 −0 controllers/authController.js
  2. +58 −0 controllers/postsController.js
  3. +55 −0 controllers/usersController.js
  4. +19 −0 models/Post.js
  5. +13 −2 models/User.js
  6. +1 −0 models/index.js
  7. +53 −0 package-lock.json
  8. +2 −0 package.json
  9. +29 −49 server.js
@@ -0,0 +1,42 @@
const express = require('express');
const router = express.Router();

const db = require('../models');

// USER REGISTER
router.post('/register', (req, res) => {
// Simple registration example. In a real app you would encrypt the password before creating the new user.
db.User.create(req.body, (err, newUser) => {
if (err) res.json(err);
res.json({
status: 200,
message: 'Registration successful. Please login'
});
});
});

// USER SESSION/LOGIN
router.post('/login', (req, res) => {
db.User.findOne({username: req.body.username}, (err, foundUser) => {
if (err) res.json(err);

// Simple example here. In a real app you would encrypt the req.body.password first and compare it to the encrypted password in foundUser.password
if (foundUser.password === req.body.password) {
req.session.loggedIn = true;
req.session.username = foundUser.username;
req.session.userId = foundUser._id;

res.json({
status: 200,
message: 'Login successful'
});
} else {
res.json({
status: 401,
message: 'Error: Username or password are incorrect'
})
}
});
});

module.exports = router;
@@ -0,0 +1,58 @@
const express = require('express');
const router = express.Router();

const db = require('../models');

// POST INDEX
router.get('/', (req, res) => {
// User must be logged in to hit this endpoint
if (req.session.loggedIn) {
db.Post.find({}, (err, allPosts) => {
if (err) res.json(err);
res.json(allPosts);
});
} else {
res.json({
status: 401, // Unauthorized status
message: 'Error: Unauthorized. Please login and try again'
})
}
})

// // POST SHOW (without populating user data)
// router.get('/:id', (req, res) => {
// db.Post.findById(req.params.id, (err, post) => {
// if (err) res.json(err);
// res.json(posts);
// });
// });

// POST SHOW (with populated user data from userId)
router.get('/:id', (req, res) => {
db.Post.findById(req.params.id)
.populate('userId')
.exec(function (err, post) {
if (err) res.json(err);
res.json(post);
});
});

// POST CREATE
router.post('/', (req, res) => {
if (req.session.loggedIn) {
db.Post.create(req.body, (err, newPost) => {
if (err) res.json(err);
newPost.userId = req.session.userId;
newPost.save((err, savedPost) => {
res.json(newPost);
});
});
} else {
res.json({
status: 401,
message: 'Error: Unauthorized. Please login and try again'
})
}
});

module.exports = router;
@@ -0,0 +1,55 @@
const express = require('express');
const router = express.Router();

const db = require('../models');

// USER INDEX
router.get('/', (req, res) => {
db.User.find({}, (err, allUsers) => {
if (err) res.json(err);
res.json(allUsers);
});
});

// USER SHOW
router.get('/:id', (req, res) => {
db.User.findById(req.params.id, (err, foundUser) => {
if (err) res.json(err);
res.json(foundUser);
});
});

// USER POSTS INDEX
router.get('/:id/posts', (req, res) => {
db.Post.find({userId: req.params.id}, (err, foundPosts) => {
if (err) res.json(err);
res.json(foundPosts);
});
});

// USER NEW
router.post('/', (req, res) => {
db.User.create(req.body, (err, newUser) => {
if (err) res.json(err);
res.json(newUser);
});
});

// USER UPDATE
router.put('/:id', (req, res) => {
db.User.findByIdAndUpdate(req.params.id, req.body, { new: true }, (err, updatedUser) => {
if (err) res.json(err);
res.json(updatedUser);
});
});

// USER DESTROY
router.delete('/:id', (req, res) => {
db.User.findByIdAndRemove(req.params.id, (err, deletedUser) => {
if (err) res.json(err);
// res.json(deletedUser);
res.json({success: `User id ${deletedUser._id} was removed.`})
});
});

module.exports = router;
@@ -0,0 +1,19 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const postSchema = new Schema({
title: String,
body: String,
userId: {
type: Schema.Types.ObjectId,
ref: 'User'
},
date_created: {
type: Date,
default: Date.now
}
});

const Post = mongoose.model('Post', postSchema);

module.exports = Post;
@@ -3,9 +3,20 @@ const Schema = mongoose.Schema;

const userSchema = new Schema({
name: String,
username: String,
email: String,
username: {
type: String,
required: true,
unique: true,
},
email: {
type: String,
required: true
},
password: String,
date_created: {
type: Date,
default: Date.now
}
});

const User = mongoose.model('User', userSchema);
@@ -15,4 +15,5 @@ mongoose.connection.on('disconnected', () => console.log(`MongoDB diconnected fr

module.exports = {
User: require('./User'),
Post: require('./Post'),
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -11,7 +11,9 @@
"license": "MIT",
"dependencies": {
"body-parser": "^1.18.3",
"cors": "^2.8.5",
"express": "^4.16.4",
"express-session": "^1.15.6",
"mongoose": "^5.4.9"
}
}
Oops, something went wrong.

0 comments on commit 130de69

Please sign in to comment.