I have recently started with loopback framework and made a simple login functionality by creating a 'customer' model inheriting from base 'User' like this:
我最近开始使用loopback框架并通过创建一个继承自base'User'的'customer'模型来创建一个简单的登录功能,如下所示:
CUSTOMER.JSON
CUSTOMER.JSON
{
"name": "customer",
"base": "User",
"idInjection": true,
"properties": {
"email":{
"type":"string",
"required":false
},
"username":{
"type":"string",
"required":false
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": []
}
CUSTOMER.JS
CUSTOMER.JS
module.exports = function(customer){
}
I then made a entry in model-config.json like this:
然后我在model-config.json中创建了一个像这样的条目:
"customer": {
"dataSource": "mango-database",
"public": true
}
And yes I was able to login and logout easily. I have a login screen with fields username and password. I submit this form to customers/login and as soon as it gets the login, I get a screen:
是的,我能够轻松登录和注销。我有一个登录界面,其中包含用户名和密码字段。我将此表单提交给客户/登录,一旦登录,我就会看到一个屏幕:
{
id: "lvrTjKBKXCFPTMFej6AyegQUFYe5mSc1BiYbROZwCBM0jqae7kZ7v8ZfGujfDGgy",
ttl: 1209600,
created: "2014-12-07T08:12:17.572Z",
userId: "5483e88b5e9cf2fe0c64dd6c"
}
Now I want that instead of this screen, I should be able to redirect user to some other page (dashboard) and if the login fails, it should go back to the login screen.
现在我想要而不是这个屏幕,我应该能够将用户重定向到其他页面(仪表板),如果登录失败,它应该返回到登录屏幕。
I googled up a lot on this and all i found was the suggestions to use hooks. But hooks doesn't have such event. Where do I write the redirection functionality? My guess is CUSTOMER.JS
我在这上面搜索了很多,我找到的就是使用钩子的建议。但钩子没有这样的事件。我在哪里写重定向功能?我的猜测是CUSTOMER.JS
I found the documentation quiet confusing !
我发现文档很安静!
1 个解决方案
#1
3
Use context.res
provided in a remote hook. For example:
使用远程钩子中提供的context.res。例如:
Customer.afterRemote('create', function(context, customer, next) {
var res = context.res; //this is the same response object you get in Express
res.send('hello world');
})
Basically, you have access to request and response objects, so just respond as you would in Express. See http://expressjs.com/4x/api.html for more info.
基本上,您可以访问请求和响应对象,因此只需像在Express中一样进行响应。有关详细信息,请参阅http://expressjs.com/4x/api.html。
#1
3
Use context.res
provided in a remote hook. For example:
使用远程钩子中提供的context.res。例如:
Customer.afterRemote('create', function(context, customer, next) {
var res = context.res; //this is the same response object you get in Express
res.send('hello world');
})
Basically, you have access to request and response objects, so just respond as you would in Express. See http://expressjs.com/4x/api.html for more info.
基本上,您可以访问请求和响应对象,因此只需像在Express中一样进行响应。有关详细信息,请参阅http://expressjs.com/4x/api.html。