-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMyAccountAuthMethodsView.swift
More file actions
217 lines (197 loc) · 8.03 KB
/
Copy pathMyAccountAuthMethodsView.swift
File metadata and controls
217 lines (197 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import SwiftUI
import Combine
import Auth0
/// Full-screen view for viewing and managing Auth0 MFA authentication methods.
///
/// Displays enrolled factors (Email OTP, SMS OTP, TOTP, Push, Passkeys, Recovery Codes),
/// lets users enroll in new ones, and navigates to per-factor management screens.
///
/// ## Standalone
///
/// Drop it anywhere — the view creates and owns its own `NavigationStack`:
///
/// ```swift
/// MyAccountAuthMethodsView()
/// ```
///
/// ## Embedded in a host `NavigationStack`
///
/// Two setup steps are required to avoid SwiftUI's nested-stack dismissal bug:
///
/// **Step 1** — inject the host path binding on your root `NavigationStack`:
/// ```swift
/// NavigationStack(path: $router.path) { ... }
/// .environment(\.hostNavigationPath, $router.path)
/// ```
///
/// **Step 2** — apply `.embeddedInNavigationStack()` at the push site:
/// ```swift
/// .navigationDestination(for: AppRoute.self) { route in
/// switch route {
/// case .loginSecurity:
/// MyAccountAuthMethodsView()
/// .embeddedInNavigationStack()
/// }
/// }
/// ```
///
/// - Precondition: Call `Auth0UniversalComponentsSDKInitializer.initialize(tokenProvider:)`
/// before this view appears.
public struct MyAccountAuthMethodsView: View {
// MARK: - Environment
@Environment(\.auth0Theme) private var theme
@Environment(\.isEmbeddedInNavigationStack) private var isEmbedded
@Environment(\.hostNavigationPath) private var hostPath
@Environment(\.dismiss) private var dismiss
// MARK: - State
/// Drives push navigation for SDK-internal routes.
///
/// In standalone mode it owns an internal `NavigationPath`.
/// In embedded mode `useExternalPath(_:)` redirects all navigation
/// operations to the host stack's path.
@StateObject private var router = Router<Route>()
/// Loads and exposes the list of authentication methods and enrollment state.
@StateObject private var viewModel: MyAccountAuthMethodsViewModel
/// When `true`, the passkey enrollment banner is hidden.
@State var collapsePasskeyBanner: Bool = false
// MARK: - Init
/// Creates the view. Dependencies are resolved from the environment at render time.
public init() {
_viewModel = StateObject(wrappedValue: MyAccountAuthMethodsViewModel())
}
// MARK: - Body
public var body: some View {
// Capture the host dismiss action before any inner NavigationStack can
// override the dismiss environment for its own children.
let hostDismiss = dismiss
if isEmbedded {
// Embedded: no inner NavigationStack — destinations attach to the
// host stack. The SDK router is redirected to the host path on appear.
sdkRootContent
.themedNavigationTitle("Login & Security", theme: theme)
#if !os(macOS)
.navigationBarTitleDisplayMode(.inline)
.toolbarBackground(theme.colors.background.layerBase, for: .navigationBar)
.toolbarBackground(.visible, for: .navigationBar)
#endif
#if !os(macOS)
.navigationBarBackButtonHidden(true)
#endif
.toolbar {
ToolbarItem(placement: .platformLeading) {
Button {
hostDismiss()
} label: {
Image(systemName: "chevron.left")
.foregroundStyle(theme.colors.text.bold)
}
}
}
.navigationDestination(for: Route.self) { route in
ViewFactory.view(for: route, delegate: viewModel)
.environmentObject(router)
}
.environmentObject(router)
.onAppear {
router.useExternalPath(hostPath)
}
} else {
// Standalone: the SDK owns the full NavigationStack.
NavigationStack(path: $router.path) {
sdkRootContent
.themedNavigationTitle("Login & Security", theme: theme)
#if !os(macOS)
.navigationBarTitleDisplayMode(.inline)
.toolbarBackground(theme.colors.background.layerBase, for: .navigationBar)
.toolbarBackground(.visible, for: .navigationBar)
#endif
.navigationDestination(for: Route.self) { route in
ViewFactory.view(for: route, delegate: viewModel)
.environmentObject(router)
}
}
.environmentObject(router)
}
}
// MARK: - SDK Root Content
@ViewBuilder
private var sdkRootContent: some View {
ZStack {
theme.colors.background.layerBase
.ignoresSafeArea()
if let errorViewModel = viewModel.errorViewModel {
ErrorScreen(viewModel: errorViewModel)
} else {
ScrollView(showsIndicators: false) {
LazyVStack(alignment: .leading, spacing: theme.spacing.xs) {
ForEach(viewModel.viewComponents, id: \.self) { component in
authMethodView(component)
}
}.padding(.all, theme.spacing.md)
}
.disabled(viewModel.showLoader)
}
if viewModel.showLoader {
loadingOverlay
}
}
.onAppear {
Task {
await viewModel.loadMyAccountAuthViewComponentData()
}
}
}
// MARK: - Loading Overlay
private var loadingOverlay: some View {
Color.black.opacity(0.25)
.ignoresSafeArea()
.overlay {
VStack(spacing: theme.spacing.md) {
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
.tint(theme.colors.background.primary)
.scaleEffect(1.4)
Text("Loading…")
.auth0TextStyle(theme.typography.label)
.foregroundStyle(theme.colors.text.bold)
}
.padding(.horizontal, 36)
.padding(.vertical, 28)
.background(theme.colors.background.layerTop)
.clipShape(RoundedRectangle(cornerRadius: theme.radius.large))
.shadow(color: Color.black.opacity(0.18), radius: 24, x: 0, y: 8)
}
}
// MARK: - Component Builder
/// Returns the view for a single `MyAccountAuthViewComponentData` item.
@ViewBuilder
private func authMethodView(_ component: MyAccountAuthViewComponentData) -> some View {
switch component {
case .createPasskey(let model):
if #available(iOS 16.6, macOS 13.5, visionOS 1.0, *) {
if let viewModel = model as? PasskeysEnrollmentViewModel,
collapsePasskeyBanner == false {
EnrollPasskeyView(collapsePasskeyBanner: $collapsePasskeyBanner,
viewModel: viewModel)
.padding(.bottom, theme.spacing.xl)
}
}
case .signinMethods(let viewModel):
MyAccountAuthMethodView(viewModel: viewModel)
.padding(.bottom, theme.spacing.xxl)
case .title(let text):
Text(text)
.foregroundStyle(theme.colors.text.bold)
.auth0TextStyle(theme.typography.titleLarge)
case .subtitle(let text):
Text(text)
.foregroundStyle(theme.colors.text.regular)
.auth0TextStyle(theme.typography.helper)
.padding(.bottom, theme.spacing.md)
case .additionalVerificationMethods(let viewModel):
MyAccountAuthMethodView(viewModel: viewModel)
case .emptyFactors:
EmptyFactorsView()
}
}
}