Skip to content

Commit

Permalink
update pw
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavishya2107 committed Feb 11, 2020
1 parent 98bbd45 commit fc6b86b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
3 changes: 3 additions & 0 deletions models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ var articleSchema = new Schema({
type: String,
required: true
},
tagList:{
type:[String]
},
image: {
type: String
},
Expand Down
7 changes: 6 additions & 1 deletion modules/auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const jwt = require('jsonwebtoken');
const slug = require('slug')
const slug = require('slug');
const bcrypt = require('bcryptjs');

module.exports = {
generateJWT: async (user) => {
Expand All @@ -25,5 +26,9 @@ module.exports = {
upateSlug: (article) => {
var slugged = slug(article.title, '-')
article.slug = slugged + '-' + article._id
},
updatePW: (user) => {
var hash = bcrypt.hashSync(user.password, 10)
user.password = hash
}
}
1 change: 1 addition & 0 deletions routes/api/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ router.get('/:slug', async (req, res) => {
var query = req.params.slug
try {
var singleArticle = await Article.findOne({ slug: query })
.populate('author','-following')
console.log(singleArticle)
res.json(singleArticle)
} catch (error) {
Expand Down
11 changes: 11 additions & 0 deletions routes/api/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
var express = require('express');
var router = express.Router();
var Article = require('../../models/article');

var userRouter = require('./users');
var articleRouter = require('./articles');
var profilesRouter = require('./profiles');


router.get('/tags', (req, res) => {
Article.find({})
.populate({path:'tagList'})
.exec((err, tags) => {
if(err) return res.json(err)
res.json({tags})
})
})

router.use('/', userRouter);

router.use('/articles', articleRouter);
router.use('/profiles', profilesRouter);

Expand Down
4 changes: 2 additions & 2 deletions routes/api/profiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ router.post('/:username/follow', async (req, res) => {
if (!user.following.includes(loginUser)) {
var follow = await User.findOneAndUpdate({ username }, { $push: { following: req.user.UserId } })
await User.findByIdAndUpdate(req.user.UserId, { $push: { followers: follow.id } })
res.json({ success: true, msg: "successfully followed" })
res.json({ success: true, follow })
} else {
res.json({ msg: `you have already followed the ${username}` })
res.json({ msg: `you have already followed the ${username}`,follow })
}
} catch (error) {
res.json({ msg: "Issue in following" })
Expand Down
8 changes: 6 additions & 2 deletions routes/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var User = require('../../models/user');
var auth = require('../../modules/auth');
var jwt = require('jsonwebtoken');
var loggedUser = auth.verifyToken;
var updatePW = auth.updatePW;


//register user
Expand Down Expand Up @@ -51,10 +52,13 @@ router.get('/user', async (req, res) => {

//update user
router.put('/user', async (req, res) => {
let { id } = req.body.user;
console.log(req.body.user)
let id = req.user.UserId;
console.log(req.user)
try {
var updatedUser = await User.findByIdAndUpdate(id, req.body.user, { new: true })
console.log(updatedUser)
var updatePassword = await updatePW(updatedUser)
console.log(updatePassword)
res.json({ success: "true", updatedUser })
} catch (error) {
res.status(400).json(error)
Expand Down

0 comments on commit fc6b86b

Please sign in to comment.