【发布时间】:2020-02-14 19:39:33
【问题描述】:
目前,我已经设法将用户信息保存在我的数据库中。但是,如果 Steam 数据库中的信息发生变化,我想在他们登录时更新我的数据库中的用户信息。所以在我的数据库里面也是一样的。
以下是我的用户架构中的信息示例
const UserSchema = mongoose.Schema({
username:{
type: String,
},
profileURL:{
type: String,
},
profileImageURL:{
type: String,
},
steamId:{
type: String,
}
});
以下是我的 app.js 的示例。当用户登录时,我检查了用户 steamId 是否存在于我的数据库中,我想更新用户信息,例如用户名、profileURL、profileImageURL 及其 steamID(如果存在),否则我在我的数据库中创建一个新用户。我怎样才能做到这一点?目前,我只是返回 done(null, user)。
passport.use(new SteamStrategy({
returnURL: 'http://localhost:3000/auth/steam/return',
realm: 'http://localhost:3000/',
apiKey: ''
},
function (identifier, done){
var steamId = identifier.match(/\d+$/)[0];
var profileURL = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' + 'api_Key' + '&steamids=' + steamId;
User.findOne({ steamId: steamId}, function(err, user){
if(user){
return done(null, user);
}
else{
request(profileURL, function (error, response, body){
if (!error && response.statusCode === 200)
{
var data = JSON.parse(body);
var profile = data.response.players[0];
var user = new User();
user.username = profile.personaname;
user.profileURL = profile.profileurl;
user.profileImageURL = profile.avatarfull;
user.steamId = steamId;
user.save(function(err){
done(err, user);
});
}
else
{
done(err, null);
}
});
}
});
}));
【问题讨论】:
-
您好,您确定示例完整吗?你在哪里定义
User变量? -
在我的 app.js 中使用 const User = require('./models/user');
标签: node.js mongodb express passport.js