-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathSkipFirebaseAuth.swift
More file actions
230 lines (178 loc) · 7.72 KB
/
SkipFirebaseAuth.swift
File metadata and controls
230 lines (178 loc) · 7.72 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
218
219
220
221
222
223
224
225
226
227
228
229
230
// This is free software: you can redistribute and/or modify it
// under the terms of the GNU Lesser General Public License 3.0
// as published by the Free Software Foundation https://fsf.org
#if !SKIP_BRIDGE
#if SKIP
import SkipFoundation
import SkipFirebaseCore
import kotlinx.coroutines.tasks.await
import android.net.Uri
// https://firebase.google.com/docs/reference/swift/firebaseauth/api/reference/Classes/Auth
// https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuth
public final class Auth {
public let platformValue: com.google.firebase.auth.FirebaseAuth
public init(platformValue: com.google.firebase.auth.FirebaseAuth) {
self.platformValue = platformValue
}
public static func auth() -> Auth {
Auth(platformValue: com.google.firebase.auth.FirebaseAuth.getInstance())
}
public static func auth(app: FirebaseApp) -> Auth {
Auth(platformValue: com.google.firebase.auth.FirebaseAuth.getInstance(app.app))
}
public var app: FirebaseApp {
FirebaseApp(app: platformValue.getApp())
}
public var currentUser: User? {
guard let user = platformValue.currentUser else { return nil }
return User(user)
}
/// Throws `FirebaseAuthInvalidUserException`/`FirebaseAuthInvalidCredentialsException`/`FirebaseAuthInvalidCredentialsException`
/// https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuth#signInWithEmailAndPassword(java.lang.String,java.lang.String)
public func signIn(withEmail email: String, password: String) async throws -> AuthDataResult {
let result = platformValue.signInWithEmailAndPassword(email, password).await()
return AuthDataResult(result)
}
/// Throws `FirebaseAuthWeakPasswordException`/`FirebaseAuthInvalidCredentialsException`/`FirebaseAuthUserCollisionException`
/// https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuth#createUserWithEmailAndPassword(java.lang.String,java.lang.String)
public func createUser(withEmail email: String, password: String) async throws -> AuthDataResult {
let result = platformValue.createUserWithEmailAndPassword(email, password).await()
return AuthDataResult(result)
}
/// Does not throw from Kotlin
public func signOut() throws {
platformValue.signOut()
}
/// Throws `FirebaseAuthInvalidUserException`
public func sendPasswordReset(withEmail email: String) async throws {
platformValue.sendPasswordResetEmail(email).await()
}
/// Throws `Exception`
public func signInAnonymously() async throws -> AuthDataResult {
let result = platformValue.signInAnonymously().await()
return AuthDataResult(result)
}
public func useEmulator(withHost host: String, port: Int) {
platformValue.useEmulator(host, port)
}
public func addStateDidChangeListener(_ listener: @escaping (Auth, User?) -> Void) -> AuthStateListener {
let stateListener = com.google.firebase.auth.FirebaseAuth.AuthStateListener { auth in
let user = auth.currentUser != nil ? User(auth.currentUser!) : nil
listener(Auth(platformValue: auth), user)
}
platformValue.addAuthStateListener(stateListener)
return AuthStateListener(platformValue: stateListener)
}
public func removeStateDidChangeListener(_ listenerHandle: NSObjectProtocol) {
platformValue.removeAuthStateListener((listenerHandle as AuthStateListener).platformValue)
}
}
public class AuthDataResult: KotlinConverting<com.google.firebase.auth.AuthResult> {
public let platformValue: com.google.firebase.auth.AuthResult
public init(_ platformValue: com.google.firebase.auth.AuthResult) {
self.platformValue = platformValue
}
public override func kotlin(nocopy: Bool = false) -> com.google.firebase.auth.AuthResult {
platformValue
}
public var description: String {
platformValue.toString()
}
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.platformValue == rhs.platformValue
}
public var user: User {
User(platformValue.user!)
}
}
public class AuthStateListener: NSObjectProtocol {
public let platformValue: com.google.firebase.auth.FirebaseAuth.AuthStateListener
public init(platformValue: com.google.firebase.auth.FirebaseAuth.AuthStateListener) {
self.platformValue = platformValue
}
}
public class User: KotlinConverting<com.google.firebase.auth.FirebaseUser> {
public let platformValue: com.google.firebase.auth.FirebaseUser
public init(_ platformValue: com.google.firebase.auth.FirebaseUser) {
self.platformValue = platformValue
}
public override func kotlin(nocopy: Bool = false) -> com.google.firebase.auth.FirebaseUser {
platformValue
}
public var description: String {
platformValue.toString()
}
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.platformValue == rhs.platformValue
}
public var isAnonymous: Bool {
platformValue.isAnonymous
}
public var isEmailVerified: Bool {
platformValue.isEmailVerified
}
public var providerID: String? {
platformValue.providerId
}
public var uid: String {
platformValue.uid
}
public var displayName: String? {
platformValue.displayName
}
public var photoURL: URL? {
guard let uri = platformValue.photoUrl else { return nil }
return URL(string: uri.toString())!
}
public var email: String? {
platformValue.email
}
public var phoneNumber: String? {
platformValue.phoneNumber
}
public var metadata: UserMetadata {
UserMetadata(platformValue.metadata)
}
public func createProfileChangeRequest() -> UserProfileChangeRequest {
return UserProfileChangeRequest(self)
}
/// Throws `FirebaseAuthInvalidUserException`/`FirebaseAuthRecentLoginRequiredException`
/// https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseUser#delete()
public func delete() async throws {
platformValue.delete().await()
}
}
public class UserMetadata {
// On iOS, UserMetadata is never nil but its properties can be. On Android, it's the opposite.
public let userMetadata: com.google.firebase.auth.FirebaseUserMetadata?
public init(_ userMetadata: com.google.firebase.auth.FirebaseUserMetadata?) {
self.userMetadata = userMetadata
}
public var creationDate: Date? {
guard let milliseconds = userMetadata?.getCreationTimestamp() else { return nil }
return Date(timeIntervalSince1970: Double(milliseconds) / 1000)
}
public var lastSignInDate: Date? {
guard let milliseconds = userMetadata?.getLastSignInTimestamp() else { return nil }
return Date(timeIntervalSince1970: Double(milliseconds) / 1000)
}
}
public class UserProfileChangeRequest/*: KotlinConverting<com.google.firebase.auth.UserProfileChangeRequest>*/ {
var user: User
fileprivate init(user: User) {
self.user = user
}
public var displayName: String?
/// Throws `FirebaseAuthInvalidUserException`
/// https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseUser#updateProfile(com.google.firebase.auth.UserProfileChangeRequest)
public func commitChanges() async throws {
let builder = com.google.firebase.auth.UserProfileChangeRequest.Builder()
if let displayName {
builder.setDisplayName(displayName)
}
let platformChangeRequest: com.google.firebase.auth.UserProfileChangeRequest = builder.build()
user.platformValue.updateProfile(platformChangeRequest).await()
}
}
#endif
#endif