Firebase Twitter Authentication swiftUI, while user cancelling authentication current view is closed

天大地大妈咪最大 提交于 2020-08-10 18:55:36

问题


In my application I'm having google, facebook, twitter login. all these three logins are working well.

But for twitter login I am facing an issue that, Once I click the twitter login custom button, OAuthProvider opens the twitter authentication page. I cancelling the authentication then it came back to the app and within a second NavigationView pops back automatically. LoginView closed and the ContentView appears(Launch view).

I followed these simple steps for twitter login given by firebase

  var provider = OAuthProvider(providerID: "twitter.com")

func twitterLogin(){
provider.getCredentialWith(nil) { credential, error in
      if error != nil {
        // Handle error.
        print(error ?? "error")

      }
      if credential != nil {
        Auth.auth().signIn(with: credential!) { authResult, error in
          if error != nil {
            // Handle error.
          }
          else{
            self.twitterLoginSuccess = true
            print("Twitter login success")
          }
          // User is signed in.
          // IdP data available in authResult.additionalUserInfo.profile.
          // Twitter OAuth access token can also be retrieved by:
          // authResult.credential.accessToken
          // Twitter OAuth ID token can be retrieved by calling:
          // authResult.credential.idToken
          // Twitter OAuth secret can be retrieved by calling:
          // authResult.credential.secret
        }
      }
    }

}

Button action from login view

  Button(action: self.viewModel.twitterLogin){
                        SignInButton(imageName: "twitter", text: "Sign in with twitter")
                    }.frame(height: 50).buttonStyle(ButtonStyleSignIn())

Launch view code

struct ContentView: View {

@State var show = false
@EnvironmentObject var viewModel : LoginViewModel // (/1)

var body: some View {
    NavigationView{
        ZStack
        {
            Color("colorPrimaryDark")
                .edgesIgnoringSafeArea(.all)
        VStack{
            NavigationLink(destination: LoginView().environmentObject(viewModel), isActive: $show, label: {
                Image("main_logo").renderingMode(.original).frame(width: 100, height: 100)
            })
        }  .navigationBarHidden(true)
            .navigationBarTitle(Text("Home"))
            .edgesIgnoringSafeArea([.top, .bottom])
        }
    }//.preferredColorScheme(.dark) // white tint on status bar
    .onAppear {
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
            self.show.toggle()
        }
    }
}

}

Here I have added the LoginView Code

import SwiftUI
import Firebase
import FirebaseAuth
import FBSDKCoreKit
import FBSDKLoginKit
struct LoginView: View {
    @EnvironmentObject var viewModel : LoginViewModel // (/1)
    @State var show = true
    
    var body: some View {
        ZStack
        {
            Color("colorPrimaryDark")
                .edgesIgnoringSafeArea(.all)
            if self.viewModel.loginSuccess {
                NavigationLink(destination: fromLoginNavigationTo(), isActive: self.$show, label: {
                    EmptyView()
                })
            }
            VStack {
                HStack {
                    Spacer()
                    
                    Button(action: {
                        self.viewModel.signInAnonymously()
                    }){
                        
                        TextViewBody(text: "Skip").padding()
                    }
                    
                }
                    VStack {
                        Button(action: self.viewModel.twitterLogin){
                            SignInButton(imageName: "twitter", text: "Sign in with twitter")
                        }.frame(height: 50).buttonStyle(ButtonStyleSignIn())

                }.padding(.init(top: 0,  leading: 32,bottom: 0,  trailing: 32))
                .navigationBarHidden(true)
                .navigationBarTitle(Text("Home"))
                .edgesIgnoringSafeArea([.top, .bottom])
            }
        }//.preferredColorScheme(.dark) // white tint on status bar
        
    }
}

Xcode : 12 beta

Device : Simulator iOS 14

来源:https://stackoverflow.com/questions/62816501/firebase-twitter-authentication-swiftui-while-user-cancelling-authentication-cu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!