Skip to content

Commit 499a4e8

Browse files
committed
feat: add optional parameters to Spotify login
1 parent 34d6a9f commit 499a4e8

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

Sources/ParseSwift/Authentication/3rd Party/ParseSpotify/ParseSpotify+async.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,25 @@ public extension ParseSpotify {
1616
Login a `ParseUser` *asynchronously* using Spotify authentication.
1717
- parameter id: The **Spotify profile id** from **Spotify**.
1818
- parameter accessToken: Required **access_token** from **Spotify**.
19+
- parameter clientId: Optional **client_id** from **Spotify**.
20+
- parameter expiresIn: Optional **expires_in** in seconds from **Spotify**.
21+
- parameter refreshToken: Optional **refresh_token** from **Spotify**.
1922
- parameter options: A set of header options sent to the server. Defaults to an empty set.
2023
- returns: An instance of the logged in `ParseUser`.
2124
- throws: An error of type `ParseError`.
2225
*/
2326
func login(id: String,
2427
accessToken: String,
28+
clientId: String? = nil,
29+
expiresIn: Int? = nil,
30+
refreshToken: String? = nil,
2531
options: API.Options = []) async throws -> AuthenticatedUser {
2632
try await withCheckedThrowingContinuation { continuation in
2733
self.login(id: id,
2834
accessToken: accessToken,
35+
clientId: clientId,
36+
expiresIn: expiresIn,
37+
refreshToken: refreshToken,
2938
options: options,
3039
completion: continuation.resume)
3140
}
@@ -53,16 +62,25 @@ public extension ParseSpotify {
5362
Link the *current* `ParseUser` *asynchronously* using Spotify authentication.
5463
- parameter id: The **Spotify profile id** from **Spotify**.
5564
- parameter accessToken: Required **access_token** from **Spotify**.
65+
- parameter clientId: Optional **client_id** from **Spotify**.
66+
- parameter expiresIn: Optional **expires_in** in seconds from **Spotify**.
67+
- parameter refreshToken: Optional **refresh_token** from **Spotify**.
5668
- parameter options: A set of header options sent to the server. Defaults to an empty set.
5769
- returns: An instance of the logged in `ParseUser`.
5870
- throws: An error of type `ParseError`.
5971
*/
6072
func link(id: String,
6173
accessToken: String,
74+
clientId: String? = nil,
75+
expiresIn: Int? = nil,
76+
refreshToken: String? = nil,
6277
options: API.Options = []) async throws -> AuthenticatedUser {
6378
try await withCheckedThrowingContinuation { continuation in
6479
self.link(id: id,
6580
accessToken: accessToken,
81+
clientId: clientId,
82+
expiresIn: expiresIn,
83+
refreshToken: refreshToken,
6684
options: options,
6785
completion: continuation.resume)
6886
}

Sources/ParseSwift/Authentication/3rd Party/ParseSpotify/ParseSpotify+combine.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,24 @@ public extension ParseSpotify {
1616
Login a `ParseUser` *asynchronously* using Spotify authentication. Publishes when complete.
1717
- parameter id: The **Spotify profile id** from **Spotify**.
1818
- parameter accessToken: Required **access_token** from **Spotify**.
19+
- parameter clientId: Optional **client_id** from **Spotify**.
20+
- parameter expiresIn: Optional **expires_in** in seconds from **Spotify**.
21+
- parameter refreshToken: Optional **refresh_token** from **Spotify**.
1922
- parameter options: A set of header options sent to the server. Defaults to an empty set.
2023
- returns: A publisher that eventually produces a single value and then finishes or fails.
2124
*/
2225
func loginPublisher(id: String,
2326
accessToken: String,
27+
clientId: String? = nil,
28+
expiresIn: Int? = nil,
29+
refreshToken: String? = nil,
2430
options: API.Options = []) -> Future<AuthenticatedUser, ParseError> {
2531
Future { promise in
2632
self.login(id: id,
2733
accessToken: accessToken,
34+
clientId: clientId,
35+
expiresIn: expiresIn,
36+
refreshToken: refreshToken,
2837
options: options,
2938
completion: promise)
3039
}
@@ -51,15 +60,24 @@ public extension ParseSpotify {
5160
Publishes when complete.
5261
- parameter id: The **Spotify profile id** from **Spotify**.
5362
- parameter accessToken: Required **access_token** from **Spotify**.
63+
- parameter clientId: Optional **client_id** from **Spotify**.
64+
- parameter expiresIn: Optional **expires_in** in seconds from **Spotify**.
65+
- parameter refreshToken: Optional **refresh_token** from **Spotify**.
5466
- parameter options: A set of header options sent to the server. Defaults to an empty set.
5567
- returns: A publisher that eventually produces a single value and then finishes or fails.
5668
*/
5769
func linkPublisher(id: String,
5870
accessToken: String,
71+
clientId: String? = nil,
72+
expiresIn: Int? = nil,
73+
refreshToken: String? = nil,
5974
options: API.Options = []) -> Future<AuthenticatedUser, ParseError> {
6075
Future { promise in
6176
self.link(id: id,
6277
accessToken: accessToken,
78+
clientId: clientId,
79+
expiresIn: expiresIn,
80+
refreshToken: refreshToken,
6381
options: options,
6482
completion: promise)
6583
}

Sources/ParseSwift/Authentication/3rd Party/ParseSpotify/ParseSpotify.swift

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,39 @@ public struct ParseSpotify<AuthenticatedUser: ParseUser>: ParseAuthentication {
2121
enum AuthenticationKeys: String, Codable {
2222
case id
2323
case accessToken = "access_token"
24-
24+
case clientId = "client_id"
25+
case expiresIn = "expires_in"
26+
case refreshToken = "refresh_token"
2527
/// Properly makes an authData dictionary with the required keys.
2628
/// - parameter id: Required id for the user.
2729
/// - parameter accessToken: Required access token for Spotify.
30+
/// - parameter clientId: Optional client id for Spotify.
31+
/// - parameter expiresIn: Optional expiration in seconds for Spotify.
32+
/// - parameter refreshToken: Optional refresh token for Spotify.
2833
/// - returns: authData dictionary.
2934
func makeDictionary(id: String,
30-
accessToken: String) -> [String: String] {
35+
accessToken: String,
36+
clientId: String? = nil,
37+
expiresIn: Int? = nil,
38+
refreshToken: String? = nil) -> [String: String] {
3139

32-
let returnDictionary = [
40+
var returnDictionary = [
3341
AuthenticationKeys.id.rawValue: id,
3442
AuthenticationKeys.accessToken.rawValue: accessToken
3543
]
44+
if let clientId = clientId {
45+
returnDictionary[AuthenticationKeys.clientId.rawValue] = clientId
46+
}
47+
if let expiresIn = expiresIn,
48+
let expirationDate = Calendar.current.date(byAdding: .second,
49+
value: expiresIn,
50+
to: Date()) {
51+
let dateString = ParseCoding.dateFormatter.string(from: expirationDate)
52+
returnDictionary[AuthenticationKeys.expiresIn.rawValue] = dateString
53+
}
54+
if let refreshToken = refreshToken {
55+
returnDictionary[AuthenticationKeys.refreshToken.rawValue] = refreshToken
56+
}
3657
return returnDictionary
3758
}
3859

@@ -62,20 +83,28 @@ public extension ParseSpotify {
6283
Login a `ParseUser` *asynchronously* using Spotify authentication.
6384
- parameter id: The **Spotify profile id** from **Spotify**.
6485
- parameter accessToken: Required **access_token** from **Spotify**.
86+
- parameter clientId: Optional **client_id** from **Spotify**.
87+
- parameter expiresIn: Optional **expires_in** in seconds from **Spotify**.
88+
- parameter refreshToken: Optional **refresh_token** from **Spotify**.
6589
- parameter options: A set of header options sent to the server. Defaults to an empty set.
6690
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
6791
- parameter completion: The block to execute.
6892
*/
6993
func login(id: String,
7094
accessToken: String,
95+
clientId: String? = nil,
96+
expiresIn: Int? = nil,
97+
refreshToken: String? = nil,
7198
options: API.Options = [],
7299
callbackQueue: DispatchQueue = .main,
73100
completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) {
74101

75102
let spotifyAuthData = AuthenticationKeys.id
76103
.makeDictionary(id: id,
77-
accessToken: accessToken)
78-
// print(spotifyAuthData)
104+
accessToken: accessToken,
105+
clientId: clientId,
106+
expiresIn: expiresIn,
107+
refreshToken: refreshToken)
79108
login(authData: spotifyAuthData,
80109
options: options,
81110
callbackQueue: callbackQueue,
@@ -108,18 +137,27 @@ public extension ParseSpotify {
108137
Link the *current* `ParseUser` *asynchronously* using Spotify authentication.
109138
- parameter id: The **Spotify profile id** from **Spotify**.
110139
- parameter accessToken: Required **access_token** from **Spotify**.
140+
- parameter clientId: Optional **client_id** from **Spotify**.
141+
- parameter expiresIn: Optional **expires_in** in seconds from **Spotify**.
142+
- parameter refreshToken: Optional **refresh_token** from **Spotify**.
111143
- parameter options: A set of header options sent to the server. Defaults to an empty set.
112144
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
113145
- parameter completion: The block to execute.
114146
*/
115147
func link(id: String,
116148
accessToken: String,
149+
clientId: String? = nil,
150+
expiresIn: Int? = nil,
151+
refreshToken: String? = nil,
117152
options: API.Options = [],
118153
callbackQueue: DispatchQueue = .main,
119154
completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) {
120155
let spotifyAuthData = AuthenticationKeys.id
121156
.makeDictionary(id: id,
122-
accessToken: accessToken)
157+
accessToken: accessToken,
158+
clientId: clientId,
159+
expiresIn: expiresIn,
160+
refreshToken: refreshToken)
123161
link(authData: spotifyAuthData,
124162
options: options,
125163
callbackQueue: callbackQueue,

0 commit comments

Comments
 (0)