feat(swift-ios): use Clerk authentication components

This commit is contained in:
Theo Browne committed 2026-08-09 20:55:03 -07:00
1 parent 55fe074aec
commit 45fbdde909
5 files changed
+152 -119

No files matched your search

+2 -26
View File
@@ -1,27 +1,6 @@
import ClerkKit
import Foundation
public enum T3ConnectSignInProvider: String, CaseIterable, Identifiable, Sendable {
case google
case github
public var id: String { rawValue }
public var title: String {
switch self {
case .google: "Continue with Google"
case .github: "Continue with GitHub"
}
}
fileprivate var clerkProvider: OAuthProvider {
switch self {
case .google: .google
case .github: .github
}
}
}
public struct T3ConnectAccount: Equatable, Sendable {
public let id: String
public let email: String?
@@ -64,6 +43,8 @@ public final class T3ConnectClerkSession {
)
}
var client: Clerk { clerk }
public var account: T3ConnectAccount? {
guard let user = clerk.user else { return nil }
return T3ConnectAccount(
@@ -79,11 +60,6 @@ public final class T3ConnectClerkSession {
_ = try await clerk.refreshClient()
}
public func signIn(with provider: T3ConnectSignInProvider) async throws {
_ = try await clerk.auth.signInWithOAuth(provider: provider.clerkProvider)
_ = try await clerk.refreshClient()
}
public func signOut() async throws {
try await clerk.auth.signOut()
}
@@ -1,3 +1,4 @@
import ClerkKit
import Foundation
import Observation
@@ -85,6 +86,8 @@ public final class T3ConnectController {
return reason
}
var clerk: Clerk? { auth?.client }
public func refresh() async {
guard let auth, let relay else { return }
refreshGeneration &+= 1
@@ -141,26 +144,6 @@ public final class T3ConnectController {
}
}
public func signIn(with provider: T3ConnectSignInProvider) async {
guard let auth else { return }
refreshGeneration &+= 1
let generation = refreshGeneration
isRefreshing = true
defer {
if refreshGeneration == generation { isRefreshing = false }
}
do {
try await auth.signIn(with: provider)
guard refreshGeneration == generation else { return }
account = auth.account
await refresh()
} catch {
if refreshGeneration == generation {
errorMessage = error.localizedDescription
}
}
}
public func signOut() async {
guard let auth, let relay else { return }
refreshGeneration &+= 1
@@ -4,6 +4,14 @@
<dict>
<key>aps-environment</key>
<string>$(APS_ENVIRONMENT)</string>
<key>com.apple.developer.applesignin</key>
<array>
<string>Default</string>
</array>
<key>com.apple.developer.associated-domains</key>
<array>
<string>webcredentials:clerk.t3.codes</string>
</array>
<key>com.apple.security.application-groups</key>
<array>
<string>$(T3CODE_APP_GROUP_IDENTIFIER)</string>
@@ -1,7 +1,12 @@
import ClerkKit
import ClerkKitUI
import SwiftUI
public struct T3ConnectView: View {
@SwiftUI.Environment(\.dismiss) private var dismiss
@Bindable private var controller: T3ConnectController
@State private var isAuthPresented = false
@State private var didFinishInitialRefresh = false
private let connectEnvironment:
@MainActor (T3ConnectManagedEnvironmentCredential) async throws -> Void
private let onConnected: @MainActor () async -> Void
@@ -16,49 +21,114 @@ public struct T3ConnectView: View {
}
public var body: some View {
List {
if let reason = controller.unavailableReason {
content
.navigationTitle("T3 Connect")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
if controller.account != nil {
ToolbarItem(placement: .topBarTrailing) {
Button("Sign out", role: .destructive) {
Task { await controller.signOut() }
}
.disabled(controller.isRefreshing)
}
}
}
.refreshable {
await controller.refresh()
}
.task {
await controller.refresh()
didFinishInitialRefresh = true
presentAuthenticationIfNeeded()
}
.onChange(of: controller.account?.id) { _, accountID in
guard didFinishInitialRefresh,
accountID == nil,
controller.unavailableReason == nil else { return }
isAuthPresented = true
}
.fullScreenCover(
isPresented: $isAuthPresented,
onDismiss: handleAuthenticationDismissal
) {
authenticationView
}
.alert(
"T3 Connect",
isPresented: Binding(
get: { controller.errorMessage != nil },
set: { if !$0 { controller.errorMessage = nil } }
)
) {
Button("OK") { controller.errorMessage = nil }
} message: {
Text(controller.errorMessage ?? "Something went wrong.")
}
.preferredColorScheme(.dark)
}
@ViewBuilder
private var content: some View {
if let reason = controller.unavailableReason {
connectList {
unavailableSection(reason)
} else if let account = controller.account {
}
} else if let account = controller.account {
connectList {
accountSection(account)
environmentSection
} else {
signInSection
}
} else {
ZStack {
Color.black.ignoresSafeArea()
ProgressView()
.tint(.white)
}
}
}
private func connectList<Content: View>(
@ViewBuilder content: () -> Content
) -> some View {
List {
content()
}
.listStyle(.plain)
.scrollContentBackground(.hidden)
.background(Color.black)
.navigationTitle("T3 Connect")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
if controller.account != nil {
ToolbarItem(placement: .topBarTrailing) {
Button("Sign out", role: .destructive) {
Task { await controller.signOut() }
}
.disabled(controller.isRefreshing)
}
}
@ViewBuilder
private var authenticationView: some View {
if let clerk = controller.clerk {
AuthView(mode: .signInOrUp)
.prefetchClerkImages()
.environment(\.clerkTheme, T3ConnectClerkAppearance.theme)
.environment(clerk)
.preferredColorScheme(.dark)
} else {
ZStack {
Color.black.ignoresSafeArea()
ProgressView()
.tint(.white)
}
}
.refreshable {
}
private func presentAuthenticationIfNeeded() {
guard controller.unavailableReason == nil,
controller.account == nil else { return }
isAuthPresented = true
}
private func handleAuthenticationDismissal() {
Task {
await controller.refresh()
if controller.account == nil {
dismiss()
}
}
.task {
await controller.refresh()
}
.alert(
"T3 Connect",
isPresented: Binding(
get: { controller.errorMessage != nil },
set: { if !$0 { controller.errorMessage = nil } }
)
) {
Button("OK") { controller.errorMessage = nil }
} message: {
Text(controller.errorMessage ?? "Something went wrong.")
}
.preferredColorScheme(.dark)
}
private func unavailableSection(_ reason: String) -> some View {
@@ -78,48 +148,6 @@ public struct T3ConnectView: View {
}
}
private var signInSection: some View {
Section {
VStack(alignment: .leading, spacing: 12) {
Text("Your environments, on every device.")
.font(T3Typography.homeTitle)
Text("T3 Connect is optional. Sign in to reach environments linked to your T3 account.")
.font(T3Typography.threadBody)
.foregroundStyle(T3Colors.textSecondary)
}
.padding(.vertical, 8)
.listRowBackground(Color.black)
ForEach(T3ConnectSignInProvider.allCases) { provider in
Button {
Task { await controller.signIn(with: provider) }
} label: {
HStack {
Text(provider.title)
.font(T3Typography.homeTitle)
Spacer()
Image(systemName: "arrow.up.right")
.font(.body.weight(.semibold))
}
.foregroundStyle(.white)
.padding(.vertical, 6)
}
.disabled(controller.isRefreshing)
.listRowBackground(Color.black)
}
if controller.isRefreshing {
HStack(spacing: 10) {
ProgressView()
Text("Opening secure sign in…")
.font(T3Typography.supporting)
.foregroundStyle(T3Colors.textSecondary)
}
.listRowBackground(Color.black)
}
}
}
private func accountSection(_ account: T3ConnectAccount) -> some View {
Section("Account") {
HStack(spacing: 12) {
@@ -241,3 +269,27 @@ public struct T3ConnectView: View {
}
}
}
@MainActor
private enum T3ConnectClerkAppearance {
static let theme = ClerkTheme(
colors: .init(
primary: .white,
background: T3Colors.background,
input: T3Colors.input,
danger: T3Colors.danger,
success: T3Colors.success,
warning: T3Colors.warning,
foreground: T3Colors.textPrimary,
mutedForeground: T3Colors.textSecondary,
primaryForeground: .black,
inputForeground: T3Colors.textPrimary,
neutral: .white,
ring: .white,
muted: T3Colors.surfaceRaised,
shadow: .black,
border: .white
),
design: .init(borderRadius: 12)
)
}
@@ -8,6 +8,7 @@
/* Begin PBXBuildFile section */
B10000000000000000000001 /* ClerkKit in Frameworks */ = {isa = PBXBuildFile; productRef = B30000000000000000000001 /* ClerkKit */; };
B10000000000000000000002 /* ClerkKitUI in Frameworks */ = {isa = PBXBuildFile; productRef = B30000000000000000000002 /* ClerkKitUI */; };
C10000000000000000000001 /* AgentActivityAttributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = C40000000000000000000001 /* AgentActivityAttributes.swift */; };
C10000000000000000000002 /* SharedContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = C40000000000000000000002 /* SharedContainer.swift */; };
C10000000000000000000003 /* TaskWidgetSnapshot.swift in Sources */ = {isa = PBXBuildFile; fileRef = C40000000000000000000003 /* TaskWidgetSnapshot.swift */; };
@@ -94,6 +95,7 @@
buildActionMask = 2147483647;
files = (
B10000000000000000000001 /* ClerkKit in Frameworks */,
B10000000000000000000002 /* ClerkKitUI in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -252,6 +254,7 @@
name = T3Code;
packageProductDependencies = (
B30000000000000000000001 /* ClerkKit */,
B30000000000000000000002 /* ClerkKitUI */,
);
productName = T3Code;
productReference = A40000000000000000000001 /* T3 Code.app */;
@@ -337,6 +340,12 @@
com.apple.Push = {
enabled = 1;
};
com.apple.SafariKeychain = {
enabled = 1;
};
com.apple.SignInWithApple = {
enabled = 1;
};
};
};
A50000000000000000000002 = {
@@ -915,6 +924,11 @@
package = B20000000000000000000001 /* XCRemoteSwiftPackageReference "clerk-ios" */;
productName = ClerkKit;
};
B30000000000000000000002 /* ClerkKitUI */ = {
isa = XCSwiftPackageProductDependency;
package = B20000000000000000000001 /* XCRemoteSwiftPackageReference "clerk-ios" */;
productName = ClerkKitUI;
};
/* End XCSwiftPackageProductDependency section */
/* Begin PBXFileReference section */