解析,如何处理无效的会话? [英] Parse, how to handle an invalid session?

查看:572
本文介绍了解析,如何处理无效的会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直接从文档中得知:

PFUser#currentUser()从磁盘获取当前登录的用户,并 返回它的一个实例.

PFUser#currentUser() gets the currently logged in user from disk and returns an instance of it.

一切正常,但是如果登录@ disk的用户不是在服务器上登录的用户,该怎么办.可以说,由于任何原因删除了用户帐户,或者由于数据库修改,该会话不再有效.这些是我目前面临的问题.

This is fine and all, but what if the user logged in @ disk isn't a user that's logged in on the server. Lets say the users account has been deleted for whatever reason, or the session is no longer valid due to database modifications. These are currently the problems that I'm facing.

在我阅读的所有教程中,我看到使用以下代码行来检查用户是否有效,因此您可以跳过应用程序的登录阶段:

Throughout the tutorials that I've read, I've seen using the following line of code as a way to check if the user is valid, and thus you can skip the login stage of the application:

if let user = PFUser.currentUser() as? Subclass {
    // Simulate successful login
}

但是,这对我来说是一个问题,因为模拟了成功登录,但是登录不成功.这是我正在处理的错误:

However, this is posing a problem for me, as the successful login is simulated, but the login was not successful. Here is the error I'm dealing with:

[错误]:无效的会话令牌(代码:209,版本:1.12.0)

[Error]: invalid session token (Code: 209, Version: 1.12.0)

所以我做的第一件事是尝试注销用户,但是这失败了(我认为是因为用户没有首先登录),现在我被扔进了应用程序中,该应用程序由于需要数据而立即崩溃服务器不存在.这是我尝试处理错误代码209的方式:

So the first thing I did was attempt to log the user out, however this fails (I assume because the user wasn't logged in to begin with) and now I'm thrown into application which immediately crashes because the data required by the server isn't there. Here's how I attempted to handle error code 209:

let query = PFQuery(className: "Foo")
query.whereKey("Bar", equalTo: "Foo")
query.findObjectsInBackgroundWithBlock {
    (foo, error) -> Void in
    if let error = error {
        print(error);
        if error.code == 209 {
            PFUser.logOutInBackgroundWithBlock {
                (error) -> Void in
                if let error = error {
                    print("Okay, I'm trapped!");
                }
            }
        }
    }
}

此查询"的输出如下:

[Error]: invalid session token (Code: 209, Version: 1.12.0)
Okay, I'm trapped!

我在这里没有主意,我想弄清楚如何在应用程序启动时正确验证用户.在每个查询中必须捕获错误代码209似乎是多余的,但是如果这是您必须执行的操作,那么这就是您必须执行的操作.

I'm out of ideas over here, and I'm ripping my hair out trying to figure out how to properly validate a user upon application launch. It seems redundant to have to catch error code 209 on every query, but if that's what you have to do, then that's what you have to do.

推荐答案

您可以使用同步注销.因此,即使发生错误,也没关系.如果用户再次启动该应用程序,则PFUser会话令牌将失效.

You can use synchronous logout. So even if error occurs, it doesn't matter. The PFUser session token will be invalidated if the user launches the app again.

最新的Parse SDK还提供PFConstants类,该类为所有可能的错误情况提供枚举.

Also latest Parse SDK provides PFConstants class, which provides enum for all possible error cases.

kPFErrorInvalidSessionToken = 209.

if (error.code == kPFErrorInvalidSessionToken) {
    PFUser.logOut()
    //Necessary pop of view controllers after executing the previous code.
}

这篇关于解析,如何处理无效的会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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