nodebb-plugin-freelog
Advanced tools
Comparing version 0.0.30 to 0.0.31
59
index.js
@@ -10,2 +10,3 @@ const plugin = {}; | ||
const async = module.parent.require('async'); | ||
const jwtHelper = require('./jwt-helper'); | ||
@@ -17,3 +18,3 @@ // 签到处理(回复指定ID的帖子) | ||
const signTid = env === 'production' ? 1 : 2; // 根据环境设定签到帖ID. | ||
console.log(JSON.stringify(data)); | ||
if (data.data.tid !== signTid) { | ||
@@ -27,3 +28,3 @@ return; | ||
taskConfigCode: 'TS000013', | ||
userId: data.data.uid | ||
userId: data.caller.uid | ||
}, { | ||
@@ -65,3 +66,7 @@ headers: { 'content-type': 'application/json' } | ||
plugin.login = function(req, username, password, next) { | ||
console.log('jwt', req.cookies.authInfo); | ||
// 如果存在freelog-jwt 则走JWT授权 | ||
if (req.cookies.authInfo) { | ||
jwtLogin(req.cookies.authInfo, next); | ||
return; | ||
} | ||
if (!username) { | ||
@@ -132,2 +137,50 @@ return next(new Error('[[error:invalid-username]]')); | ||
function jwtLogin(token, next) { | ||
const userInfo = jwtHelper.jwtVerify(token); | ||
if (!userInfo) { | ||
next(new Error('JWT校验失败')); // 一般不会出现此问题,除非能篡改JWT内容 | ||
return; | ||
} | ||
async.waterfall([ | ||
// 根据登录信息获取nodeBB用户信息 | ||
function(callback) { | ||
User.getUserData(userInfo.userId).then(userData => { | ||
callback(null, userData, userInfo); | ||
}).catch(error => callback(error)); | ||
}, | ||
// 判定nodeBB用户是否已同步,如果没同步,则同步 | ||
function(nodeUserInfo, freelogUserInfo, callback) { | ||
if (nodeUserInfo && nodeUserInfo.uid > 0) { | ||
callback(null, nodeUserInfo); | ||
return; | ||
} | ||
User.create({ | ||
uid: freelogUserInfo.userId, | ||
username: freelogUserInfo.username, | ||
password: 'password', // 填啥都无所谓,反正不用nodeBB自身的登录系统. | ||
fullname: freelogUserInfo.username, | ||
picture: 'https://image.freelog.com/headImage/' + userInfo.userId | ||
// email: freelogUserInfo.email 会报错,nodebb会校验邮箱 | ||
}).then(() => { | ||
User.getUserData(freelogUserInfo.userId).then(userData => { | ||
callback(null, userData); | ||
}).catch(error => callback(error)); | ||
}).catch(error => callback(error)); | ||
}, function(nodebbUserInfo, callback) { | ||
// condition = {_key:"group:administrators:members",value:uid } | ||
User.isAdministrator(nodebbUserInfo.uid).then(isAdmin => { | ||
nodebbUserInfo.isAdmin = isAdmin; | ||
callback(null, nodebbUserInfo); | ||
}).catch(error => callback(error)); | ||
} | ||
], function(error, result) { | ||
if (error) { | ||
next(error); | ||
return; | ||
} | ||
next(null, result, '[[success:authentication-successful]]'); | ||
}); | ||
} | ||
module.exports = plugin; |
@@ -5,3 +5,3 @@ { | ||
"description": "NodeBB Forum", | ||
"version": "0.0.30", | ||
"version": "0.0.31", | ||
"homepage": "http://www.nodebb.org", | ||
@@ -8,0 +8,0 @@ "repository": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9372
9
222