登录成功或失败后在环回框架中的重定向 [英] Redirection after the login succeeds or fails in loopback framework

查看:94
本文介绍了登录成功或失败后在环回框架中的重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用环回框架,并通过创建一个继承自基本用户"的客户"模型来实现简单的登录功能:

I have recently started with loopback framework and made a simple login functionality by creating a 'customer' model inheriting from base 'User' like this:

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){

  }

然后我像这样在model-config.json中输入一个条目:

I then made a entry in model-config.json like this:

"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.

我对此进行了很多搜索,发现的所有建议都是使用钩子的建议.但是钩子没有这样的事件.我在哪里编写重定向功能?我的猜测是CUSTOMER.JS

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

我发现文档令人困惑!

推荐答案

使用远程挂钩中提供的context.res.例如:

Use context.res provided in a remote hook. 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');
})

基本上,您可以访问请求和响应对象,因此只需像在Express中一样进行响应即可.有关更多信息,请参见 http://expressjs.com/4x/api.html .

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.

这篇关于登录成功或失败后在环回框架中的重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆