Skip to content

Commit

Permalink
feat: Make it easier to login with email or authData (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaker6 authored Mar 10, 2023
1 parent 62eee27 commit 11bb8e6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* _Contributing to this repo? Add info about your change here to be included in the next release_

__New features__
* 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).
* 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).

### 5.0.1
Expand Down
11 changes: 9 additions & 2 deletions Sources/ParseSwift/Objects/ParseUser+async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,27 @@ public extension ParseUser {
Returns an instance of the successfully logged in `ParseUser`.

This also caches the user locally so that calls to *current* will use the latest logged in user.
- parameter username: The username of the user.
- parameter username: The username of the user. Defauilts to **nil**.
- parameter email: The email address associated with the user that forgot their password.
Defauilts to **nil**.
- parameter password: The password of the user.
- parameter authData: The authentication data for the `ParseUser`. Defauilts to **nil**.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- returns: Returns the logged in `ParseUser`.
- throws: An error of type `ParseError`.
- note: The default cache policy for this method is `.reloadIgnoringLocalCacheData`. If a developer
desires a different policy, it should be inserted in `options`.
*/
@discardableResult static func login(username: String,
@discardableResult static func login(username: String? = nil,
email: String? = nil,
password: String,
authData: [String: [String: String]?]? = nil,
options: API.Options = []) async throws -> Self {
try await withCheckedThrowingContinuation { continuation in
Self.login(username: username,
email: email,
password: password,
authData: authData,
options: options,
completion: continuation.resume)
}
Expand Down
15 changes: 11 additions & 4 deletions Sources/ParseSwift/Objects/ParseUser+combine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public extension ParseUser {

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

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

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

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

This also caches the user locally so that calls to *current* will use the latest logged in user.
- parameter username: The username of the user.
- parameter username: The username of the user. Defauilts to **nil**.
- parameter email: The email address associated with the user that forgot their password.
Defauilts to **nil**.
- parameter password: The password of the user.
- parameter authData: The authentication data for the `ParseUser`. Defauilts to **nil**.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- returns: A publisher that eventually produces a single value and then finishes or fails.
- note: The default cache policy for this method is `.reloadIgnoringLocalCacheData`. If a developer
desires a different policy, it should be inserted in `options`.
*/
static func loginPublisher(username: String,
static func loginPublisher(username: String? = nil,
email: String? = nil,
password: String,
authData: [String: [String: String]?]? = nil,
options: API.Options = []) -> Future<Self, ParseError> {
Future { promise in
Self.login(username: username,
email: email,
password: password,
authData: authData,
options: options,
completion: promise)
}
Expand Down
38 changes: 22 additions & 16 deletions Sources/ParseSwift/Objects/ParseUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,9 @@ public extension ParseUser {
// MARK: SignupLoginBody
struct SignupLoginBody: ParseEncodable {
var username: String?
var email: String?
var password: String?
var authData: [String: [String: String]?]?

init(username: String, password: String) {
self.username = username
self.password = password
}

init(authData: [String: [String: String]?]) {
self.authData = authData
}
}

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

This also caches the user locally so that calls to *current* will use the latest logged in user.
- parameter username: The username of the user.
This also stores the user locally so that calls to *current* will use the latest logged in user.
- parameter username: The username of the user. Defauilts to **nil**.
- parameter email: The email address associated with the user that forgot their password.
Defauilts to **nil**.
- parameter password: The password of the user.
- parameter authData: The authentication data for the `ParseUser`. Defauilts to **nil**.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
- parameter completion: The block to execute.
Expand All @@ -224,26 +219,37 @@ extension ParseUser {
desires a different policy, it should be inserted in `options`.
*/
public static func login(
username: String,
username: String? = nil,
email: String? = nil,
password: String,
authData: [String: [String: String]?]? = nil,
options: API.Options = [],
callbackQueue: DispatchQueue = .main,
completion: @escaping (Result<Self, ParseError>) -> Void
) {
Task {
var options = options
options.insert(.cachePolicy(.reloadIgnoringLocalCacheData))
await loginCommand(username: username, password: password)
await loginCommand(username: username,
email: email,
password: password,
authData: authData)
.execute(options: options,
callbackQueue: callbackQueue,
completion: completion)
}
}

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

let body = SignupLoginBody(username: username, password: password)
let body = SignupLoginBody(username: username,
email: email,
password: password,
authData: authData)
return API.Command<SignupLoginBody, Self>(method: .POST,
path: .login,
body: body) { (data) async throws -> Self in
Expand Down

0 comments on commit 11bb8e6

Please sign in to comment.