Browse Source

Token invalidation bugfix

pull/16/head
Djordje Djoric 4 years ago
parent
commit
4ce06e06f8
2 changed files with 11 additions and 4 deletions
  1. 5
    2
      src/models/token.js
  2. 6
    2
      src/routes/token.js

+ 5
- 2
src/models/token.js View File

@@ -23,6 +23,7 @@ tokenSchema.statics.findByCredentials = async (email, password) => {
const checkMatch = await bcrypt.compare(password, user.password)
console.log(password)
console.log(user.password)
console.log(checkMatch)
if(checkMatch) {
return user
}
@@ -52,12 +53,14 @@ tokenSchema.statics.refreshAuthToken = async function(token, refreshOptions) {

tokenSchema.statics.destroyToken = async function(token) {
const findUser = await User.findOne({ 'tokens.token': token })
if(!findUser) {
return null
}
findUser.tokens = findUser.tokens.filter((currToken) => {
return currToken.token !== token
})
await findUser.save()
console.log('uspesno')
return jwt.sign(token, 'a', { expiresIn: 1 })
return true
}

const Token = mongoose.model('Token', tokenSchema)

+ 6
- 2
src/routes/token.js View File

@@ -10,7 +10,7 @@ const auth = require('../middleware/auth')
router.post('/login', async (req, res) => {
const findUser = await Token.findByCredentials(req.body.email, req.body.password)
if(!findUser) {
return res.status(400).send('User does not exist, wrong email')
return res.status(400).send('Wrong credentials!')
}

const isValidPassword = await bcrypt.compare(req.body.password, findUser.password)
@@ -24,7 +24,11 @@ router.post('/login', async (req, res) => {
})

router.post('/logout/', async (req, res) => {
const result = Token.destroyToken(req.body.token)
const result = await Token.destroyToken(req.body.token)
if(!result) {
return res.status(404).send('No user has the token provided!')
}
return res.send('Token ' + req.body.token + ' invalidated!')
})

module.exports = router

Loading…
Cancel
Save