大多数DRY方式将未经身份验证的用户重定向到登录屏幕 [英] most DRY way to redirect unauthenticated user to login screen in ember

查看:99
本文介绍了大多数DRY方式将未经身份验证的用户重定向到登录屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Ember开始使用引导模式类创建一个模态登录屏幕。



我知道如何显示登录屏幕希望将其与路由集成,以便在每个转换时检查用户的当前登录状态,如果用户未登录,请将其重定向到入口页面并弹出登录屏幕。



我正在寻找最多的不要重复自己的方式来交叉切割这个要求,任何一个ember专家可以帮助我吗?

解决方案

这样做的一个方法是创建一个特殊路由(或者通过重新打开来覆盖 Ember.Route )和未保护路由的公共路由。然后扩展这些路由。

  / * Ember.Route.reopen({* // *全局保护所有路由* / 
App.ProtectedRoute = Ember.Route.extend({
beforeModel:function(){

//如果没有promise used和isAuthenticated返回一个布尔值
// if (!isAuthenticated()){
// this.transitionTo(login);
//}

//如果promise已使用,isAuthenticated返回承诺
var self = this;
/ *使用错误部分,如果从服务器返回错误代码,否则可以使用成功部分检查数据值并确定结果* /
return isAuthenticated ).then(null,function(data){
self.transitionTo(login);
return Ember.RSVP.resolve(data);
});
}
});

/ *用于不需要身份验证的视图* /
App.PublicRoute = Ember.Route.extend({
/ *跳过默认路由的安全检查* /
beforeModel:function(){
}
});

受保护的路线将是

  App.FirstRoute = App.ProtectedRoute.extend(); 

公共路线将是

  App.SecondRoute = App.PublicRoute.extend(); 

isAuthenticated 与承诺可能是一样的

 函数isAuthenticated(){
/ *调用服务器检查是否通过身份验证,如果未通过身份验证服务器返回错误代码ie 500 * /
return $ .ajax({});
}


I'm currently starting out with Ember and have managed to create a modal login screen using bootstrap's modal class.

I know how to display the login screen and I want to integrate it with routing so that with each transition the current loggedIn state of the user is checked and if the user is not logged in, redirect them to the entry page and pop up the login screen.

I'm looking for the most 'Dont-Repeat-Yourself' way of cross cutting this requirement, can any ember experts please assist me?

解决方案

One approach of doing this is by creating a special route (or override the Ember.Route through reopen) for protected routes and a public route for non protected routes. Then extend these routes.

/*Ember.Route.reopen({*//*to globally protect all routes*/
App.ProtectedRoute = Ember.Route.extend({
    beforeModel: function() {

//if no promise used and isAuthenticated returns a boolean
//        if (!isAuthenticated()) {
//            this.transitionTo("login");
//        }

//if promise used and isAuthenticated returns a promise
        var self = this;
/*using the error part of then if error code is returned from server, otherwise the succes part could be used check the data value and decide the outcome*/
        return isAuthenticated().then(null, function(data) {
            self.transitionTo("login");
            return Ember.RSVP.resolve(data);
        });
    }
});

/*to be used for views that do not require authentication*/
App.PublicRoute = Ember.Route.extend({
    /*skip default Route's security check*/
    beforeModel: function() {
    }
});

A protected route would be,

App.FirstRoute = App.ProtectedRoute.extend();

A public route would be,

App.SecondRoute = App.PublicRoute.extend();

The isAuthenticated with promise could be something like

function isAuthenticated(){
/*calling server to check if authenticated, if not authenticated server returns error code i.e. 500*/
return $.ajax({});
}

这篇关于大多数DRY方式将未经身份验证的用户重定向到登录屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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