我一直在用Passport,Express 4和Jade创建一个应用程序.我想向用户显示一个在登录时更改的导航栏.
但是,我无法访问req.user以查找除了调用isLoggedIn的配置文件页面之外的任何其他页面:
function isLoggedIn(req, res, next) {
// if user is authenticated in the session, carry on
if (req.isAuthenticated())
return next()
// if they aren't redirect them to the home page
res.redirect("/login")
}
Run Code Online (Sandbox Code Playgroud)
使用任何其他功能在未登录时不重定向用户会导致req.user正在运行undefined.我得到这样的用户:
router.get("/profile", isLoggedIn, function(req, res) {
res.render("profile", {
title: 'Gebruikersprofiel van ' + req.user.firstname + " " + req.user.lastname,
user: req.user // get the user out of session and pass to template
})
})
Run Code Online (Sandbox Code Playgroud)
并且没有调用isLoggedIn(这不起作用):
router.get("/", function(req, res) {
// serve index.html
res.render("index", { …Run Code Online (Sandbox Code Playgroud) 正如很多人到目前为止所做的那样,我正在将FragmentTabsPager实现到我的应用程序中,该应用程序正在使用ActionBarSherlock 4.0.但是,我输了.
碎片,以及Google围绕它的所有小想法,计划和方法,让我感到困惑.如果有人可以看看我的代码并引导我完成这个,提供帮助使其工作,我会感谢他们一千次:D.
我有另一个项目,有一个ViewPager的开头,但Tabs只是混合得更好,特别是它们在横向和平板电脑的ActionBar中.
我的代码全部压缩并准备好在这里:http: //dl.dropbox.com/u/21807195/Antonius%20College%202.zip
提前致谢!
我在我的一个应用程序中使用了带有两个MenuItem的ABS 4.0,但发现了一个小错误:当按下第二个MenuItem时,它与第一个完全相同...
我已经尝试了我能想到的一切,但它没有用.我改变了onOptionItemSelected,因为我认为这是我需要编辑的方法.
编辑:
我一直在关注@ Ollie的建议,但LogCat和Debug都没有显示出奇怪的东西.也许它在代码的其他部分,或ABS的声明?这是整个代码,如果你能看一遍,那就太好了!
整个Activity的代码,因为它可能在其他地方?
package bas.sie.Antonius;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
public class TeacherInfo extends SherlockActivity {
String URLhome;
String Info;
String TeacherAb;
TextView mTxtvInfo;
Button mBtnTeacherStSchedule;
Button mBtnTeacherDaySchedule;
private static String mainUrl = "http://www.carmelcollegegouda.nl/site_ant/";
private static String endUrl = ".htm";
private static String[] myUrls = { "roosters/dagroosters/Doc_V1_",
"roosters/standaardroosters/Doc1_" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) …Run Code Online (Sandbox Code Playgroud) 之前我曾问过类似的问题,但我注意到它出现在Javascript部分.我对现在可能出现的问题有更具体的想法.
基本上,req.session.passport在我的日志中是空的.每当我开始浏览我的网站时,req.user就会变得未定义,因为会话已经没有Passport登录用户了.
我想知道是否有人知道如何解决这个问题?也许这只是Passport配置或整个Express设置中的错误?
App.js:
var express = require("express"),
bodyParser = require("body-parser"),
mongodb = require("mongodb"),
mongoose = require("mongoose"),
uriUtil = require("mongodb-uri"),
morgan = require("morgan"),
session = require("express-session"),
passport = require("passport"),
flash = require("connect-flash"),
ip = "hidden",
port = process.env.PORT || 80
var app = express()
app.disable("x-powered-by")
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}))
app.use(morgan("dev")); // log every request to the console
// required for passport
app.use(session({
secret: "hidden",
key: 'asdasdasd',
cookie: { maxAge: 60000, secure: false },
resave: true,
saveUninitialized: false
})); // …Run Code Online (Sandbox Code Playgroud)