Auth0从刷新令牌检索新的访问令牌

时间:2018-07-13 14:23:33

标签: ios swift native auth0 auth0-lock

尝试从刷新令牌中获取新的访问令牌时遇到了一些麻烦。我正在使用带有Swift 4的本机应用程序。

LoginController看起来像

func checkAccessToken() {
        SessionManager.shared.refreshToken { error in
            DispatchQueue.main.async {
                guard error == nil else {
                    print("Failed to renew credentials: \(String(describing: error))")
                    return self.showLock()
                }
                SessionManager.shared.retrieveProfile { error in
                    DispatchQueue.main.async {
                        guard error == nil else {
                            print("Failed to retrieve profile: \(String(describing: error))")
                            return self.showLock()
                        }
                        self.performSegue(withIdentifier: "HomeView", sender: nil)
                    }
                }
            }
        }
    }

SessionManager如下:

func refreshToken(_ callback: @escaping (Error?) -> ()) {
        guard let refreshToken = self.keychain.string(forKey: self.keyRefresh) else {
            return callback(SessionManagerError.noRefreshToken)
        }

        Auth0
            .authentication()
            .renew(withRefreshToken: refreshToken)
            .start { result in
                switch (result) {
                case .success(let credentials):
                    self.storeTokens(credentials.accessToken)
                    callback(nil)
                case .failure(let error):
                    self.logout()
                    callback(error)
                }
            }
    }

并检索个人资料:

func retrieveProfile(_ callback: @escaping (Error?) -> ()) {
    guard let accessToken = self.keychain.string(forKey: self.keyAccessToken) else {
        return callback(SessionManagerError.noAccessToken)
    }

    Auth0
        .authentication()
        .userInfo(token: accessToken)
        .start { result in
            switch(result) {
            case .success(let profile):
                self.profile = profile
                callback(nil)
            case .failure(let error):
                callback(error)
            }
    }
}

refreshToken 返回true。因此,从理论上讲,访问令牌是新的。 然后,我尝试使用新的访问令牌再次检索配置文件,并且 RetrieveProfile 的结果是以下错误:

Failed to retrieve profile: Optional({"sub":"****","email":"*****@gmail.com","email_verified":true}) 状态码为0

0 个答案:

没有答案