0

I am trying to use the OAuth protocol to authenticate a user using the eBay API. I have created a separate class to handle the authenticatio with a function to start the process. I am calling the function from the login view via a button. When I click the button on the login screen nothing happens, not even an error.

class WebAuthenticate: NSObject, ObservableObject {
       
    
    func authenticate() {
        guard let authURL = URL(string: "https://auth.sandbox.ebay.com/oauth2/authorize")
        else { return  }
        let scheme = "myapp"
        
        //Initializing session
        let session = ASWebAuthenticationSession(url: authURL, callbackURLScheme: scheme) {
            callbackURL, error in
            
            
            
        }
        
        session.start()
        
    }
    
}

extension WebAuthenticate: ASWebAuthenticationPresentationContextProviding {
    func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
        return ASPresentationAnchor()
    }
}

Login View

import SwiftUI



struct LoginView: View {
    
    @State var isAuthenticated = false
   
    @StateObject private var auth = WebAuthenticate()
   
    var body: some View {


        VStack {
            Text("Welcome, please login")
                .padding(100)
            Spacer()


            if isAuthenticated {


                VStack {

                    Text("You’re logged in!")


                    Button("Log out") {
                        logout()
                    }


                } // VStack

            } else {


                VStack {


                    Button("Log in") {
                        auth.authenticate()
                      }

                    } // VStack

                } // if isAuthenticated

            } // Body Vstack
    } // body


Would appreciate any help or reading material to figure this out.

  • You already asked this question before, why did you delete it, together with the useful comments? Did you read the documentations about how to authenticate with ebay (I presume), for example at: https://developer.ebay.com/api-docs/static/authorization_guide_landing.html. they usually have examples of how to do it. Don't you think you have to provide some credentials/token to access the API. I recommend you do a **basic** search for how to do authentication with your target server. – workingdog support Ukraine Aug 21 '23 at 22:44
  • Yes I did, hence me asking here. The comments weren't very helpful so I thought I'd re-ask the question. – Electronic Trip Aug 22 '23 at 01:06
  • how can you say `The comments weren't very helpful ...`, you basically copied my code into your question, and changed the `Button` action as mentioned in the other comment. – workingdog support Ukraine Aug 22 '23 at 01:28
  • Copied your code? That one line with the @State keyword? That's a reach, and yes I changed the button action but here I am still looking for more guidance because clearly that was not enough to go from. – Electronic Trip Aug 22 '23 at 02:14
  • Yes, I can see your deleted question. This is the comment I gave you: Your class should be `class WebAuthenticate: ObservableObject {...}`. You declare it like `@StateObject private var authenticate = WebAuthenticate()` in your view hierarchy, and pass it to another view (LoginView) directly or using, for example `.environmentObject(authenticate)`. Have a look at this link, it gives you some good examples of how to manage data in your app: [monitoring data](https://developer.apple.com/documentation/swiftui/monitoring-model-data-changes-in-your-app) – workingdog support Ukraine Aug 22 '23 at 02:22
  • Okay, I made the class an observable object and I had tried that before the original question (thank you anyway) but overall it didn't get me any closer to getting the login window to pop up. I've seen how to manage the state of the object (publishing and all that), I am just trying to understand what piece I am missing to execute the method from the login view. It's not clicking so I am looking for more explanations. – Electronic Trip Aug 22 '23 at 02:47
  • does this answer your question: https://stackoverflow.com/questions/58574143/is-it-possible-to-use-aswebauthenticationsession-from-swiftui – workingdog support Ukraine Aug 22 '23 at 02:56

0 Answers0