简体   繁体   English

登录成功后重定向或在环回框架中失败

[英]Redirection after the login succeeds or fails in loopback framework

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 ! 我发现文档很安静!

Use context.res provided in a remote hook. 使用远程钩子中提供的context.res For example: 例如:

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. 基本上,您可以访问请求和响应对象,因此只需像在Express中一样进行响应。 See http://expressjs.com/4x/api.html for more info. 有关详细信息,请参阅http://expressjs.com/4x/api.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM