GetStream / stream-chat-swift

💬 iOS Chat SDK in Swift - Build your own app chat experience for iOS using the official Stream Chat API
https://getstream.io/chat/sdk/ios/
Other
858 stars 211 forks source link

ServerErrorPayload: Token signature is invalid 401 #2707

Closed glennposadas closed 1 year ago

glennposadas commented 1 year ago

What did you do?

We have multiple environments, therefore multiple Stream projects. Two of which are sometimes giving the said error. Uninstalling and reinstalling aren't helping.

What did you expect to happen?

Switching between environments should not cause any issues.

What happened instead?

This error shows up in the logs:

API Error: Optional("Error WebSocket in /Users/meme/Documents/GIT/SC/FN-ios/Pods/StreamChat/Sources/StreamChat/WebSocketClient/WebSocketClient.swift:212 -> ServerErrorPayload(code: 5, message: \"Connect failed with error: \"Token signature is invalid\"\", statusCode: 401)).") ❌

Do tokens have some kind of default expiration time? We are not adding an expiration time explicitly.

GetStream Environment

GetStream Chat version: 4.32 GetStream Chat frameworks: StreamChat, StreamChatUI iOS version: 15.2+ Swift version:5.7.2 Xcode version:14.2 Device:Real iPhones

Additional context

I am saving the token from the server to my defaults, and reuse it. I am not sure if this causes the issue, but as mentioned, this should be resolved by simply reinstalling but no.

testableapple commented 1 year ago

Hi @glennposadas. Thanks for opening the issue.

The error message you provided states that the token signature is invalid, which is not related to the token expiration time.

If your environments have different app keys/secrets on Stream Dashboard (and I assume this is true), then you need to recreate the client every time you change the environment so the client picks up the correct token.

Hope this helps.

Best wishes, Alexey

glennposadas commented 1 year ago

Hi, @testableapple . Thanks for replying. But can you help me with how to do what you suggested. Could you please help me and point it to me? Thanks!

testableapple commented 1 year ago

Hi @glennposadas, just follow the docs here

/// for sake of simplicity we are extending ChatClient and add a static var `shared`
extension ChatClient {
    static var shared: ChatClient!
}

/// you can grab your API Key from https://getstream.io/dashboard/
let config = ChatClientConfig(apiKey: .init("<# Your API Key Here #>"))

/// create an instance of ChatClient and share it using the singleton
ChatClient.shared = ChatClient(config: config) { completion in
            /// This closure will be called when the token is expired
            /// You don't need to provide a closure if you're not using expired tokens
            /// You need to fetch a valid token for your Stream user here
            YourNetworkLayer.fetchUserToken { token in
                /// and call the completion closure with the token
                completion(.success(token))
            }
        }

You'll need to use different configs for each environment and update ChatClient.shared as soon as you switch the environment.

Best, Alexey

glennposadas commented 1 year ago

Thank you, @testableapple! Have you had a chance before wherein you read a code and you get surprised like: 'oh I wrote this?'? Anyway, yeah, it should be fixed now. Thanks again!