z-h*_*hao 3 authentication node.js hapijs
reply.redirect('back')在hapi.js中有类似的东西吗?我试图在用户成功登录之前将用户重定向回他们请求的原始页面.
使用hapi-auth-cookie方案时,有一个方便的设置用于此目的.看看appendNext选项.
将此设置为时true,重定向到登录页面将包含查询参数next.next将等于原始请求的请求路径.
然后,您可以在成功登录时使用此选项重定向到所需的页面.这是一个可运行的示例,您可以根据自己的需要进行操作并进行修改:
var Hapi = require('hapi');
var server = new Hapi.Server();
server.connection({ port: 8080 });
server.register(require('hapi-auth-cookie'), function (err) {
server.auth.strategy('session', 'cookie', {
password: 'secret',
cookie: 'sid-example',
redirectTo: '/login',
appendNext: true, // adds a `next` query value
isSecure: false
});
});
server.route([
{
method: 'GET',
path: '/greetings',
config: {
auth: 'session',
handler: function (request, reply) {
reply('Hello there ' + request.auth.credentials.name);
}
}
},
{
method: ['GET', 'POST'],
path: '/login',
config: {
handler: function (request, reply) {
if (request.method === 'post') {
request.auth.session.set({
name: 'John Doe' // for example just let anyone authenticate
});
return reply.redirect(request.query.next); // perform redirect
}
reply('<html><head><title>Login page</title></head><body>' +
'<form method="post"><input type="submit" value="login" /></form>' +
'</body></html>');
},
auth: {
mode: 'try',
strategy: 'session'
},
plugins: {
'hapi-auth-cookie': {
redirectTo: false
}
}
}
}
]);
server.start(function (err) {
if (err) {
throw err;
}
console.log('Server started!');
});
Run Code Online (Sandbox Code Playgroud)
为了测试这个:
/login/login然后重定向到/greetings成功| 归档时间: |
|
| 查看次数: |
3473 次 |
| 最近记录: |