首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Node js - Hapi JS使用Bell进行身份验证

Node js - Hapi JS使用Bell进行身份验证
EN

Stack Overflow用户
提问于 2016-03-18 01:23:04
回答 1查看 1.7K关注 0票数 1

我不熟悉使用Hapi进行身份验证,并且尝试使用this教程连接到教程中提到的Twitter API,以使您必须使用copy the Consumer Key and Consumer Secret from your Twitter application and then add them to your version of the code in the twitter auth strategy的代码工作。

我的server.js如下所示(将客户端密钥和客户端id更改为在此处发布):

代码语言:javascript
复制
 'use strict';

const Hapi = require('hapi');
const Boom = require('boom');

// Create a server with a host and port
const server = new Hapi.Server();
server.connection({
  port: 3000
});



server.route({
    method: 'GET',
    path: '/',
    handler: function (request, reply) {
        reply('Hello, world!');
    }
});

server.route({
    method: 'GET',
    path: '/{name}',
    handler: function (request, reply) {
        reply('Hello, ' + encodeURIComponent(request.params.name) + '!');
    }
});



// Register bell and hapi-auth-cookie with the server
server.register([require('hapi-auth-cookie'), require('bell')], function(err) {

  //Setup the session strategy
  server.auth.strategy('session', 'cookie', {
    password: 'secret_cookie_encryption_password', //Use something more secure in production
    redirectTo: '/auth/twitter', //If there is no session, redirect here
    isSecure: false //Should be set to true (which is the default) in production
  });

  //Setup the social Twitter login strategy
  server.auth.strategy('twitter', 'bell', {
    provider: 'twitter',
    password: 'secret_cookie_encryption_password', //Use something more secure in production
    clientId: 'lTIBJtiRT4G32wwMUYbU4yCRw',
    clientSecret: '6TBrWfoP4QEIB6lrApIxJun1SfLYb4e2fEs3vtrphFpB2FieGg',
    isSecure: false //Should be set to true (which is the default) in production
  });

  //Setup the routes (this could be done in an own file but for the sake of simplicity isn't)
  server.route({
    method: 'GET',
    path: '/auth/twitter',
    config: {
      auth: 'twitter', //<-- use our twitter strategy and let bell take over
      handler: function(request, reply) {

        if (!request.auth.isAuthenticated) {
          return reply(Boom.unauthorized('Authentication failed: ' + request.auth.error.message));
        }

        //Just store the third party credentials in the session as an example. You could do something
        //more useful here - like loading or setting up an account (social signup).
        request.auth.session.set(request.auth.credentials);

        return reply.redirect('/');
      }
    }
  });



  // Start the server

});





server.start((err) => {

    if (err) {
        throw err;
    }
    console.log('Server running at:', server.info.uri);
});

当我尝试打开此http://localhost:3000/auth/twitter

获取下方错误

代码语言:javascript
复制
{"statusCode":502,"error":"Bad Gateway","message":"connect ETIMEDOUT 10.10.34.36:443"}

我也试着用console.log调试代码,但看起来不是work.what,我该怎么办?谢谢

EN

回答 1

Stack Overflow用户

发布于 2016-03-18 16:13:49

我最初以为您使用的是较新版本的npm包(bellboom等)。如我的教程中所列。

所以我测试了你的代码,没有任何问题--无论是最新版本的包还是旧版本的包。

我想你可能遇到了网络问题,比如代理之类的问题。

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

https://stackoverflow.com/questions/36067603

复制
相关文章

相似问题

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