Forcing UID value
-
@julian it look getUserField() need nodebb uid i've to get userdata having the remoteid (the customfield i've added)
plugin.filterUserCreate = function (data, callback) {
data.user.auid = data.data.auid;
callback(null, data);
};plugin.whitelistFields = function(data, callback) {
data.whitelist.push('auid');
callback(null, data);
}; -
Yeah you'll have to save a map of auids to uids in the database.
-
@julian I'm trying to understand how make an hook for getUserDataByField, but pheraps i'm too noob, i write what i've done :
i've added the user create hook in plugin.json :
{ "hook": "filter:user.create", "method": "filterUserCreate" },
than i've added the function to the library lo listen for 'filter:user.create' :
plugin.filterUserCreate = function (data, callback) { data.user.auid = data.data.auid; callback(null, data); };
I've added the whitelist hook :
{ "hook": "filter:user.whitelistFields", "method": "whitelistFields" },
than i've added the function to the library to listen for 'filter:user.whitelistFields' :
plugin.whitelistFields = function(data, callback) { data.whitelist.push('auid'); callback(null, data); };
At this point i've checked and everythings work, on user create i've also the 'auid' fields with relative value, now i need a function to get user data knowing the remote id field 'auid', i've checked in controllers/user.js and i've found 'getUserDataByField' function wich should help me for my pourpose, but it's limited to uid, username and email.
I've begin adding the hook for getUserDataByField function in plugin.json :{ "hook": "filter:user.getUserDataByField", "method": "getUserDataByField" }
than i've begin to adding the filter listener in the library :
plugin.getUserDataByField = function (callerUid, field, fieldValue, callback) { if (field === 'auid') { user.getUidByAUid(fieldValue, next); }
Than i've loose my self
-
I'm getting confused , i would need to have a function like getUidByEmail but for my added field AUID :
User.getUidByEmail = function (email, callback) {
db.sortedSetScore('email:uid', email.toLowerCase(), callback);
};i need :
User.getUidByAUID = function (auid, callback) {
db.sortedSetScore('auid:uid', auid, callback);
};What i've to do to make that function with a plugin ? I've been able to make it work modifing nodebb source user/index.js , but i don't understad how i can do with a plugin without modifing nodebb core.
-
@Simos said in Forcing UID value:
auid:uid
Just put it in your plugin.
plugin.getUserDataByField = function (callerUid, field, fieldValue, callback) { if (field === 'auid') { user.getUidByAUid(fieldValue, next); } ... }; plugin.getUidByAUid= funtion(auid, callback) { db.sortedSetScore('auid:uid', auid, callback); };
-
@Simos require.main.require is preferred:
require.main.require('./src/database')
-
What i'm doing wrong ? I get error on getUserDataByField :
var passport = module.parent.require('passport'), passportLocal = module.parent.require('passport-local').Strategy, plugin = {}; var async = require('async'); var request = require('request'); plugin.filterUserCreate = function (data, callback) { data.user.auid = data.data.auid; callback(null, data); }; plugin.whitelistFields = function(data, callback) { data.whitelist.push('auid'); callback(null, data); }; plugin.thirdLogin = function() { passport.use(new passportLocal({passReqToCallback: true}, plugin.continueLogin)); }; var db = require.main.require('./src/database'); plugin.getUserDataByField = function (callerUid, field, fieldValue, callback) { if (field === 'auid') { user.getUidByAUid(fieldValue, next); } }; plugin.getUidByAUid = function(auid, callback) { db.sortedSetScore('auid:uid', auid, callback); }; plugin.continueLogin = function(req, username, password, next) { var user = module.parent.require('../user'); var userObj = {} async.waterfall([ function (callback) { if (!username || !password) { return callback(new Error('Il nome utente o la password non possono essere vuoti')); } else { request.post({url:'https://www.simosnap.org/rest/service.php/rlogin', form:{username: username, password: password}}, function(error, response, body) { if (!error && response.statusCode == 200) { const data = JSON.parse(body) //console.log(data); if (data.ERROR == null) { if (data.UNCONFIRMED == null) { userObj = data return callback(null, userObj); } else { //console.log(data.msg) return callback(new Error("Account non confermato.")); } } else { //console.log(data.msg) return callback(new Error("Accesso non riuscito.")); } } else { return callback(new Error('Errore di rete...')); } if (error) { return callback(new Error('Errore')); } }) } }, function (userObj, callback) { //user.getUidByAUID(userObj.uid, callback); user.getUserDataByField('1', 'auid', userObj.uid, function(err, userData) { console.log(userData.auid); return callback(userData.auid, null); }); }, function (_uid, callback) { console.log(_uid); .........
-
@julian I think his problem is actually that he's not importing the user module from NodeBB:
var user = require.main.require('./src/user');
-
@Simos please don't use module.parent.require, use require.main.require instead, like I said before.
Yes, you have it in there once, but you should be importing it at a higher level. The way it is, it's only available within that one function, but you use it in a different hook entirely.