0
我已經建立了一些基本的後端API需要多個身份驗證。現在我試圖使用Bell連接到Twitter API,但問題是顯示的頁面不是讓我驗證應用程序,它顯示錯誤{「statusCode」:500,「error」:「內部服務器錯誤」,「消息 「:」 發生內部服務器錯誤「}與貝爾哈比未能通過Twitter驗證
下面是相關文件:
index.ts
'use strict';
import * as hapi from "hapi";
import * as boom from "boom";
import router from './router/router';
const server: hapi.Server = new hapi.Server();
server.connection({ port: 3000, host: 'localhost' });
// .register(...) registers a module within the instance of the API. The callback is then used to tell that the loaded module will be used as an authentication strategy.
server.register([require('hapi-auth-jwt'), require('hapi-auth-cookie'), require('bell')], (err) => {
// normal server auth strategy using JWT
server.auth.strategy('token', 'jwt', {
key: 'secretkey',
verifyOptions: {
algorithms: [ 'HS256' ],
},
redirectTo: '/login'
});
//Setup the session strategy
server.auth.strategy('session', 'cookie', {
password: 'secret_cookie_encryption_password', //Use something more secure in production
redirectTo: '/login', //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: 'secretkey',
clientSecret: ' secretkey',
isSecure: false //Should be set to true (which is the default) in production
});
//server.auth.default('token');
// Make sure server get auth first before attach the router
router.forEach((router) => {
console.log(`attaching ${ router.path }`);
server.route(router);
});
});
server.start((err) => {
if (err) {
throw err;
}
console.log(`Server running at: ${server.info.uri}`);
});
router.ts
'use strict';
import controllers from '../server/controllers/Index';
import models from '../server/models/index';
import { compareHashPassword } from '../Helper';
import * as jwt from "jsonwebtoken";
import * as Boom from "boom";
// Init express router saja
let router;
let User = models.User;
// Setting API URL
router = [
{
method: 'GET',
path: '/',
config: {
auth: {
strategies: ['token', 'session'],
}, //<-- require a session for this, so we have access to the twitter profile
handler: function(request, reply) {
//Return a message using the information from the session
return reply('Hello, ' + request.auth.credentials.displayName + '!');
}
}
},
{
method: 'GET',
path: '/login',
handler: function(request, reply) {
return reply('Please Login to ReviewUr!');
}
},
// Authentication route for Token
{
path: '/auth',
method: 'POST',
handler: controllers.Auths.list
},
// Authentication route for Twitter
{
method: 'GET',
path: '/auth/twitter',
config: {
auth: 'twitter',
handler: function(request, reply) {
if (!request.auth.isAuthenticated) {
//return reply(Boom.unauthorized('Authentication failed: ' + request.auth.error.message));
return reply('unauthorized!');
}
const profile = request.auth.credentials.profile;
request.cookieAuth.set({
twitterId: profile.id,
username: profile.username,
displayName: profile.displayName
});
return reply.redirect('/').catch(error => reply(error));
}
}
},
///////////////////////////////////////
];
export default router
有什麼我失蹤?