This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const mongoose = require('mongoose'); | |
| const CommentSchema = new mongoose.Schema({ | |
| comment:{ | |
| type:String, | |
| trim: true | |
| }, | |
| author:{ | |
| type: mongoose.Schema.Types.ObjectId, | |
| required:true, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express'); | |
| const router = new express.Router() | |
| const User = require('../models/user') | |
| const {ObjectID} = require('mongodb') | |
| module.exports = router |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express'); | |
| const router = new express.Router() | |
| const Post = require('../models/post') | |
| const {ObjectID} = require('mongodb') | |
| module.exports = router |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express') | |
| require('./db/mongoose'); | |
| const userRoutes = require('./router/user') | |
| const PostRoutes = require('./router/post') | |
| const app = express(); | |
| const port = process.env.PORT || 3005 | |
| app.use(express.json()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| router.post('/users', async (req,res) => { | |
| const user = new User(req.body); | |
| try{ | |
| const token = await user.newAuthToken() | |
| res.status(201).send({user, token}) | |
| }catch(e){ | |
| res.status(400).send(e) | |
| } | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| UserSchema.methods.newAuthToken = async function(){ | |
| const user = this | |
| const token = jwt.sign({ _id: user.id.toString() },'thisismynewblog', {expiresIn: "7 days"}) | |
| user.tokens = user.tokens.concat({ token }) | |
| await user.save() | |
| return token | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| UserSchema.pre('save', async function(next){ | |
| const user = this | |
| if(user.isModified('password')){ | |
| user.password = await bcrypt.hash(user.password, 8) | |
| } | |
| next() | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| router.post('/users/login', async (req, res) => { | |
| try { | |
| const user = await User.checkValidCredentials(req.body.email, req.body.password) | |
| const token = await user.newAuthToken() | |
| res.send({ user, token}) | |
| } catch (error) { | |
| res.status(400).send() | |
| } | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| router.post('/users/logout', auth, async (req, res) => { | |
| try { | |
| req.user.tokens = req.user.tokens.filter((token) =>{ | |
| return token.token !== req.token | |
| }) | |
| await req.user.save() | |
| res.send() | |
| } catch (error) { | |
| res.status(500).send() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| router.post('/users/login', async (req, res) => { | |
| try { | |
| const user = await User.checkValidCredentials(req.body.email, req.body.password) | |
| const token = await user.newAuthToken() | |
| res.send({ user, token}) | |
| } catch (error) { | |
| res.status(400).send() | |
| } | |
| }) |