Twitch OAuth redirect not working with OAuthSwift library

I’m having trouble with OAuth authentication for Twitch using the OAuthSwift library. When users complete the authorization process on Twitch, they don’t get redirected back to my iOS app.

Instead, Safari shows this warning message: You are about to leave Twitch. Twitch has no control over the content or security of http://localhost.

I tried setting up URL handling in my AppDelegate but it’s not working:

func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    if url.host == "localhost" {
        OAuthSwift.handle(url: url)
    }
    return true
}

Here’s my authentication setup code:

@IBAction func loginButtonTapped(_ sender: Any) {
    let oauth = OAuth2Swift(
        consumerKey: "my_client_id",
        consumerSecret: "my_client_secret",
        authorizeUrl: "https://id.twitch.tv/oauth2/authorize",
        accessTokenUrl: "https://id.twitch.tv/oauth2/token",
        responseType: "code",
        contentType: "application/json"
    )
    
    oauth.authorizeURLHandler = OAuthSwiftOpenURLExternally.sharedInstance
    self.oauth = oauth
    
    oauth.authorize(withCallbackURL: URL(string: "http://localhost")!, scope: "user:read:email", state: "TWITCH") { result in
        switch result {
        case .success(let (credentials, response, params)):
            print(credentials.oauthToken)
            TokenStorage.shared.token = credentials.oauthToken
        case .failure(let error):
            print(error.localizedDescription)
        }
    }
}

Any suggestions on how to fix this redirect issue would be helpful!

This happens because iOS doesn’t recognize localhost as a valid callback for your app. I fixed it by creating a proper URL scheme in Info.plist and updating the callback URL. Add a URL Types entry with your custom scheme like twitchauth://callback. Then change your authorize call to use this custom URL instead of localhost. Also, throw in some debug prints to check if your URL handling method’s actually being called. The AppDelegate method should trigger when the redirect happens, but if you’re using SceneDelegate on newer iOS versions, you might need to implement the URL handling there instead.

The issue you’re facing with localhost as a callback URL stems from Twitch’s inability to handle it for mobile applications. To resolve this, implement a custom URL scheme, like yourappname://callback, in your Info.plist. Ensure this scheme is also registered in your Twitch developer console. I encountered a similar challenge when I integrated Twitch authentication in my app, and switching to a custom scheme resolved it quickly. It’s crucial for both your app and Twitch settings to have matching URL schemes.

yeah, localhost issues on iOS are a pain. you really shud switch to a custom url scheme like myapp://auth in your info.plist, and make sure to update your callback URL. also, remember to add this scheme in the twitch dev console or the error pops up again.