jwt token
This commit is contained in:
parent
47394c712b
commit
fcf39ed838
7
app.js
7
app.js
|
|
@ -3,6 +3,7 @@ require('dotenv').config()
|
|||
const express=require('express')
|
||||
const mongoose = require('mongoose')
|
||||
const morgan = require('morgan')
|
||||
const koaJwt = require('koa-jwt')
|
||||
const bodyParser = require('body-parser')
|
||||
|
||||
const EmployeeRoute=require('./routers/employee')
|
||||
|
|
@ -21,6 +22,12 @@ db.once('open',()=>{
|
|||
|
||||
const app=express()
|
||||
|
||||
app.use(koaJwt({ secret: 'yiyuan0911'}).unless({
|
||||
path: [
|
||||
/^\/user\/login/,
|
||||
/^\/user\/register/
|
||||
]
|
||||
}))
|
||||
app.use(morgan('dev'))
|
||||
app.use(bodyParser.urlencoded({extended:true}))
|
||||
app.use(bodyParser.json())
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
const User=require("../model/user")
|
||||
const jwt = require('jsonwebtoken')
|
||||
|
||||
const create=(req,res,next)=>{
|
||||
let obj=new User({
|
||||
Name: req.body.Name,
|
||||
Age: req.body.Age,
|
||||
Sex: req.body.Sex
|
||||
name: req.body.name,
|
||||
account: req.body.account,
|
||||
password: req.body.password,
|
||||
role: req.body.role
|
||||
})
|
||||
obj.save()
|
||||
.then(data=>{
|
||||
|
|
@ -35,6 +37,24 @@ const list=(req,res,next)=>{
|
|||
})
|
||||
}
|
||||
|
||||
const login=(req,res,next)=>{
|
||||
const config={
|
||||
PRIVATE_KEY: 'yiyuan0911',
|
||||
JWT_EXPIRED: 3600
|
||||
}
|
||||
User.find({account:req.body.account})
|
||||
.then(response=>{
|
||||
if (response.password!==req.body.password){
|
||||
res.status(401)
|
||||
return
|
||||
}
|
||||
const token=jwt.sign({data:response,exp: config.JWT_EXPIRED }, config.PRIVATE_KEY)
|
||||
res.json({
|
||||
token
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
module.exports={
|
||||
list,create
|
||||
list,create,login
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
const mongoose = require('mongoose')
|
||||
const Schema = mongoose.Schema
|
||||
|
||||
const customerSchema = new Schema({
|
||||
name: {
|
||||
type: String
|
||||
},
|
||||
designation:{
|
||||
type: String
|
||||
},
|
||||
email:{
|
||||
type: String
|
||||
},
|
||||
phone: {
|
||||
type: String
|
||||
},
|
||||
age: {
|
||||
type: Number
|
||||
}
|
||||
},{timestamps: true})
|
||||
|
||||
const Customer = mongoose.model('Customer',customerSchema)
|
||||
module.exports= Customer
|
||||
|
|
@ -1,8 +1,9 @@
|
|||
const mongoose=require('mongoose')
|
||||
const userSchema=new mongoose.Schema({
|
||||
Name : String,
|
||||
Sex: String,
|
||||
Age: Number
|
||||
name : String,
|
||||
account: String,
|
||||
password: String,
|
||||
role: String
|
||||
})
|
||||
|
||||
const User=mongoose.model("User",userSchema)
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@
|
|||
"dependencies": {
|
||||
"body-parser": "^1.20.3",
|
||||
"express": "^4.21.0",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"koa-jwt": "^4.0.4",
|
||||
"log4js": "^6.9.1",
|
||||
"mongoose": "^8.6.2",
|
||||
"morgan": "^1.10.0",
|
||||
|
|
|
|||
154
pnpm-lock.yaml
154
pnpm-lock.yaml
|
|
@ -14,6 +14,12 @@ importers:
|
|||
express:
|
||||
specifier: ^4.21.0
|
||||
version: 4.21.0
|
||||
jsonwebtoken:
|
||||
specifier: ^9.0.2
|
||||
version: 9.0.2
|
||||
koa-jwt:
|
||||
specifier: ^4.0.4
|
||||
version: 4.0.4
|
||||
log4js:
|
||||
specifier: ^6.9.1
|
||||
version: 6.9.1
|
||||
|
|
@ -52,6 +58,10 @@ packages:
|
|||
resolution: {integrity: sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw==}
|
||||
engines: {node: '>= 0.6'}
|
||||
|
||||
aggregate-error@3.1.0:
|
||||
resolution: {integrity: sha512-4I7Td01quW/RpocfNayFdFVk1qSuoh0E7JrbRJ16nH01HhKFQ88INq9Sd+nd72zqRySlr9BmDA8xlEJ6vJMrYA==}
|
||||
engines: {node: '>=8'}
|
||||
|
||||
anymatch@3.1.3:
|
||||
resolution: {integrity: sha512-KMReFUr0B4t+D+OBkjR3KYqvocp2XaSzO55UcB6mgQMd3KbcE+mWTyvVV7D/zsdEbNnV6acZUutkiHQXvTr1Rw==}
|
||||
engines: {node: '>= 8'}
|
||||
|
|
@ -91,6 +101,9 @@ packages:
|
|||
resolution: {integrity: sha512-iOJg8pr7wq2tg/zSlCCHMi3hMm5JTOxLTagf3zxhcenHsFp+c6uOs6K7W5UE7A4QIJGtqh/ZovFNMP4mOPJynQ==}
|
||||
engines: {node: '>=16.20.1'}
|
||||
|
||||
buffer-equal-constant-time@1.0.1:
|
||||
resolution: {integrity: sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==}
|
||||
|
||||
buffer-from@1.1.2:
|
||||
resolution: {integrity: sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==}
|
||||
|
||||
|
|
@ -110,6 +123,10 @@ packages:
|
|||
resolution: {integrity: sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw==}
|
||||
engines: {node: '>= 8.10.0'}
|
||||
|
||||
clean-stack@2.2.0:
|
||||
resolution: {integrity: sha512-4diC9HaTE+KRAMWhDhrGOECgWZxoevMc5TlkObMqNSsVU62PYzXZ/SMTjzyGAFF1YusgxGcSWTEXBhp0CPwQ1A==}
|
||||
engines: {node: '>=6'}
|
||||
|
||||
concat-map@0.0.1:
|
||||
resolution: {integrity: sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==}
|
||||
|
||||
|
|
@ -172,6 +189,9 @@ packages:
|
|||
resolution: {integrity: sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==}
|
||||
engines: {node: '>=12'}
|
||||
|
||||
ecdsa-sig-formatter@1.0.11:
|
||||
resolution: {integrity: sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==}
|
||||
|
||||
ee-first@1.1.1:
|
||||
resolution: {integrity: sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==}
|
||||
|
||||
|
|
@ -282,6 +302,10 @@ packages:
|
|||
ignore-by-default@1.0.1:
|
||||
resolution: {integrity: sha512-Ius2VYcGNk7T90CppJqcIkS5ooHUZyIQK+ClZfMfMNFEF9VSE73Fq+906u/CWu92x4gzZMWOwfFYckPObzdEbA==}
|
||||
|
||||
indent-string@4.0.0:
|
||||
resolution: {integrity: sha512-EdDDZu4A2OyIK7Lr/2zG+w5jmbuk1DVBnEwREQvBzspBJkCEbRa8GxU1lghYcaGJCnRWibjDXlq779X1/y5xwg==}
|
||||
engines: {node: '>=8'}
|
||||
|
||||
inherits@2.0.4:
|
||||
resolution: {integrity: sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==}
|
||||
|
||||
|
|
@ -311,13 +335,51 @@ packages:
|
|||
jsonfile@4.0.0:
|
||||
resolution: {integrity: sha512-m6F1R3z8jjlf2imQHS2Qez5sjKWQzbuuhuJ/FKYFRZvPE3PuHcSMVZzfsLhGVOkfd20obL5SWEBew5ShlquNxg==}
|
||||
|
||||
jsonwebtoken@9.0.2:
|
||||
resolution: {integrity: sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==}
|
||||
engines: {node: '>=12', npm: '>=6'}
|
||||
|
||||
jwa@1.4.1:
|
||||
resolution: {integrity: sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==}
|
||||
|
||||
jws@3.2.2:
|
||||
resolution: {integrity: sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==}
|
||||
|
||||
kareem@2.6.3:
|
||||
resolution: {integrity: sha512-C3iHfuGUXK2u8/ipq9LfjFfXFxAZMQJJq7vLS45r3D9Y2xQ/m4S8zaR4zMLFWh9AsNPXmcFfUDhTEO8UIC/V6Q==}
|
||||
engines: {node: '>=12.0.0'}
|
||||
|
||||
koa-jwt@4.0.4:
|
||||
resolution: {integrity: sha512-Tid9BQfpVtUG/8YZV38a+hDKll0pfVhfl7A/2cNaYThS1cxMFXylZzfARqHQqvNhHy9qM+qkxd4/z6EaIV4SAQ==}
|
||||
engines: {node: '>= 8'}
|
||||
|
||||
koa-unless@1.0.7:
|
||||
resolution: {integrity: sha512-NKiz+nk4KxSJFskiJMuJvxeA41Lcnx3d8Zy+8QETgifm4ab4aOeGD3RgR6bIz0FGNWwo3Fz0DtnK77mEIqHWxA==}
|
||||
|
||||
lodash-contrib@241.4.14:
|
||||
resolution: {integrity: sha512-uU0lUaevOdRwprSShDldqY4PfG7HorHr0O31Dqc5/81bu6PdaI0qmPZKspkgviKnyHGQfymbDeAO9cByn1jJVQ==}
|
||||
|
||||
lodash.includes@4.3.0:
|
||||
resolution: {integrity: sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==}
|
||||
|
||||
lodash.isboolean@3.0.3:
|
||||
resolution: {integrity: sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==}
|
||||
|
||||
lodash.isinteger@4.0.4:
|
||||
resolution: {integrity: sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==}
|
||||
|
||||
lodash.isnumber@3.0.3:
|
||||
resolution: {integrity: sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==}
|
||||
|
||||
lodash.isplainobject@4.0.6:
|
||||
resolution: {integrity: sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==}
|
||||
|
||||
lodash.isstring@4.0.1:
|
||||
resolution: {integrity: sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==}
|
||||
|
||||
lodash.once@4.1.1:
|
||||
resolution: {integrity: sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==}
|
||||
|
||||
lodash@2.4.1:
|
||||
resolution: {integrity: sha512-qa6QqjA9jJB4AYw+NpD2GI4dzHL6Mv0hL+By6iIul4Ce0C1refrjZJmcGvWdnLUwl4LIPtvzje3UQfGH+nCEsQ==}
|
||||
engines: {'0': node, '1': rhino}
|
||||
|
|
@ -535,6 +597,18 @@ packages:
|
|||
resolution: {integrity: sha512-VBk1bfdaO4gh3OWO8LBuDY2alp0buL8YzQ6t13xyc8PQPrnUg5AgQvINQx3UkS4dom8UGCL597q4Y2+M4TPvmw==}
|
||||
deprecated: This package is no longer supported.
|
||||
|
||||
p-any@2.1.0:
|
||||
resolution: {integrity: sha512-JAERcaMBLYKMq+voYw36+x5Dgh47+/o7yuv2oQYuSSUml4YeqJEFznBrY2UeEkoSHqBua6hz518n/PsowTYLLg==}
|
||||
engines: {node: '>=8'}
|
||||
|
||||
p-cancelable@2.1.1:
|
||||
resolution: {integrity: sha512-BZOr3nRQHOntUjTrH8+Lh54smKHoHyur8We1V8DSMVrl5A2malOOwuJRnKRDjSnkoeBh4at6BwEnb5I7Jl31wg==}
|
||||
engines: {node: '>=8'}
|
||||
|
||||
p-some@4.1.0:
|
||||
resolution: {integrity: sha512-MF/HIbq6GeBqTrTIl5OJubzkGU+qfFhAFi0gnTAK6rgEIJIknEiABHOTtQu4e6JiXjIwuMPMUFQzyHh5QjCl1g==}
|
||||
engines: {node: '>=8'}
|
||||
|
||||
parseurl@1.3.3:
|
||||
resolution: {integrity: sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==}
|
||||
engines: {node: '>= 0.8'}
|
||||
|
|
@ -686,6 +760,10 @@ packages:
|
|||
resolution: {integrity: sha512-2lv/66T7e5yNyhAAC4NaKe5nVavzuGJQVVtRYLyQ2OI8tsJ61PMLlelehb0wi2Hx6+hT/OJUWZcw8MjlSRnxvw==}
|
||||
engines: {node: '>=14'}
|
||||
|
||||
type-fest@0.3.1:
|
||||
resolution: {integrity: sha512-cUGJnCdr4STbePCgqNFbpVNCepa+kAVohJs1sLhxzdH+gnEoOd8VhbYa7pD3zZYGiURWM2xzEII3fQcRizDkYQ==}
|
||||
engines: {node: '>=6'}
|
||||
|
||||
type-is@1.6.18:
|
||||
resolution: {integrity: sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==}
|
||||
engines: {node: '>= 0.6'}
|
||||
|
|
@ -748,6 +826,11 @@ snapshots:
|
|||
mime-types: 2.1.35
|
||||
negotiator: 0.6.3
|
||||
|
||||
aggregate-error@3.1.0:
|
||||
dependencies:
|
||||
clean-stack: 2.2.0
|
||||
indent-string: 4.0.0
|
||||
|
||||
anymatch@3.1.3:
|
||||
dependencies:
|
||||
normalize-path: 3.0.0
|
||||
|
|
@ -795,6 +878,8 @@ snapshots:
|
|||
|
||||
bson@6.8.0: {}
|
||||
|
||||
buffer-equal-constant-time@1.0.1: {}
|
||||
|
||||
buffer-from@1.1.2: {}
|
||||
|
||||
busboy@1.6.0:
|
||||
|
|
@ -823,6 +908,8 @@ snapshots:
|
|||
optionalDependencies:
|
||||
fsevents: 2.3.3
|
||||
|
||||
clean-stack@2.2.0: {}
|
||||
|
||||
concat-map@0.0.1: {}
|
||||
|
||||
concat-stream@1.6.2:
|
||||
|
|
@ -868,6 +955,10 @@ snapshots:
|
|||
|
||||
dotenv@16.4.5: {}
|
||||
|
||||
ecdsa-sig-formatter@1.0.11:
|
||||
dependencies:
|
||||
safe-buffer: 5.2.1
|
||||
|
||||
ee-first@1.1.1: {}
|
||||
|
||||
encodeurl@1.0.2: {}
|
||||
|
|
@ -1002,6 +1093,8 @@ snapshots:
|
|||
|
||||
ignore-by-default@1.0.1: {}
|
||||
|
||||
indent-string@4.0.0: {}
|
||||
|
||||
inherits@2.0.4: {}
|
||||
|
||||
ipaddr.js@1.9.1: {}
|
||||
|
|
@ -1024,12 +1117,58 @@ snapshots:
|
|||
optionalDependencies:
|
||||
graceful-fs: 4.2.11
|
||||
|
||||
jsonwebtoken@9.0.2:
|
||||
dependencies:
|
||||
jws: 3.2.2
|
||||
lodash.includes: 4.3.0
|
||||
lodash.isboolean: 3.0.3
|
||||
lodash.isinteger: 4.0.4
|
||||
lodash.isnumber: 3.0.3
|
||||
lodash.isplainobject: 4.0.6
|
||||
lodash.isstring: 4.0.1
|
||||
lodash.once: 4.1.1
|
||||
ms: 2.1.3
|
||||
semver: 7.6.3
|
||||
|
||||
jwa@1.4.1:
|
||||
dependencies:
|
||||
buffer-equal-constant-time: 1.0.1
|
||||
ecdsa-sig-formatter: 1.0.11
|
||||
safe-buffer: 5.2.1
|
||||
|
||||
jws@3.2.2:
|
||||
dependencies:
|
||||
jwa: 1.4.1
|
||||
safe-buffer: 5.2.1
|
||||
|
||||
kareem@2.6.3: {}
|
||||
|
||||
koa-jwt@4.0.4:
|
||||
dependencies:
|
||||
jsonwebtoken: 9.0.2
|
||||
koa-unless: 1.0.7
|
||||
p-any: 2.1.0
|
||||
|
||||
koa-unless@1.0.7: {}
|
||||
|
||||
lodash-contrib@241.4.14:
|
||||
dependencies:
|
||||
lodash: 2.4.1
|
||||
|
||||
lodash.includes@4.3.0: {}
|
||||
|
||||
lodash.isboolean@3.0.3: {}
|
||||
|
||||
lodash.isinteger@4.0.4: {}
|
||||
|
||||
lodash.isnumber@3.0.3: {}
|
||||
|
||||
lodash.isplainobject@4.0.6: {}
|
||||
|
||||
lodash.isstring@4.0.1: {}
|
||||
|
||||
lodash.once@4.1.1: {}
|
||||
|
||||
lodash@2.4.1: {}
|
||||
|
||||
lodash@3.10.1: {}
|
||||
|
|
@ -1168,6 +1307,19 @@ snapshots:
|
|||
osenv@0.0.3:
|
||||
optional: true
|
||||
|
||||
p-any@2.1.0:
|
||||
dependencies:
|
||||
p-cancelable: 2.1.1
|
||||
p-some: 4.1.0
|
||||
type-fest: 0.3.1
|
||||
|
||||
p-cancelable@2.1.1: {}
|
||||
|
||||
p-some@4.1.0:
|
||||
dependencies:
|
||||
aggregate-error: 3.1.0
|
||||
p-cancelable: 2.1.1
|
||||
|
||||
parseurl@1.3.3: {}
|
||||
|
||||
path-to-regexp@0.1.10: {}
|
||||
|
|
@ -1339,6 +1491,8 @@ snapshots:
|
|||
dependencies:
|
||||
punycode: 2.3.1
|
||||
|
||||
type-fest@0.3.1: {}
|
||||
|
||||
type-is@1.6.18:
|
||||
dependencies:
|
||||
media-typer: 0.3.0
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
/**
|
||||
* 响应成功格式
|
||||
* @param {*} data 数据
|
||||
* @param {*} code 响应码
|
||||
* @param {*} msg 消息
|
||||
* @returns {}
|
||||
*/
|
||||
const responseFormat = (data = {}, msg = 'success',code = 200) => {
|
||||
return {
|
||||
code,
|
||||
msg,
|
||||
data
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
responseFormat
|
||||
}
|
||||
|
|
@ -5,5 +5,6 @@ const userHandler=require('../handler/user')
|
|||
|
||||
router.get('/list',userHandler.list)
|
||||
router.post('/create',userHandler.create)
|
||||
router.post('/login',userHandler.login)
|
||||
|
||||
module.exports=router
|
||||
|
|
@ -1,11 +1,23 @@
|
|||
@url=http://localhost:8080/api/user
|
||||
@token=''
|
||||
|
||||
### create
|
||||
POST {{url}}/create HTTP/1.1
|
||||
Content-Type: application/json
|
||||
Authorization: {{token}}
|
||||
|
||||
{
|
||||
"Name": "bbbb",
|
||||
"Sex": "male",
|
||||
"Age": 32
|
||||
}
|
||||
|
||||
### login
|
||||
POST {{url}}/login HTTP/1.1
|
||||
Content-Type: application/json
|
||||
Token: {{token}}
|
||||
|
||||
{
|
||||
"account": "bbbb",
|
||||
"password": "male"
|
||||
}
|
||||
Loading…
Reference in New Issue