如何在SproutCore应用程序中处理身份验证状态 [英] How to deal with authenticated state in a SproutCore application

查看:84
本文介绍了如何在SproutCore应用程序中处理身份验证状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Peter Kniestedt 的一个问题中(现在已删除),他想知道在哪里以及如何设置用于检查用户身份验证的计时器.

In a (now sort of deleted) question by Peter Kniestedt he was wondering where and how to set a timer to check for the authentication of the user.

他的问题是一个较大问题的一部分(这是该问题的标题).因为我认为回答这个问题很重要,所以我创建了一个新问题,作为一种在一个地点收集所有重要信息的方法.

His question is part of a larger question (which is the title of this question). Because I think it is important that this question is answered, I created a new question as a way to gather all the important information in one spot.

推荐答案

这是使用状态图的一种典型情况,更具体地说是并发状态.

This is a typical case for the use of statechart, and more specifically concurrent states.

如果您还不知道,SproutCore包含一个非常有用的库,称为SC.Statechart,这是一种处理应用程序状态的方法,它比通过使用布尔属性更容易控制,因为它也就像响应者和控制器一样.

If you didn't know already, SproutCore contains a very useful library called SC.Statechart, which is a way of dealing with application state in a way which is much more controllable than through the use of boolean properties, as it also acts like a responder as well as a controller.

在这种情况下,您需要一个状态图,该状态图在根中具有两个并发状态:一个用于处理身份验证周围的状态,另一个用于处理应用程序的其余部分.

In this case you'd want a statechart which in the root has two concurrent states: one to deal with the states around authentication, and one to deal with the rest of the application.

MyApp.statechart = SC.Statechart.create({
  rootState: SC.State.design({
    substatesAreConcurrent: true,
    AUTH: SC.State.design({
      initialSubstate: 'CHECKAUTH',
      CHECKAUTH: SC.State.design({
      }),
      LOGIN: SC.State.design({
      }),
      AUTHENTICATING: SC.State.design({
      }),
      AUTHENTICATED: SC.State.design({
      }),      
    }),
    APPMAIN: SC.State.design({
    })
  })
});

这应该如何工作: 当您的应用程序启动时,它将立即进入两种状态,一种是APPMAIN状态,即与应用程序本身相关的状态.另一个是AUTH状态,它将立即进入初始子状态CHECKAUTH,该状态将检查用户是否具有有效的会话.如果不是,则此状态应进入LOGIN状态,该状态负责显示登录屏幕.当用户随后执行登录时,LOGIN状态将转换为AUTHENTICATING状态,该状态将由服务器进行检查.如果此尝试无效或不正确,则AUTHENTICATING状态应转换为LOGIN,否则应转换为AUTHENTICATED.

How is this supposed to work: When your app starts, it will go to two states at once, one being the APPMAIN state which is the state which deals with the application itself. The other is the AUTH state, which will immediately go to the initial substate CHECKAUTH, which checks whether the user has a valid session. If not, this state should go to the LOGIN state, which is responsible for showing a login screen. When the user then performs a login, the LOGIN state transitions to the AUTHENTICATING state, which does the server checking. If this the attempt is invalid or incorrect, the AUTHENTICATING state should transition to LOGIN, otherwise to AUTHENTICATED.

要回答原始问题,此问题基于:如果您需要一个定期检查身份验证是否仍然有效的计时器,则该计时器应处于AUTHENTICATED状态.如果失败,则可以立即转换为LOGIN状态,以显示登录屏幕.

To answer the original question this question is based on: if you need a timer somewhere which regularly checks whether the authentication is still valid, it should be in the AUTHENTICATED state. If it would fail, you can immediately transition to the LOGIN state, to display the login screen.

像这样使用状态图,可以防止用户不得不重新加载或退出应用程序以再次登录,从而可能丢失数据.简而言之,它可以带来更好的用户体验.

Using the state chart like this prevents the user having to reload or quit the application in order to login again, and possibly lose data as a result. In short it makes a much better user experience.

这篇关于如何在SproutCore应用程序中处理身份验证状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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