Skip to content

Initial version #1

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

Merged
merged 51 commits into from
May 19, 2024
Merged

Initial version #1

merged 51 commits into from
May 19, 2024

Conversation

Firehed
Copy link
Contributor

@Firehed Firehed commented May 16, 2024

This creates a basic iOS/macOS (/tvOS/visionOS?) SDK for integrating SnapAuth.

So far:

  • Registration seems to work
  • Model authentication seems to work
  • Autofill authentication DOES NOT work (I can't find anything wrong logically, but the UI in the test app glitches out and gets console warnings when it's given the appropriate hint)

It's been tested on-device for macOS and iOS, and in simulators for visionOS (seems ok-ish given the limitations) and tvOS (basically non-functional, but I think this is a simulator problem)

Logging is a hot mess, error handling needs (a lot of) work, and a lot of the internal names are pretty goofy. I'll file issues for all of that.

BUT the core integration experience is pretty decent:

  • Initialize with a publishable key
  • Implement (and set) a delegate
  • Call one of the two (three?) main APIs
  • Get your callback

I'd prefer a more direct async/await approach, but this is probably the more native experience and trying to work through threading the ASAuthorization callbacks back into async/await is probably not a good time.

import SwiftUI
import AuthenticationServices
import SnapAuth

struct ContentView: View {

    let snapAuth = SnapAuth(
        publishableKey: "pubkey_f73c62d953397a45edbf1e6a7a1ce6dc41d69a31")

    @State private var username: String = ""
    @State private var resultText: String = ""
    @State private var running = false

    var body: some View {
        VStack {
            Text("SnapAuth Example")
                .font(.title)

            Spacer()

            TextField("Username", text: $username)
                .textContentType(/*@START_MENU_TOKEN@*/.username/*@END_MENU_TOKEN@*/)
                .padding()

            Button("Sign In", systemImage: "person.badge.key") {
                running = true
                signIn()
            }.padding()

            Button("Register", systemImage: "person.badge.key.fill") {
                running = true
                register()
            }.padding()

            Text("Auth token: \(resultText)")

            if running {
                ProgressView()
            }

        }
//        .onAppear(perform: autoFill)
        .padding()
        .background(.purple)

        
    }

    func autoFill() {
        #if os(iOS)
        Task {
            snapAuth.delegate = self
            await snapAuth.handleAutoFill()
        }
        #endif
    }

    func register() {
        Task {
            snapAuth.delegate = self
            await snapAuth.startRegister(name: username)
        }
    }

    func signIn() {
        Task {
            snapAuth.delegate = self
            await snapAuth.startAuth(.handle(username))

            
        }
    }
}

extension ContentView: SnapAuthDelegate {

    func snapAuth(didFinishAuthentication result: SnapAuthResult) async {
        running = false
        guard case .success(let auth) = result else {
            resultText = "AUTH FAILED"
            return
        }
        resultText = "Auth succeeded: \(auth.token)"
    }

    func snapAuth(didFinishRegistration result: SnapAuthResult) async {
        running = false
        switch result {
        case .success(let registration):
            resultText = "Reg success \(registration.token)"
        case .failure(let error):
            resultText = "REG FAILED"
        }
    }
}

Specifically with loading it like this in SwiftUI and a single view, it's a bit error-prone, since you can easily forget to bind the delegate - and then everything silently fails.

There are also some Apple bugs where the internal ASAuthorization delegate methods aren't called in some failure scenarios, and the whole domain linking process is a pain. They're a bit tricky to repro but I'll file radars for all of them once I get it figured out.

@Firehed Firehed marked this pull request as ready for review May 19, 2024 02:23
@Firehed Firehed merged commit b855de9 into main May 19, 2024
@Firehed Firehed deleted the build branch May 19, 2024 02:28
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

Successfully merging this pull request may close these issues.

1 participant