Skip to content

Commit

Permalink
✏️ Fix username change issues
Browse files Browse the repository at this point in the history
  • Loading branch information
benlmyers committed Feb 11, 2022
1 parent c47095c commit 4965c20
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 12 additions & 4 deletions Sources/EasyFirebase/Services/Auth/EasyUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public extension EasyUser {
completion: @escaping (Error?, String?) -> Void) where T: EasyUser {
EasyAuth.checkUsernameAvailable(newUsername, forUserType: T.self) { available in
if available {
self.unsafelyUpdateUsername(to: newUsername) { error in
self.unsafelyUpdateUsername(to: newUsername, ofUserType: T.self) { error in
completion(error, nil)
return
}
Expand All @@ -228,9 +228,17 @@ public extension EasyUser {
If you wish to update the user's username if it is available and provide a suggested username upon failure, see ``safelyUpdateUsername(to:suggesting:completion:)``.
*/
func unsafelyUpdateUsername(to newUsername: String, completion: @escaping (Error?) -> Void = { _ in }) {
func unsafelyUpdateUsername<T>(to newUsername: String, ofUserType: T.Type, completion: @escaping (Error?) -> Void = { _ in }) where T: EasyUser {
let oldUsername = username
self.username = newUsername
set(\.username, completion: completion)
set(\.username, ofUserType: T.self, completion: { error in
if let error = error {
completion(error)
self.username = oldUsername
} else {
completion(nil)
}
})
}

/**
Expand Down Expand Up @@ -360,7 +368,7 @@ public extension EasyUser {
let new = generator(base)
EasyAuth.checkUsernameAvailable(new, forUserType: Self.self) { available in
if available {
completion(base)
completion(new)
} else {
self.getUniqueUsername(new, using: generator, completion: completion)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ extension Document {
- parameter path: The path to the field to update remotely.
- parameter completion: The completion handler.
*/
public func set<T>(_ path: KeyPath<Self, T>, completion: @escaping (Error?) -> Void = { _ in }) where T: Codable {
EasyFirestore.Storage.set(path, in: self, completion: completion)
public func set<T, U>(_ path: KeyPath<T, U>, ofUserType: T.Type, completion: @escaping (Error?) -> Void = { _ in }) where T: Document, U: Codable {
EasyFirestore.Storage.set(path, in: self as! T, completion: completion)
}

/**
Expand Down

0 comments on commit 4965c20

Please sign in to comment.