Skip to content

Commit

Permalink
Profile creation / updating feature added / tested -- prettier fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
luckynumberke7in committed Oct 1, 2020
1 parent 279e1e9 commit 88de7b9
Show file tree
Hide file tree
Showing 10 changed files with 1,073 additions and 330 deletions.
1,038 changes: 782 additions & 256 deletions MERN-social-app.sublime-workspace

Large diffs are not rendered by default.

Binary file modified README.md
Binary file not shown.
6 changes: 3 additions & 3 deletions config/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ const connectDB = async () => {
await mongoose.connect(db, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
useUnifiedTopology: true,
});

console.log('MongoDB Connected..');
} catch(err) {
} catch (err) {
console.error(err.message);
// exit after failure
process.exit(1);
}
};

module.exports = connectDB;
module.exports = connectDB;
10 changes: 5 additions & 5 deletions middleware/auth.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const jwt = require('jsonwebtoken');
const config = require('config');

module.exports = function(req, res, next) {
module.exports = function (req, res, next) {
// get token from header -- request, response, and callback func
const token = req.header('x-auth-token');

// check if token exists
if(!token) {
if (!token) {
return res.status(401).json({ msg: 'No token, authorization denied' });
}

Expand All @@ -16,7 +16,7 @@ module.exports = function(req, res, next) {

req.user = decoded.user;
next();
} catch(err) {
res.status(401).json({ msg: 'Token not valid' })
} catch (err) {
res.status(401).json({ msg: 'Token not valid' });
}
};
};
115 changes: 115 additions & 0 deletions models/Profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
const mongoose = require('mongoose');

const ProfileSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'user',
},
company: {
type: String,
},
website: {
type: String,
},
location: {
type: String,
},
status: {
type: String,
required: true,
},
skills: {
type: [String],
required: true,
},
bio: {
type: String,
},
githubusername: {
type: String,
},
experience: [
{
title: {
type: String,
required: true,
},
company: {
type: String,
required: true,
},
location: {
type: String,
},
from: {
type: Date,
required: true,
},
to: {
type: Date,
required: true,
},
current: {
type: Boolean,
default: false,
},
description: {
type: String,
},
},
],
education: [
{
school: {
type: String,
required: true,
},
degree: {
type: String,
required: true,
},
fieldofstudy: {
type: String,
required: true,
},
from: {
type: Date,
required: true,
},
to: {
type: Date,
required: true,
},
current: {
type: Boolean,
default: false,
},
description: {
type: String,
},
},
],
social: {
youtube: {
type: String,
},
twitter: {
type: String,
},
facebook: {
type: String,
},
linkedin: {
type: String,
},
instagram: {
type: String,
},
},
date: {
type: Date,
default: Date.now,
},
});

module.exports = Profile = mongoose.model('profile', ProfileSchema);
16 changes: 8 additions & 8 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true
required: true,
},
email: {
type: String,
required: true,
unique: true
unique: true,
},
password: {
type: String,
required: true
required: true,
},
avatar: {
type: String
type: String,
},
date: {
type: Date,
default: Date.now
}
default: Date.now,
},
});
// create and export User model
module.exports = User = mongoose.model('user', UserSchema);
// create and export User model
module.exports = User = mongoose.model('user', UserSchema);
45 changes: 22 additions & 23 deletions routes/api/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const { check, validationResult } = require('express-validator');

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


// @route GET api/auth
// @description Test Route
// @access Public
Expand All @@ -19,7 +18,7 @@ router.get('/', auth, async (req, res) => {
const user = await User.findById(req.user.id).select('-password');
// '-password' leaves password out
res.json(user);
} catch(err) {
} catch (err) {
console.error(err.message);
res.status(500).send('Server Error');
}
Expand All @@ -29,19 +28,19 @@ router.get('/', auth, async (req, res) => {
// @description Authenticate user + get token
// @access Public


// user and password validator
router.post(
'/', [
'/',
[
check('email', 'Please include a valid email').isEmail(),
check('password', 'Password required').exists()
],
check('password', 'Password required').exists(),
],
// asynchronous callback receiving input email and password, verifies against database w. encryption
async (req, res) => {
// if errors exist, send bad request(400) w.errors array
const errors = validationResult(req);

if(!errors.isEmpty()) {
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}

Expand All @@ -50,46 +49,46 @@ router.post(

try {
// see if user exists
let user = await User.findOne( { email } );
let user = await User.findOne({ email });
console.log(user); // returns correct user w. all info (except encrypted password)
console.log(user.password); // undefined....WHY?

if(!user) {
return res.status(400)
.json({ errors: [{ msg: 'Invalid username / password' }] });
if (!user) {
return res
.status(400)
.json({ errors: [{ msg: 'Invalid username / password' }] });
}
// compare input credentials - username and password
const isMatch = await bcrypt.compare(password, user.password);

if(!isMatch) {
return res.status(400)
if (!isMatch) {
return res
.status(400)
.json({ errors: [{ msg: 'Invalid username / password' }] });
} // use same error msg for both username and password


// get payload for webtoken
const payload = {
user: {
id: user.id
}
id: user.id,
},
};
// callback function returning error or verified json webtoken
jwt.sign(
payload,
payload,
config.get('jwtSecret'),
{ expiresIn: 360000 },
// change to 3600 after testing is done
(err, token) => {
if(err) throw err;
res.json({ token })
if (err) throw err;
res.json({ token });
}
);

} catch(err) {
} catch (err) {
console.error(err.message);
res.status(500).send([ 'Server Error', err.message ]);
res.status(500).send(['Server Error', err.message]);
}
}
);

module.exports = router;
module.exports = router;
2 changes: 1 addition & 1 deletion routes/api/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ const router = express.Router();
// @access Public
router.get('/', (req, res) => res.send('Post route'));

module.exports = router;
module.exports = router;
Loading

0 comments on commit 88de7b9

Please sign in to comment.