Skip to content

Commit 11bb8e6

Browse files
authored
feat: Make it easier to login with email or authData (#67)
1 parent 62eee27 commit 11bb8e6

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* _Contributing to this repo? Add info about your change here to be included in the next release_
77

88
__New features__
9+
* Make it easier to login with email or authData ([#67](https://github.com/netreconlab/Parse-Swift/pull/67)), thanks to [Corey Baker](https://github.com/cbaker6).
910
* Add missing async/await and Combine ParseUser.become() type methods ([#66](https://github.com/netreconlab/Parse-Swift/pull/66)), thanks to [Corey Baker](https://github.com/cbaker6).
1011

1112
### 5.0.1

Sources/ParseSwift/Objects/ParseUser+async.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,27 @@ public extension ParseUser {
6060
Returns an instance of the successfully logged in `ParseUser`.
6161

6262
This also caches the user locally so that calls to *current* will use the latest logged in user.
63-
- parameter username: The username of the user.
63+
- parameter username: The username of the user. Defauilts to **nil**.
64+
- parameter email: The email address associated with the user that forgot their password.
65+
Defauilts to **nil**.
6466
- parameter password: The password of the user.
67+
- parameter authData: The authentication data for the `ParseUser`. Defauilts to **nil**.
6568
- parameter options: A set of header options sent to the server. Defaults to an empty set.
6669
- returns: Returns the logged in `ParseUser`.
6770
- throws: An error of type `ParseError`.
6871
- note: The default cache policy for this method is `.reloadIgnoringLocalCacheData`. If a developer
6972
desires a different policy, it should be inserted in `options`.
7073
*/
71-
@discardableResult static func login(username: String,
74+
@discardableResult static func login(username: String? = nil,
75+
email: String? = nil,
7276
password: String,
77+
authData: [String: [String: String]?]? = nil,
7378
options: API.Options = []) async throws -> Self {
7479
try await withCheckedThrowingContinuation { continuation in
7580
Self.login(username: username,
81+
email: email,
7682
password: password,
83+
authData: authData,
7784
options: options,
7885
completion: continuation.resume)
7986
}

Sources/ParseSwift/Objects/ParseUser+combine.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ public extension ParseUser {
1818

1919
This will also enforce that the username is not already taken.
2020

21-
- warning: Make sure that password and username are set before calling this method.
2221
- parameter username: The username of the user.
2322
- parameter password: The password of the user.
2423
- parameter options: A set of header options sent to the server. Defaults to an empty set.
2524
- returns: A publisher that eventually produces a single value and then finishes or fails.
25+
- warning: Make sure that password and username are set before calling this method.
2626
- note: The default cache policy for this method is `.reloadIgnoringLocalCacheData`. If a developer
2727
desires a different policy, it should be inserted in `options`.
2828
*/
@@ -42,9 +42,9 @@ public extension ParseUser {
4242

4343
This will also enforce that the username is not already taken.
4444

45-
- warning: Make sure that password and username are set before calling this method.
4645
- parameter options: A set of header options sent to the server. Defaults to an empty set.
4746
- returns: A publisher that eventually produces a single value and then finishes or fails.
47+
- warning: Make sure that password and username are set before calling this method.
4848
- note: The default cache policy for this method is `.reloadIgnoringLocalCacheData`. If a developer
4949
desires a different policy, it should be inserted in `options`.
5050
*/
@@ -60,19 +60,26 @@ public extension ParseUser {
6060
Publishes an instance of the successfully logged in `ParseUser`.
6161

6262
This also caches the user locally so that calls to *current* will use the latest logged in user.
63-
- parameter username: The username of the user.
63+
- parameter username: The username of the user. Defauilts to **nil**.
64+
- parameter email: The email address associated with the user that forgot their password.
65+
Defauilts to **nil**.
6466
- parameter password: The password of the user.
67+
- parameter authData: The authentication data for the `ParseUser`. Defauilts to **nil**.
6568
- parameter options: A set of header options sent to the server. Defaults to an empty set.
6669
- returns: A publisher that eventually produces a single value and then finishes or fails.
6770
- note: The default cache policy for this method is `.reloadIgnoringLocalCacheData`. If a developer
6871
desires a different policy, it should be inserted in `options`.
6972
*/
70-
static func loginPublisher(username: String,
73+
static func loginPublisher(username: String? = nil,
74+
email: String? = nil,
7175
password: String,
76+
authData: [String: [String: String]?]? = nil,
7277
options: API.Options = []) -> Future<Self, ParseError> {
7378
Future { promise in
7479
Self.login(username: username,
80+
email: email,
7581
password: password,
82+
authData: authData,
7683
options: options,
7784
completion: promise)
7885
}

Sources/ParseSwift/Objects/ParseUser.swift

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,9 @@ public extension ParseUser {
188188
// MARK: SignupLoginBody
189189
struct SignupLoginBody: ParseEncodable {
190190
var username: String?
191+
var email: String?
191192
var password: String?
192193
var authData: [String: [String: String]?]?
193-
194-
init(username: String, password: String) {
195-
self.username = username
196-
self.password = password
197-
}
198-
199-
init(authData: [String: [String: String]?]) {
200-
self.authData = authData
201-
}
202194
}
203195

204196
// MARK: EmailBody
@@ -213,9 +205,12 @@ extension ParseUser {
213205
Makes an *asynchronous* request to log in a user with specified credentials.
214206
Returns an instance of the successfully logged in `ParseUser`.
215207

216-
This also caches the user locally so that calls to *current* will use the latest logged in user.
217-
- parameter username: The username of the user.
208+
This also stores the user locally so that calls to *current* will use the latest logged in user.
209+
- parameter username: The username of the user. Defauilts to **nil**.
210+
- parameter email: The email address associated with the user that forgot their password.
211+
Defauilts to **nil**.
218212
- parameter password: The password of the user.
213+
- parameter authData: The authentication data for the `ParseUser`. Defauilts to **nil**.
219214
- parameter options: A set of header options sent to the server. Defaults to an empty set.
220215
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
221216
- parameter completion: The block to execute.
@@ -224,26 +219,37 @@ extension ParseUser {
224219
desires a different policy, it should be inserted in `options`.
225220
*/
226221
public static func login(
227-
username: String,
222+
username: String? = nil,
223+
email: String? = nil,
228224
password: String,
225+
authData: [String: [String: String]?]? = nil,
229226
options: API.Options = [],
230227
callbackQueue: DispatchQueue = .main,
231228
completion: @escaping (Result<Self, ParseError>) -> Void
232229
) {
233230
Task {
234231
var options = options
235232
options.insert(.cachePolicy(.reloadIgnoringLocalCacheData))
236-
await loginCommand(username: username, password: password)
233+
await loginCommand(username: username,
234+
email: email,
235+
password: password,
236+
authData: authData)
237237
.execute(options: options,
238238
callbackQueue: callbackQueue,
239239
completion: completion)
240240
}
241241
}
242242

243-
internal static func loginCommand(username: String,
244-
password: String) -> API.Command<SignupLoginBody, Self> {
243+
internal static func loginCommand(username: String? = nil,
244+
email: String? = nil,
245+
password: String,
246+
// swiftlint:disable:next line_length
247+
authData: [String: [String: String]?]? = nil) -> API.Command<SignupLoginBody, Self> {
245248

246-
let body = SignupLoginBody(username: username, password: password)
249+
let body = SignupLoginBody(username: username,
250+
email: email,
251+
password: password,
252+
authData: authData)
247253
return API.Command<SignupLoginBody, Self>(method: .POST,
248254
path: .login,
249255
body: body) { (data) async throws -> Self in

0 commit comments

Comments
 (0)