强制执行Ember.JS应用程序的用户/验证状态的最佳方法 [英] Best way to enforce user/authentication state in Ember.JS app

查看:119
本文介绍了强制执行Ember.JS应用程序的用户/验证状态的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一次EmberJS应用工作。整个应用程序要求用户先登录。我试图总结我的周围的最好办法头强制用户在现在登录(当页面最初加载)和将来(当用户注销而且没有刷新)。

Working on my first EmberJS app. The entire app requires that a user be logged in. I'm trying to wrap my head around the best way to enforce that a user is logged in now (when the page is initially loaded) and in the future (when user is logged out and there is no refresh).

我有用户身份验证挂钩处理 - 现在我有一个连接,处理授权用户和创建用户会话(使用的sessionStorage)余烬的数据模型和相关的存储。

I have the user authentication hooks handled - right now I have an ember-data model and associated store that connects that handles authorizing a user and creating a user "session" (using sessionStorage).

我不知道该怎么做的是加强跨路由过渡的时候,包括在根路径初始转换的用户进行身份验证。我在哪里可以把这个逻辑是什么?如果我有一个StateManager的认证,我怎么挂钩,在该路线?我应该有一个权威性的路线是根本途径之外吗?

What I don't know how to do is enforce that a user is authenticated when transitioning across routes, including the initial transition in the root route. Where do I put this logic? If I have an authentication statemanager, how do I hook that in to the routes? Should I have an auth route that is outside of the root routes?

请注意:让我知道,如果这个问题不好是措辞还是我需要解释什么都好,我会很乐意这样做。

Note: let me know if this question is poorly worded or I need to explain anything better, I will be glad to do so.

编辑:
我落得这样做,我认为多一点余烬式的东西,虽然可能一个混乱的实现。我有一个auth StateManager的存储当前用户的认证密钥,以及当前状态。

I ended up doing something that I consider a little more ember-esque, albeit possibly a messy implementation. I have an auth statemanager that stores the current user's authentication key, as well as the current state.

每当有什么需要验证,它只是要求它authmanager并传递一个回调函数与认证密钥运行。如果用户没有登录,就拉了一个登录表单,持观望态度的回调函数,直到用户登录。

Whenever something needs authentication, it simply asks the authmanager for it and passes a callback function to run with the authentication key. If the user isn't logged in, it pulls up a login form, holding off the callback function until the user logs in.

下面是我使用的code的某些选择部分。需要清理,而我离开了一些东西。 http://gist.github.com/3741751

Here's some select portions of the code I'm using. Needs cleaning up, and I left out some stuff. http://gist.github.com/3741751

推荐答案

如果您需要的初始状态转换之前执行检查,上有一个特殊功能 Ember.Application 类名为 deferReadiness()。从源头code注释:

If you need to perform a check before initial state transition, there is a special function on the Ember.Application class called deferReadiness(). The comment from the source code:

在默认情况下,路由器将开始尝试将当前的URL转换成
    应用程序的状态,一旦浏览器发出的 DOMContentReady 事件。如果你
    需要推迟路由,可以调用应用程序的 deferReadiness()方法。
    一旦路由就可以开始,请拨打 advanceReadiness()方法。

By default, the router will begin trying to translate the current URL into application state once the browser emits the DOMContentReady event. If you need to defer routing, you can call the application's deferReadiness() method. Once routing can begin, call the advanceReadiness() method.

请注意,在写这个函数的时候只有在烬,最新的可用

Note that at the time of writing this function is available only in ember-latest

这篇关于强制执行Ember.JS应用程序的用户/验证状态的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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