Просмотр исходного кода

User token generation, login route and jsonwebtoken dependency

pull/16/head
Djordje Djoric 4 лет назад
Родитель
Сommit
fccc205a97
4 измененных файлов: 124 добавлений и 3 удалений
  1. 71
    0
      package-lock.json
  2. 1
    0
      package.json
  3. 34
    1
      src/models/user.js
  4. 18
    2
      src/routers/user.js

+ 71
- 0
package-lock.json Просмотреть файл

"bcryptjs": "^2.4.3", "bcryptjs": "^2.4.3",
"express": "^4.18.1", "express": "^4.18.1",
"express-jwt": "^7.7.2", "express-jwt": "^7.7.2",
"jsonwebtoken": "^8.5.1",
"mongodb": "^4.6.0", "mongodb": "^4.6.0",
"mongoose": "^6.3.4", "mongoose": "^6.3.4",
"nodemon": "^2.0.16", "nodemon": "^2.0.16",
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/lodash.includes": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz",
"integrity": "sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8="
},
"node_modules/lodash.isboolean": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz",
"integrity": "sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY="
},
"node_modules/lodash.isinteger": {
"version": "4.0.4",
"resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz",
"integrity": "sha1-YZwK89A/iwTDH1iChAt3sRzWg0M="
},
"node_modules/lodash.isnumber": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz",
"integrity": "sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w="
},
"node_modules/lodash.isplainobject": {
"version": "4.0.6",
"resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz",
"integrity": "sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs="
},
"node_modules/lodash.isstring": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz",
"integrity": "sha1-1SfftUVuynzJu5XV2ur4i6VKVFE="
},
"node_modules/lodash.once": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz",
"integrity": "sha1-DdOXEhPHxW34gJd9UEyI+0cal6w="
},
"node_modules/lowercase-keys": { "node_modules/lowercase-keys": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-1.0.1.tgz", "resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-1.0.1.tgz",
"package-json": "^6.3.0" "package-json": "^6.3.0"
} }
}, },
"lodash.includes": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz",
"integrity": "sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8="
},
"lodash.isboolean": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz",
"integrity": "sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY="
},
"lodash.isinteger": {
"version": "4.0.4",
"resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz",
"integrity": "sha1-YZwK89A/iwTDH1iChAt3sRzWg0M="
},
"lodash.isnumber": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz",
"integrity": "sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w="
},
"lodash.isplainobject": {
"version": "4.0.6",
"resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz",
"integrity": "sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs="
},
"lodash.isstring": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz",
"integrity": "sha1-1SfftUVuynzJu5XV2ur4i6VKVFE="
},
"lodash.once": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz",
"integrity": "sha1-DdOXEhPHxW34gJd9UEyI+0cal6w="
},
"lowercase-keys": { "lowercase-keys": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-1.0.1.tgz", "resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-1.0.1.tgz",

+ 1
- 0
package.json Просмотреть файл

"bcryptjs": "^2.4.3", "bcryptjs": "^2.4.3",
"express": "^4.18.1", "express": "^4.18.1",
"express-jwt": "^7.7.2", "express-jwt": "^7.7.2",
"jsonwebtoken": "^8.5.1",
"mongodb": "^4.6.0", "mongodb": "^4.6.0",
"mongoose": "^6.3.4", "mongoose": "^6.3.4",
"nodemon": "^2.0.16", "nodemon": "^2.0.16",

+ 34
- 1
src/models/user.js Просмотреть файл

const validator = require('validator') const validator = require('validator')
const mongoose = require('mongoose') const mongoose = require('mongoose')
const bcrypt = require('bcryptjs') const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const ejwt = require('express-jwt')


const userSchema = new mongoose.Schema({ const userSchema = new mongoose.Schema({
name: { name: {
}, },
password: { password: {
type: String type: String
}
},
tokens: [{
token: {
type: String,
required: true
}
}]
}) })


userSchema.pre('save', async function(next) { userSchema.pre('save', async function(next) {
next() next()
}) })


userSchema.statics.findByCredentials = async (email, password) => {
const user = await User.findOne({email})
if(!user) {
throw new Error('Login unsuccessfull!')
}

const checkMatch = await bcrypt.compare(password, user.password)
console.log(password)
console.log(user.password)
console.log(checkMatch)
if(!checkMatch) {
throw new Error('Wrong password!')
}
return user
}

userSchema.methods.generateAuthToken = async function() {
const user = this
const token = jwt.sign({ _id: user._id.toString() }, 'ovoJeSecret')
user.tokens = user.tokens.concat({ token })
await user.save()

return token
}





const User = mongoose.model('User', userSchema) const User = mongoose.model('User', userSchema)

+ 18
- 2
src/routers/user.js Просмотреть файл

// } // }
// }) // })


router.get('/users', (req, res) => {
router.get('/users', async (req, res) => {
try { try {
if (Object.entries(req.params).length === 0) { if (Object.entries(req.params).length === 0) {
res.status(200).send('getting all users')
const allUsers = await User.find({})
return res.send(allUsers)
} }
else { else {
res.status(400).send('unable to get all users, request was bad') res.status(400).send('unable to get all users, request was bad')
} }
}) })


router.post('/users/login', async (req, res) => {
try {
const user = await User.findByCredentials(req.body.email, req.body.password)
if(!user) {
return res.status(400).send('Invalid email or password!')
}
console.log(user)
const token = await user.generateAuthToken()
console.log(token)
return res.send(user)
} catch(e) {

}
})

module.exports = router module.exports = router

Загрузка…
Отмена
Сохранить