Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling login in a SwiftUI project will cause a crash! #20

Open
Arthas-cn opened this issue Dec 20, 2024 · 1 comment
Open

Calling login in a SwiftUI project will cause a crash! #20

Arthas-cn opened this issue Dec 20, 2024 · 1 comment

Comments

@Arthas-cn
Copy link

Arthas-cn commented Dec 20, 2024

SPM: SnapSDK 2.5.0 (https://github.com/Snapchat/snap-kit-spm)
Xcode 16.2
Swift 6.0

Crash Image:
image

View Info:

struct RootView: View {
    var body: some View {
        ZStack{
            Button("Snapchat sign in") {
                Task {
                    do {
                        let provider =  SnapchatAuthCoordinator()
                        let params = try await provider.performSignIn() //throw SignInWithError
                    } catch {
                        print("Error: \(error)")
                    }
                }
            }
        }.onOpenURL { url in
            print("Received URL: \(url)")
            if SCSDKLoginClient.application(UIApplication.shared, open: url, options: [:]) { // Snapchat
                print("snapschat sign")
            }
        }
    }
}

Code:


import Foundation
import SCSDKLoginKit

enum LoginAPI {
    case auth(params: AuthParams)
}

extension LoginAPI {
    
    
    struct AuthParams: Codable {
        
        var username: String?
        
        var password: String?
        
        var nickname: String?
        
        var headImgFileUrl: String?
        
        var authorizationCode: String?
        
        var email: String?
        
        var authType: String
    }
}

@MainActor
protocol LoginProvider:Sendable {
    func performSignIn() async throws -> LoginAPI.AuthParams
}



final class SnapchatAuthCoordinator: LoginProvider {
    
    
    func performSignIn() async throws -> LoginAPI.AuthParams {
        
        try await performSnapchatLogin()
        
        return try await fetchUserProfile()
    }

    private func performSnapchatLogin() async throws {
        
        try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
            SCSDKLoginClient.login(from: getRootViewController()) { success, error in
                if let error = error {
                    
                    continuation.resume(throwing: SignInWithError.failed("Snapchat login failed: \(error.localizedDescription)"))
                    return
                }
                
                guard success else {
                    
                    continuation.resume(throwing: SignInWithError.failed("Snapchat login failed."))
                    return
                }
                
                continuation.resume(returning: ())
            }
        }
    }

    
    
    private func fetchUserProfile() async throws -> LoginAPI.AuthParams {
        try await withCheckedThrowingContinuation { continuation in
            
            let builder = SCSDKUserDataQueryBuilder()
                .withDisplayName()
                .withBitmojiTwoDAvatarUrl()
                .withIdToken()
                .withExternalId()
            
            let userDataQuery = builder.build()
            
            
            SCSDKLoginClient.fetchUserData(with: userDataQuery) { userData, error in
                if let error = error {
                    continuation.resume(throwing: SignInWithError.failed("Fetch user data failed: \(error.localizedDescription)"))
                    return
                }
                
                guard let userInfo = userData else {
                    continuation.resume(throwing: SignInWithError.failed("Fetch user data is nil."))
                    return
                }
                
                
                var authParams = LoginAPI.AuthParams(authType: "SNAPCHAT")
                authParams.password = userInfo.idToken
                authParams.username = userInfo.externalID
                authParams.nickname = userInfo.displayName
                
                
                if let bitmojiAvatarUrlString = userInfo.bitmojiTwoDAvatarUrl,
                   !bitmojiAvatarUrlString.isEmpty,
                   let avatarUrl = URL(string: bitmojiAvatarUrlString)
                {
                    
                    if let avatarPath = URLComponents(url: avatarUrl, resolvingAgainstBaseURL: false)?.path {
                        authParams.headImgFileUrl = avatarPath
                    }
                }
                
                continuation.resume(returning: authParams)
            } failure: { _, _ in
                continuation.resume(throwing: SignInWithError.failed("Get user profile failed."))
            }
        }
    }
    
}

@Arthas-cn
Copy link
Author

@driefler @mwestergaardsc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant