81 lines
1.5 KiB
Swift
81 lines
1.5 KiB
Swift
//
|
|
// AuthView.swift
|
|
// UserManagement
|
|
//
|
|
// Created by Guilherme Souza on 17/11/23.
|
|
//
|
|
|
|
import Supabase
|
|
import SwiftUI
|
|
|
|
struct AuthView: View {
|
|
@State var email = ""
|
|
@State var isLoading = false
|
|
@State var result: Result<Void, Error>?
|
|
|
|
var body: some View {
|
|
Form {
|
|
Section {
|
|
TextField("Email", text: $email)
|
|
.textContentType(.emailAddress)
|
|
.autocorrectionDisabled()
|
|
#if os(iOS)
|
|
.textInputAutocapitalization(.never)
|
|
#endif
|
|
}
|
|
|
|
Section {
|
|
Button("Sign in") {
|
|
signInButtonTapped()
|
|
}
|
|
|
|
if isLoading {
|
|
ProgressView()
|
|
}
|
|
}
|
|
|
|
if let result {
|
|
Section {
|
|
switch result {
|
|
case .success: Text("Check you inbox.")
|
|
case let .failure(error): Text(error.localizedDescription).foregroundStyle(.red)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.onMac { $0.padding() }
|
|
.onOpenURL(perform: { url in
|
|
Task {
|
|
do {
|
|
try await supabase.auth.session(from: url)
|
|
} catch {
|
|
result = .failure(error)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
func signInButtonTapped() {
|
|
Task {
|
|
isLoading = true
|
|
defer { isLoading = false }
|
|
|
|
do {
|
|
try await supabase.auth.signInWithOTP(
|
|
email: email,
|
|
redirectTo: URL(string: "io.supabase.user-management://login-callback")
|
|
)
|
|
result = .success(())
|
|
} catch {
|
|
result = .failure(error)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#if swift(>=5.9)
|
|
#Preview {
|
|
AuthView()
|
|
}
|
|
#endif
|