我正在尝试通过Steam建立身份验证系统。
当我在文件“ passport-setup.js”中的“ console.log (profile)
”处返回用户的个人资料信息时,我得到以下返回的用户信息未定义:
{ displayName: undefined,
emails: [ { value: undefined } ],
name: { familyName: undefined, givenName: undefined } }
**任何可以帮助我的人,在此先感谢您! **
代码如下:
**app.js**
`` `const express = require('express');
const mongoose = require('mongoose');
const session = require('express-session');
const passport = require('passport');
const authRoutes = require('./routes/auth');
const passportSetup = require('./config/passport-setup');
const keys = require('./config/keys');
mongoose.connect(keys.mongoose.connect, {
useNewUrlParser: true,
useUnifiedTopology: true,
}, () => console.log('Conectado ao MONGO DB'));
const app = express();
//Initialise session
app.use(session({
secret: 's3cr3tStr1nG',
saveUninitialized: false,
resave: true
}));
//Authentication middleware
app.use(passport.initialize());
app.use(passport.session());
//Point to static asset directory
app.use(express.static('public'));
//para rodar a view instalar "yarn add ejs" e o seguinte comando:
app.set('view engine', 'ejs');
//configurando rotas (importacao)
app.use('/auth', authRoutes);
app.get('/', (req, res) => {
res.render('home');
});
const base_url = "http://localhost:3000";
app.listen(3000), () => {
console.log('Servidor Rodando na porta 3000'),
console.log("base_url " + base_url);
}` ``
**rota de autenticação - auth.js**
`` `const router = require('express').Router();
const passport = require('passport');
//auth login
router.get('/login', (req, res) => {
res.render('login');
});
//auth logout
router.get('/logout', (req, res) => {
//handle with passport
res.send('logging out');
});
router.get('/steam', passport.authenticate('steam'), function(req, res) {
// The request will be redirected to Steam for authentication, so
// this function will not be called.
});
router.get('/steam/return',
passport.authenticate('steam', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/');
});
module.exports = router;` ``
**passport-setup.js**
`` ` const passport = require('passport');
const SteamStrategy = require('passport-steam');
const User = require('../models/User');
const keys = require('./keys');
//Determine data to be stored in session
passport.serializeUser(function(user, done) {
//save JSON data to session
done(null, user._json);
});
//Match session data with DB data and parse
passport.deserializeUser(function(obj, done) {
//Search DB for user with session's steam ID
User.findOne({steam_id: obj.steamid},
(err, user) => {
//Fetched object is attached to request object (req.user)
done(err, user);
});
});
passport.use(new SteamStrategy({
returnURL: keys.steam.returnURL,
realm: keys.steam.realm,
profile: keys.steam.profile,
scope: ['profile']
}, (identfy, profile, done) => {
//callback function
User.findOne({ steam_id: profile.id }, (err, user) => {
console.log('1')
if (err) throw err;
console.log('2')
if (!user) {
console.log('3')
var newUser = User({
steam_id: profile.id,
username: profile.displayName,
photo_url: profile.photos[2].value
});
console.log(profile)
newUser.create(function(err) {
if (err) throw err;
console.log('New user ' + profile.displayName + '[' + profile.id + '] created');
});
}
console.log(profile)
});
})
); ` ``
我在passportjs文档中寻找了一种解决方案,但仍然无法解决这个问题,这很奇怪,因为当我使用护照执行Google或Facebook身份验证时,通常会返回。