具有NodeJS身份验证解决方案的CouchDB [英] CouchDB with NodeJS authentication solution
问题描述
在node.js中,成功登录后,我的身份验证库(lockit)未设置会话。 (免责声明:我不宽容使用lockit,我认为它在各个方面都非常糟糕。)
In node.js my auth library (lockit) is not setting session after logging in successfully. (Disclaimer: I am not condoning the use of lockit, I think it is positively terrible in every way.)
我阅读了lockit源代码并找到了以下代码:
I read lockit source code and found this code:
// create session and save the name and email address
req.session.name = user.name;
req.session.email = user.email;
这是我的快速配置:
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(cookieSession({
secret: 'this is my super secret string'
}));
app.use(lockit.router);
与用户登录后,我使用相同的浏览器发送另一个请求,并且cookie相同这是从登录请求中设置的...
成功登录后,这里显示的是Set-Cookies:
After I login with a user, I send another request with the same browser and the cookie is the same that gets set from the login request...
After successful login here is the Set-Cookies I get:
Set-Cookie express:sess=eyJmYWlsZWRMb2dpbkF0dGVtcHRzIjowLCJuYW1lIjoianVzdGluIiwiZW1haWwiOiJqdXN0aW5Ad2ViaW52ZXJ0ZXJzLmNvbSIsImxvZ2dlZEluIjp0cnVlfQ==; path=/; httponly
Set-Cookie express:sess.sig=FMcv9fswWmWG6A7hpOEnEysbqd4; path=/; httponly
然后登录后我的请求包含以下cookie:
Then my request after the login contains these cookies:
express:sess eyJmYWlsZWRMb2dpbkF0dGVtcHRzIjowLCJuYW1lIjoianVzdGluIiwiZW1haWwiOiJqdXN0aW5Ad2ViaW52ZXJ0ZXJzLmNvbSIsImxvZ2dlZEluIjp0cnVlfQ==
express:sess.sig FMcv9fswWmWG6A7hpOEnEysbqd4
但是即使有cookie,我也会得到:
req.session === undefined
But even though the cookie is there, I'm getting: req.session === undefined
我以前从未使用过这些技术,所以我做的可能很愚蠢……
I've never used these technologies before, so it could be something really stupid that I am doing...
推荐答案
我是Lockit的作者,我相信我们会找到解决您问题的方法。您是否已完成所有必需的步骤来安装Lockit?这是我刚刚做的,并且工作正常。
I'm the author of Lockit and I'm sure we will find a solution to your problem. Did you go through all the required steps to install Lockit? Here is what I just did and it works fine.
-
创建一个新的Express应用(在我的情况下为Express 4.2.0) 。
Create a new Express app (Express 4.2.0 in my case).
express
安装Express依赖项。
Install Express dependencies.
npm install
安装Lockit和会话。
Install Lockit and Sessions.
npm install lockit cookie-session --save
安装CouchDB适配器。
Install CouchDB adapter.
npm install lockit-couchdb-adapter --save
使用CouchDB的URL创建 config.js
// settings for local CouchDB
exports.db = 'http://127.0.0.1:5984/';
使用配置启动Lockit。
Initiate Lockit with your config.
// in your header
var cookieSession = require('cookie-session');
var Lockit = require('lockit');
var config = require('./config.js');
var lockit = new Lockit(config);
// after all your other middleware
app.use(cookieSession({
secret: 'my super secret String'
}));
app.use(lockit.router);
启动您的应用。
Start your app.
DEBUG=tmp ./bin/www
您现在可以导航到 http:// localhost:3000 / signup
并创建一个新用户。如果您尚未设置任何电子邮件服务器,则必须查看数据库(在我的情况下为 http://127.0.0.1:5984/_utils/database.html?_users
)作为您的注册令牌。然后转到 http:// localhost:3000 / signup /:token
激活您的新用户。
You can now navigate to http://localhost:3000/signup
and create a new user. If you haven't set up any email server you have to look in your database (in my case at http://127.0.0.1:5984/_utils/database.html?_users
) for your signup token. Then go to http://localhost:3000/signup/:token
to activate your new user.
很好,您现在可以使用用户名(或电子邮件)和密码登录。要使用自己的路线访问当前登录的用户,请使用 req.session
。
Great, you can now use your username (or email) and password to log in. To access the currently logged in user in your own routes use req.session
.
// default routes from Express
app.use('/', routes);
app.use('/users', users);
// your own custom route
app.get('/awesome', function(req, res) {
console.log(req.session.name);
console.log(req.session.email);
res.send('awesome');
});
我希望我能为您提供帮助。如果您还有其他问题,请告诉我。
I hope I could help you. If you've got any other problems just let me know.
这篇关于具有NodeJS身份验证解决方案的CouchDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!