我正在尝试实现LDAP活动目录。当我尝试通过前端传递用户凭据时,出现错误无法读取未定义的属性'session'。我认为我没有将会话传递给AD。当我对用户凭据进行硬编码时,我能够对用户进行身份验证。
我该如何解决?
path = require('path'),
logger = require('morgan'),
ActiveDirectory = require('activedirectory'),
bodyParser = require('body-parser'),
dotenv = require('dotenv'),
passport = require('passport'),
session = require('express-session');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(session({
resave : true,
secret : 'kool',
saveUninitialized : true,
cookie : {}
}));
const Strategy = {
url: 'n/a',
baseDN: 'n/a',
username: 'n/a',
password: 'n/a'
}
const ad = new ActiveDirectory(Strategy);
let user;
app.post('/login', async (req, res) => {
user.session;
const username = req.body.username;
const password = req.body.password;
const loginSuccess = await ad.authenticate(username, password, (err, auth, res, req) => {
if(err) {
console.log(`Yo invalid credentials: ***** ${JSON.stringify(err)} ]******* `);
return
}
if(auth) {
req.session.loggedin = true;
req.session.username = username;
res.redirect('/index')
console.log(`User ${username} is Authenticated`);
} else {
// res.send('Auth Failed')
console.log(`Auth failed`)
res.redirect('/')
}
res.end();
});
});
app.get('/index', function(request, response) {
if(request.session.loggedin) {
response.send(`Welcome back, ${request.session.username}`)
res.render('index', {title: 'Main Page'})
} else {
response.redirect('/')
}
response.end();
})