首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Hapi-auth cookie设置cookie,但不进行validationFunc调用并抛出未经授权的

Hapi-auth cookie设置cookie,但不进行validationFunc调用并抛出未经授权的
EN

Stack Overflow用户
提问于 2017-05-15 19:12:14
回答 1查看 633关注 0票数 1

我正在为应用程序构建小型REST后端,使用HapiJS和策略,但无法使其正常工作。我已经定义了这样的路线:

/ -用于服务应用程序的前端部分

/login -为用户执行mongodb检查,然后比较密码,最后尝试设置cookie

/restaurants --我试图用auth来保护它:‘会话’,但无法使它工作

我已经定义了策略(几乎是从hapi-auth-cookie github页面复制过来的),但正如我注意到的,但是使用console.log,validateFunc传入的策略选项甚至一次都不会被调用。为什么?是我的主要问题还是代码的其他部分坏了?

一些代码示例:

会话的策略定义:

代码语言:javascript
运行
复制
exports.register = function(server, options, next) {
    const cache = server.cache({ segment: 'sessions', expiresIn: 3 * 24 * 60 * 60 * 1000 });
    server.app.cache = cache;

    server.auth.strategy('session', 'cookie', false, {
        password: 'password-should-be-32-characters',
        cookie: 'lun-cookie',
        redirectTo: false,
        isSecure: false,
        ttl: 20 * 1000,
        validateFunc: function (request, session, callback) {
            cache.get(session.sid, (err, cached) => {

                if (err) {
                    return callback(err, false);
                }

                if (!cached) {
                    return callback(null, false);
                }

                return callback(null, true, cached.account);
            });
        }
    });

    return next();
};

负责设置cookie的登录方法:

代码语言:javascript
运行
复制
login: (request, reply) => {
    const dbQuery = {
        email: request.payload.email
    };

    UserSchema.findOne(dbQuery, (err, user) => {
        if (err) {
            return console.log(err);
        }
        if (!user) {
            return reply(Boom.unauthorized());
        }

        Bcrypt.compare(request.payload.password, user.password, (err, res) => {
            if (err) {
                return console.log(err);
            }
            if (!res) {
                return reply(Boom.unauthorized());
            }

            const sid = String(123);
            request.server.app.cache.set(sid, { account: user }, 0, (err) => {

                if (err) {
                    reply(err);
                }

                request.cookieAuth.set({ sid: sid });
                return reply("ok");
            });
        })
    });
}

由策略保护的路由定义:

代码语言:javascript
运行
复制
{
    method: 'GET',
    path: '/restaurants',
    handler: controller.getRestaurants,
    config: {
        validate: {
            query: {
                list: Joi.string().allow('full').optional(),
                type: Joi.string().valid(restaurantTypeEnum).optional(),
            }
        },
        auth: 'session',
    }
}

有什么想法吗?我已经花了两天时间想办法解决这个问题。

EN

回答 1

Stack Overflow用户

发布于 2017-05-17 09:17:11

首先,确保hapi-auth-cookie在路由之前已注册。

然后,我会检查登录函数是否真的通过并回复一些cookie。

然后我会编辑“/餐馆”到auth: { mode: 'try', strategy: 'session' },的路线

并检查request.headers是否包括cookie。

如果让它正常工作,请确保为它编写一些单元测试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43987136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档