Swift -AWS身份验证提取 - 发挥全球功能

发布于 2025-02-09 14:47:50 字数 1824 浏览 0 评论 0原文

我在应用程序上使用AWS身份验证。而且我想创建一个全局函数,以获取用户令牌,以便每当需要获取新ID令牌时,我都可以在应用程序中的任何地方访问它,代币每5分钟都会更改一次,因此我需要不时调用它。我这样做了

    func fetchSession() {
        Amplify.Auth.fetchAuthSession { result in
            do {
                let session = try result.get()

                // Get cognito user pool token
                if let cognitoTokenProvider = session as? AuthCognitoTokensProvider {
                    let tokens = try cognitoTokenProvider.getCognitoTokens().get()

                    return tokens.idToken
                }
            } catch {
                print("Fetch auth session failed with error - \(error)")
            }
        }
    }

,但是我遇到了这个错误,

Unexpected non-void return value in void function

我也尝试这样做

func fetchSession() {
        let token = Amplify.Auth.fetchAuthSession { result -> AnyObject in
            do {
                let session = try result.get()

                // Get cognito user pool token
                if let cognitoTokenProvider = session as? AuthCognitoTokensProvider {
                    let tokens = try cognitoTokenProvider.getCognitoTokens().get()

                    return tokens.idToken
                }
            } catch {
                print("Fetch auth session failed with error - \(error)")
            }
        }
    }

,这是我得到的错误

Cannot convert value of type '(AmplifyOperation<AuthFetchSessionRequest, AuthSession, AuthError>.OperationResult) -> AnyObject' (aka '(Result<AuthSession, AuthError>) -> AnyObject') to expected argument type '((AmplifyOperation<AuthFetchSessionRequest, AuthSession, AuthError>.OperationResult) -> Void)?' (aka 'Optional<(Result<AuthSession, AuthError>) -> ()>')

I am using AWS authentication on my app. And I want to create a global function for fetching the user token so I can access it anywhere in the app whenever I need to fetch the new id token, the token is changing every 5 mins so I need to call it from time to time. I do it like this

    func fetchSession() {
        Amplify.Auth.fetchAuthSession { result in
            do {
                let session = try result.get()

                // Get cognito user pool token
                if let cognitoTokenProvider = session as? AuthCognitoTokensProvider {
                    let tokens = try cognitoTokenProvider.getCognitoTokens().get()

                    return tokens.idToken
                }
            } catch {
                print("Fetch auth session failed with error - \(error)")
            }
        }
    }

but I am getting this error

Unexpected non-void return value in void function

I also try to make it like this

func fetchSession() {
        let token = Amplify.Auth.fetchAuthSession { result -> AnyObject in
            do {
                let session = try result.get()

                // Get cognito user pool token
                if let cognitoTokenProvider = session as? AuthCognitoTokensProvider {
                    let tokens = try cognitoTokenProvider.getCognitoTokens().get()

                    return tokens.idToken
                }
            } catch {
                print("Fetch auth session failed with error - \(error)")
            }
        }
    }

and this is the error I got

Cannot convert value of type '(AmplifyOperation<AuthFetchSessionRequest, AuthSession, AuthError>.OperationResult) -> AnyObject' (aka '(Result<AuthSession, AuthError>) -> AnyObject') to expected argument type '((AmplifyOperation<AuthFetchSessionRequest, AuthSession, AuthError>.OperationResult) -> Void)?' (aka 'Optional<(Result<AuthSession, AuthError>) -> ()>')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女净妖师 2025-02-16 14:47:50

看起来您需要指定什么fetchsession将返回。现在,您说的是它将返回void,因为您已经忽略了返回类型。

这是指定返回类型的示例。

func fetchSession() -> String? {
  // ...
  do {
    // ...
    return tokens.idToken // I assume idToken is a String
 } catch {
   print("Fetch auth session failed with error - \(error)")
   return nil
 }
}

不要忘记在捕获量中退还零或重新误差错误。

Swift Docs on functions: https://docs.swift.orgg/swift.org/swift.orgbook /languageGuide/functions.html

Looks like you need to specify what fetchSession will return. Right now you are saying it will return Void because you have neglected the return type.

Here is an example of specifying the returned type.

func fetchSession() -> String? {
  // ...
  do {
    // ...
    return tokens.idToken // I assume idToken is a String
 } catch {
   print("Fetch auth session failed with error - \(error)")
   return nil
 }
}

Do not forgot to return nil in the catch or rethrow the error.

Swift docs on functions: https://docs.swift.org/swift-book/LanguageGuide/Functions.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文