因此,我有一个基于生成器-角度-全栈的小应用程序。我想向当前登录的用户添加一个新帐户(Withing)的凭据。这个想法是,登录的用户也可以添加其他方式,通过使用Withing或Twitter或Facebook进行登录。
以下是我的路线:
router
.get('/', passport.authorize('withings', {
failureRedirect: '/'
}))
.get('/callback', passport.authorize('withings', {
successRedirect : '/settings',
failureRedirect : '/'
}));这是回调实现:
passport.use(new WithingsStrategy({
consumerKey: config.withings.clientID,
consumerSecret: config.withings.clientSecret,
callbackURL: config.withings.callbackURL,
passReqToCallback: true
},
function(req, token, tokenSecret, profile, done) {
console.log('user' + req.user);
return done(null, null);
}
));重点是,当我返回到函数时,即使我是登录的,req.user也始终是undefined。
有谁有主意吗?我读到你需要像deserializeUser和serializeUser这样的几个函数,但是它们从来没有被调用过。
想法?我是个新手,在3-4个晚上之后,我开始感到沮丧:
PS:这是我的配置
app.set('views', config.root + '/server/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(compression());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(cookieParser());
app.use(passport.initialize());
app.use(passport.session()); //persistent login session
// Persist sessions with mongoStore
// We need to enable sessions for passport twitter because its an oauth 1.0 strategy
app.use(session({
secret: config.secrets.session,
resave: true,
saveUninitialized: true,
store: new mongoStore({ mongoose_connection: mongoose.connection })
}));
if ('production' === env) {
app.use(favicon(path.join(config.root, 'public', 'favicon.ico')));
app.use(express.static(path.join(config.root, 'public')));
app.set('appPath', config.root + '/public');
app.use(morgan('dev'));
}
if ('development' === env || 'test' === env) {
app.use(require('connect-livereload')());
app.use(express.static(path.join(config.root, '.tmp')));
app.use(express.static(path.join(config.root, 'client')));
app.set('appPath', 'client');
app.use(morgan('dev'));
app.use(errorHandler()); // Error handler - has to be last
}发布于 2015-07-17 01:54:41
我知道这个问题很老,但如果其他人忽略了这个问题,我认为有两个问题。
首先,在你的回调实现中,你可以说:
return done(null, null);done接口为done([err], [user])。通过将null作为第二个参数传递给done,您的代码表明您没有找到用户,因此passport不会让用户登录。你实际上需要说:
return done(null, profile);其次,您需要移动这些行:
app.use(passport.initialize());
app.use(passport.session());在你的app.use(session({...}))调用下面。passport依赖于快速会话,因此您需要在初始化Passport之前设置快速会话。
希望这能有所帮助!
https://stackoverflow.com/questions/27432149
复制相似问题