-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathLastUsedAuth.swift
More file actions
170 lines (144 loc) · 4.11 KB
/
LastUsedAuth.swift
File metadata and controls
170 lines (144 loc) · 4.11 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
//
// LastUsedAuth.swift
// Clerk
//
#if os(iOS)
import ClerkKit
import Foundation
@MainActor
enum LastUsedAuth: Equatable {
case email
case username
case phone
case social(OAuthProvider)
init?(environment: Clerk.Environment?) {
guard let lastAuth = Clerk.shared.client?.lastAuthenticationStrategy,
(environment?.totalEnabledFirstFactorMethods ?? 0) > 1
else {
return nil
}
let providers = environment?.authenticatableSocialProviders ?? []
if let provider = providers.first(where: {
Self.shouldShowBadge(for: [.oauth($0)], lastAuth: lastAuth, environment: environment)
}) {
self = .social(provider)
return
}
if Self.shouldShowBadge(for: FactorStrategy.phoneStrategies, lastAuth: lastAuth, environment: environment) {
self = .phone
return
}
if Self.shouldShowBadge(for: FactorStrategy.emailStrategies, lastAuth: lastAuth, environment: environment) {
self = .email
return
}
if Self.shouldShowBadge(for: FactorStrategy.usernameStrategies, lastAuth: lastAuth, environment: environment) {
self = .username
return
}
return nil
}
var socialProvider: OAuthProvider? {
switch self {
case .social(let provider):
provider
case .email, .username, .phone:
nil
}
}
var showsEmailUsernameBadge: Bool {
switch self {
case .email, .username:
true
case .phone, .social:
false
}
}
var showsPhoneBadge: Bool {
switch self {
case .phone:
true
case .email, .username, .social:
false
}
}
static func storeIdentifierType(_ identifier: LastUsedAuth, userDefaults: UserDefaults = .standard) {
guard let rawValue = identifier.identifierStorageValue else { return }
userDefaults.set(rawValue, forKey: identifierStorageKey)
}
static func retrieveStoredIdentifierType(userDefaults: UserDefaults = .standard) -> LastUsedAuth? {
guard let rawValue = userDefaults.string(forKey: identifierStorageKey) else {
return nil
}
return switch rawValue {
case "email":
.email
case "phone":
.phone
case "username":
.username
default:
nil
}
}
static func clearStoredIdentifierType(userDefaults: UserDefaults = .standard) {
userDefaults.removeObject(forKey: identifierStorageKey)
}
}
extension LastUsedAuth {
fileprivate static let identifierStorageKey = "clerk_last_used_identifier_type"
fileprivate static func shouldShowBadge(
for strategies: [FactorStrategy],
lastAuth: FactorStrategy,
environment: Clerk.Environment?
) -> Bool {
if lastAuth == .password, let storedIdentifier = retrieveStoredIdentifierType() {
return storedIdentifier.matches(strategies)
}
if lastAuth == .password, !canShowLastUsedBadge(in: environment) {
return false
}
return strategies.contains(lastAuth)
}
fileprivate func matches(_ strategies: [FactorStrategy]) -> Bool {
switch self {
case .email:
strategies.contains(.emailCode)
case .phone:
strategies.contains(.phoneCode)
case .username:
strategies.contains(.password)
&& Set(strategies).isDisjoint(with: [.emailCode, .phoneCode])
case .social:
false
}
}
fileprivate static func canShowLastUsedBadge(in environment: Clerk.Environment?) -> Bool {
let hasEmail = environment?.userSettings.attributes.contains { key, value in
key == "email_address" && value.enabled && value.usedForFirstFactor
} ?? false
let hasPhone = environment?.userSettings.attributes.contains { key, value in
key == "phone_number" && value.enabled && value.usedForFirstFactor
} ?? false
let hasUsername = environment?.userSettings.attributes.contains { key, value in
key == "username" && value.enabled && value.usedForFirstFactor
} ?? false
if hasPhone, hasEmail || hasUsername {
return false
}
return true
}
fileprivate var identifierStorageValue: String? {
switch self {
case .email:
"email"
case .phone:
"phone"
case .username:
"username"
case .social:
nil
}
}
}
#endif