【问题标题】:How to renew the id token using auth0 in SwiftUI app如何在 SwiftUI 应用程序中使用 auth0 更新 id 令牌
【发布时间】:2023-02-09 07:03:40
【问题描述】:

我在我的 iOS 应用程序中使用 Auth0 进行登录和注销。用户登录后,我得到一个 id 令牌,我用它在我的应用程序中进行进一步的 api 调用。我们需要继续使用 auth0 更新令牌,如他们的doc 中所述 我的功能如下

struct UpdateToken {
let credentialsManager: CredentialsManager

init() {
    self.credentialsManager = CredentialsManager(authentication: Auth0.authentication())
}


func updateToken() {
    
    guard credentialsManager.canRenew() else {
        // Present login screen
        print("not renewing")
        return
    }
    Auth0
        .webAuth()
        .scope("openid profile offline_access")
    
        .audience("\(audience)/userinfo")
        .start {
            switch $0 {
            case .failure(let error):
                print("token update failed")
                break
                // Handle error
            case .success(let credentials):
                // Pass the credentials over to the Credentials Manager
                credentialsManager.store(credentials: credentials)
                UserDefaults.standard.set(credentials.idToken, forKey: "id_token")
                print("token updated")
                
            }
    }
}

}

它在我的控制台中打印not renewing。我不确定我在这里错过了什么。

登录功能完美无缺

func login() {
    Auth0
        .webAuth()
        .start { result in
            // Handle the result of the authentication
            switch result {
            case .failure(let error):
                // If the authentication fails, print the error message
                print("Failed with: \(error)")
                
            case .success(let credentials):
                // If the authentication is successful, store the credentials and user information in UserDefaults
                self.userProfile = Profile.from(credentials.idToken)
                self.userIsAuthenticated = "1"
                print("Credentials: \(credentials)")
                
                // Store the ID token
                print("ID token: \(credentials.idToken)")
                UserDefaults.standard.set(credentials.idToken, forKey: "id_token")
                
                // Print and store the token type and access token
                print("token type: \(credentials.tokenType)")
                print("access token \(credentials.accessToken)")
                
                // Extract and store the user ID, name, and email from the user profile
                print("userID is \(userProfile.id)")
                let fullString = userProfile.id
                let parts = fullString.split(separator: "|")
                let desiredPart = String(parts[1])
                print(desiredPart)

                UserDefaults.standard.set(desiredPart, forKey: "userId")
                UserDefaults.standard.set(userProfile.name, forKey: "userName")
                UserDefaults.standard.set(userProfile.email, forKey: "userEmail")
                
            }
        }
}

【问题讨论】:

    标签: swift auth0 auth0-connection


    【解决方案1】:

    听起来 canRenew() 无法找到任何存储的凭据 - 尝试在初始登录时使用 credentialsManager.store,类似于您在 updateToken() 中的方式。这样,当用户开始登录时,凭据为stored in the keychain

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-22
      • 2020-05-29
      • 2017-03-10
      • 2016-11-20
      • 2018-03-15
      • 1970-01-01
      • 2021-11-09
      • 2019-02-25
      相关资源
      最近更新 更多