Skip to content

Instantly share code, notes, and snippets.

@lawgimenez
Last active May 22, 2025 01:29
Show Gist options
  • Select an option

  • Save lawgimenez/29f5b7ed1880646233c2ce76f6c3191e to your computer and use it in GitHub Desktop.

Select an option

Save lawgimenez/29f5b7ed1880646233c2ce76f6c3191e to your computer and use it in GitHub Desktop.
Simple SwiftUI Sign In

AuthObservable

import SwiftUI

@Observable class AuthObservable {

    enum Status {
        case splash
        case success
        case loggedOut
        case failed
    }

    var status: Status = .loggedOut
    var email = ""
    var password = ""
}

SignInView

import SwiftUI
import OSLog

private let logger = Logger(subsystem: app.demo"", category: "Sign In")

struct SignInView: View {
    @State private var authObservable = AuthObservable()
    @State private var isPresentIncomplete = false

    var body: some View { 
        NavigationStack {
            if authObservable.status == .success {
                HomeView()
            } else {
                VStack {
                    TextField("Email Address", text: $authObservable.email)
                        .textInputAutocapitalization(.never)
                        .keyboardType(.emailAddress)
                    TextField("Password", text: $authObservable.password)
                        .submitLabel(.done)
                    Button {
                        Task {
                            await signIn()
                        }
                    } label: {
                        if authObservable.isSigningIn {
                            ProgressView()
                                .background(Color(.clear))
                                .frame(maxWidth: .infinity, maxHeight: 45)
                                .tint(Color(.white))
                        } else {
                            Text("Submit")
                                .frame(maxWidth: .infinity, maxHeight: 45)
                                .padding([.top, .bottom], 5)
                        }
                    }
                }
                .alert("Please enter your email or password", isPresented: $isPresentIncomplete) {
                    Button("OK") {
                        isPresentIncomplete = false
                    }
                }
            }
        }
    }
    
    private func signIn() async {
        if !authObservable.email.isEmpty && !authObservable.password.isEmpty {
            logger.debug("Email is: \(authObservable.email)")
            logger.debug("Password is: \(authObservable.password)")
        } else {
            isPresentIncomplete = true
        }
    }
}

Hi, so this code snippet was written in SwiftUI, this is a code snippet from a couple of production apps currently in the app store but I simplified it with less modifiers.

AuthObservable is the one that manages and observes the changes in our variables: status, email and password, as you can see in the TextField UI. When the user taps on the Button submit, it will call asynchronously the signIn() function. It will display an alert if either email and password is empty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment