Skip to content

Commit

Permalink
get logged in user && middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
o5faruk committed Oct 21, 2018
1 parent 4fb8050 commit 8c52251
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 5 deletions.
4 changes: 4 additions & 0 deletions api/controllers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@ module.exports = {
} catch (error) {
return res.status(500).send({ message: "Server Error" })
}
},
me: async (req, res) => {
console.log("HERE");
return res.json(req.user)
}
}
28 changes: 28 additions & 0 deletions api/middleware/isLoggedIn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const tokenDecoder = require('../services/token/decode'),
User = require('../models/User')

module.exports = async (req, res, next) => {
let auth = req.headers.authorization

if (!auth) {
return res.status(401).json({ "message": "Forbidden" })
}

auth = auth.split(' ')

const auth_type = auth[0]
const token = auth[1]

if (auth[0] !== "Bearer") {
return res.status(401).json({ "message": "Forbidden" })
}

try {
const decoded = await tokenDecoder(token)
req.user = await User.findOne({ _id: decoded._id })
return next()
}
catch (exception) {
return res.status(401).json(exception)
}
}
2 changes: 1 addition & 1 deletion api/models/User.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const mongoose = require('mongoose'),
Schema = mongoose.Schema,
bcrypt = require('bcrypt'),
uniqueValidator = require('mongoose-unique-validator');
uniqueValidator = require('mongoose-unique-validator')


const User = new Schema({
Expand Down
4 changes: 3 additions & 1 deletion api/routes/auth.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const router = require('express').Router()
const AuthController = require('../controllers/AuthController')
const isLoggedIn = require('../middleware/isLoggedIn')

router.post('/register', AuthController.register)
router.post('/login', AuthController.login)
router.get('/me', isLoggedIn, AuthController.me)

module.exports = router;
module.exports = router
2 changes: 1 addition & 1 deletion api/services/token/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ module.exports = async (user) => {
const token = await jwt.sign(user, config.secret, {
expiresIn: 24 * 60 * 60
})
return token;
return token
}
2 changes: 1 addition & 1 deletion api/services/token/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ const config = require('../../../config')

module.exports = async (token) => {
let decoded = await jwt.verify(token, config.secret)
return decoded;
return decoded
}
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ const port = process.env.PORT || 3000

app.listen(port, () => {
console.log(`Server started on port: ${port}`)
});
})

0 comments on commit 8c52251

Please sign in to comment.