Skip to content

Commit

Permalink
solution 4.20*: bloglist expansion, step8. Refactoring to take the to…
Browse files Browse the repository at this point in the history
…ken to a middleware
  • Loading branch information
patchamama committed Sep 11, 2023
1 parent 41f1994 commit b2327eb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ npm test
## d. Token authentication

- [Exercises 4.15.-4.23.](https://fullstackopen.com/en/part4/token_authentication#exercises-4-15-4-23)
_Solution details: [4.15](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/5ec001cec3b44f41a111681af2ae785289d76b6d) | [4.16](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/7839750f9aa7d52deaa62b6d8a8eafa46dd98ca1) | [4.17](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/fd871d2de79352ff62c26c6aeec438fe43f7167a) | [4.18](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/530d7c2eab9c8ce3bbfd2220e904290e28f9b262) | [4.19]() | [4.20]() | [4.21]() | [4.22]() | [4.23]()_
_Solution details: [4.15](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/5ec001cec3b44f41a111681af2ae785289d76b6d) | [4.16](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/7839750f9aa7d52deaa62b6d8a8eafa46dd98ca1) | [4.17](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/fd871d2de79352ff62c26c6aeec438fe43f7167a) | [4.18](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/530d7c2eab9c8ce3bbfd2220e904290e28f9b262) | [4.19](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/41f1994a2a145dc97b01e4477efb8b689d626c47) | [4.20]() | [4.21]() | [4.22]() | [4.23]()_

# Deploy
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ app.use(cors())
app.use(express.static('build'))
app.use(express.json())
app.use(middleware.requestLogger)
app.use(middleware.tokenExtractor)

app.use('/api/login', loginRouter)
app.use('/api/users', usersRouter)
Expand Down
21 changes: 11 additions & 10 deletions controllers/blogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const User = require('../models/user')
const jwt = require('jsonwebtoken')
require('express-async-errors')

const getTokenFrom = (request) => {
const authorization = request.get('authorization')
if (authorization && authorization.toLowerCase().startsWith('bearer ')) {
return authorization.substring(7)
}
return null
}
// const getTokenFrom = (request) => {
// const authorization = request.get('authorization')
// if (authorization && authorization.toLowerCase().startsWith('bearer ')) {
// return authorization.substring(7)
// }
// return null
// }

blogsRouter.get('/', async (request, response) => {
const blogs = await Blog.find({}).populate('user', { username: 1, name: 1 })
Expand All @@ -35,9 +35,10 @@ blogsRouter.post('/', async (request, response) => {
const body = request.body

// Authentication is required
const token = getTokenFrom(request)
const decodedToken = jwt.verify(token, process.env.SECRET)
if (!token || !decodedToken.id) {
// const token = getTokenFrom(request)
// const decodedToken = jwt.verify(token, process.env.SECRET)
const decodedToken = jwt.verify(request.token, process.env.SECRET)
if (!request.token || !decodedToken.id) {
return response.status(401).json({ error: 'token missing or invalid' })
}
const user = await User.findById(decodedToken.id)
Expand Down
13 changes: 13 additions & 0 deletions utils/middleware.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
const logger = require('./logger')

const tokenExtractor = (request, response, next) => {
const authorization = request.get('authorization')
// console.log('authorization:', authorization)
if (authorization && authorization.toLowerCase().startsWith('bearer ')) {
request.token = authorization.substring(7)
// console.log('request.token:', request.token)
} else {
request.token = null
}
next()
}

const requestLogger = (request, response, next) => {
logger.info('Method:', request.method)
logger.info('Path: ', request.path)
Expand Down Expand Up @@ -34,4 +46,5 @@ module.exports = {
requestLogger,
unknownEndpoint,
errorHandler,
tokenExtractor,
}

0 comments on commit b2327eb

Please sign in to comment.