Iam試圖爲我的節點js應用程序提供身份驗證。對於使用express-session npm的Iam。基本上我在嘗試的是當我的憑據在我的/登錄頁面中正確時。它應該啓動會話並帶我到/ admin頁面。所以/ admin可以在其他標籤中直接訪問一段時間。當我點擊註銷按鈕時,它應該帶我到主頁面,會話應該被取消,並且/ admin頁面不應該直接訪問並重定向到/登錄頁面。快速會話不能使用「錯誤:發送後無法設置標題」。
我試過在登錄成功時開始會話,但它給了我錯誤:發送後無法設置標題。
app.js
var session = require('express-session');
app.use(session({
secret: 'securedsession',
resave: false,
saveUninitialized: true,
cookie: {
secure: true,
resave: true,
saveUninitialized: true,
maxAge: new Date(Date.now() + 3600000)
}
}));
var auth = function(req, res, next) {
if (req.session && req.session.user === "admin" && req.session.admin)
return next();
else
return res.sendStatus(401);
};
app.post('/verifylogin', function(req, res) {
var username = req.headers.uname;
var password = req.headers.pwd;
if (!username || !password) {
res.send('login failed');
} else if (username === "admin" || password === "adminnn") {
req.session.user = "admin";
req.session.admin = true;
res.contentType('application/json');
var data = JSON.stringify('/admin');
res.header('Content-Length', data.length);
res.send(data);
}
app.get('/logout', function(req, res) {
req.session.destroy();
res.send("logout success!");
});
app.get('/admin', auth, function(req, res, next) {
res.sendFile(__dirname + '/public/views/admin.html');
});
的login.html
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function login() {
var uname = document.getElementById('uname').value;
var pwd = document.getElementById('pwd').value;
$.ajax({
url: '/verifylogin',
type: "POST",
headers: {
"uname": uname,
"pwd": pwd
},
success: function(data) {
window.location = data;
}
});
}
</script>
<h1>Login</h1>
<p>
<label class="control-label">Username</label>
<input type="text" placeholder="Username" id="uname" />
</p>
<p>
<label class="control-label">Password</label>
<input type="password" placeholder="Password" id="pwd" /><br/>
</p>
<p>
<button type="submit" class="btn btn-primary" onclick="login()">Login</button>
</p>
有人能幫我出了錯誤,我在做如有這裏如何解決這一問題,並開始會話
*********編輯
建議後的最新代碼,即使信用是正確的,它被重定向我未經授權,而不是,/管理員。可以ü看看
app.js
var app = express();
var d = new Date();
app.use(methodOverride());
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(session({
secret: 'securedsession',
resave: false,
saveUninitialized: true,
cookie: {
secure: true,
resave: true,
saveUninitialized: true,
maxAge: d.getTime() + 10
}
}));
app.set('view engine', 'pug');
app.post('/verifylogin', function(req, res) {
var username = req.body.uname;
var password = req.body.pwd;
var uname = "admin";
var pwd = "[email protected]";
if (uname == username && pwd == password) {
console.log("success");
req.session.user = "admin";
req.session.admin = true;
req.session.save();
console.log("resdasdasda", req.session);
res.contentType('application/json');
var data = JSON.stringify('/admin');
res.header('Content-Length', data.length);
res.send(data);
} else {
console.log("fail");
res.send("Bad luck");
}
});
var auth = function(req, res, next) {
if (req.session && req.session.user === "admin" && req.session.admin)
return next();
else
return res.sendStatus(401);
};
app.get('/', function(req, res, next) {
res.sendFile(path.join(__dirname + '/public/views/home.html'));
});
app.get('/login', function(req, res, next) {
res.sendFile(__dirname + '/public/views/login.html');
});
app.get('/admin', auth, function(req, res, next) {
res.sendFile(__dirname + '/public/views/admin.html');
});
app.get('/logout', function(req, res) {
req.session.destroy();
res.send("logout success!");
});
module.exports = app;
HTML
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function login() {
var uname = document.getElementById('uname').value;
var pwd = document.getElementById('pwd').value;
$.ajax({
url : '/verifylogin',
type : "POST",
data : {
"uname":uname,
"pwd" :pwd
},
success: function(data) {
window.location = data;
}
});
}
</script>
<h1>Login</h1>
<p>
<label class="control-label">Username</label>
<input type="text" placeholder="Username" id="uname" />
</p>
<p>
<label class="control-label">Password</label>
<input type="password" placeholder="Password" id="pwd" /><br/>
</p>
<p>
<button type="submit" class="btn btn-primary" onclick="login()">Login</button>
</p>
你能否正確縮進你的問題中的代碼。它的方式實際上是不可讀的。 – jfriend00
@ jfriend00我已經正確地縮進了。你看看,爲什麼它沒有帶我到/管理 – user7350714