更新依赖项

This commit is contained in:
2017-07-16 22:56:44 +08:00
parent 3df9391bc1
commit 6232645481
8 changed files with 127 additions and 43 deletions

49
src/controllers/auth.js Normal file
View File

@ -0,0 +1,49 @@
import jwt from 'jsonwebtoken'
import fs from 'fs'
import path from 'path'
const publicKey = fs.readFileSync(path.join(__dirname, '../../publicKey.pub'))
// 用户登录的时候返回token
// let token = jwt.sign({
// userInfo: userInfo // 你要保存到token的数据
// }, publicKey, { expiresIn: '7d' })
/**
* 检查授权是否合法
*/
export let CheckAuth = (ctx) => {
let token = ctx.request.header.authorization
try {
let decoded = jwt.verify(token.substr(7), publicKey)
if (decoded.userInfo) {
return {
status: 1,
result: decoded.userInfo
}
} else {
return {
status: 403,
result: {
errInfo: '没有授权'
}
}
}
} catch (err) {
return {
status: 503,
result: {
errInfo: '解密错误'
}
}
}
}
export let Post = (ctx) => {
switch (ctx.params.action) {
case 'check':
return CheckAuth(ctx).then(result => { ctx.body = result })
default:
return CheckAuth(ctx).then(result => { ctx.body = result })
}
}