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

User token generation, login route and jsonwebtoken dependency

pull/16/head
Djordje Djoric 3 лет назад
Родитель
С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 Просмотреть файл

@@ -12,6 +12,7 @@
"bcryptjs": "^2.4.3",
"express": "^4.18.1",
"express-jwt": "^7.7.2",
"jsonwebtoken": "^8.5.1",
"mongodb": "^4.6.0",
"mongoose": "^6.3.4",
"nodemon": "^2.0.16",
@@ -1445,6 +1446,41 @@
"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": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-1.0.1.tgz",
@@ -3727,6 +3763,41 @@
"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": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-1.0.1.tgz",

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

@@ -16,6 +16,7 @@
"bcryptjs": "^2.4.3",
"express": "^4.18.1",
"express-jwt": "^7.7.2",
"jsonwebtoken": "^8.5.1",
"mongodb": "^4.6.0",
"mongoose": "^6.3.4",
"nodemon": "^2.0.16",

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

@@ -1,6 +1,8 @@
const validator = require('validator')
const mongoose = require('mongoose')
const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const ejwt = require('express-jwt')

const userSchema = new mongoose.Schema({
name: {
@@ -11,7 +13,13 @@ const userSchema = new mongoose.Schema({
},
password: {
type: String
}
},
tokens: [{
token: {
type: String,
required: true
}
}]
})

userSchema.pre('save', async function(next) {
@@ -30,6 +38,31 @@ userSchema.pre('save', async function(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)

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

@@ -17,10 +17,11 @@ const app = express()
// }
// })

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

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

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