@superfan-app/spotify-auth 0.1.44 → 0.1.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -44,6 +44,12 @@ class SpotifyOAuthView: ExpoView {
44
44
  private static let authTimeoutInterval: TimeInterval = 300 // 5 minutes
45
45
  private var observerToken: NSKeyValueObservation?
46
46
 
47
+ private func secureLog(_ message: String) {
48
+ #if DEBUG
49
+ print("[SpotifyOAuthView] \(message)")
50
+ #endif
51
+ }
52
+
47
53
  required init(appContext: AppContext? = nil) {
48
54
  // Generate a random state string for CSRF protection
49
55
  self.state = UUID().uuidString
@@ -65,6 +71,7 @@ class SpotifyOAuthView: ExpoView {
65
71
  return
66
72
  }
67
73
 
74
+ secureLog("Setting up WebView configuration...")
68
75
  // Create a configuration that prevents data persistence
69
76
  let config: WKWebViewConfiguration = {
70
77
  let configuration = WKWebViewConfiguration()
@@ -76,12 +83,18 @@ class SpotifyOAuthView: ExpoView {
76
83
  // Ensure cookies and data are not persisted
77
84
  let dataStore = WKWebsiteDataStore.nonPersistent()
78
85
  configuration.websiteDataStore = dataStore
86
+ secureLog("WebView configuration created with non-persistent data store")
79
87
  return configuration
80
88
  }()
81
89
 
82
90
  // Initialize webview on main thread with error handling
83
91
  do {
84
92
  webView = WKWebView(frame: .zero, configuration: config)
93
+ guard webView != nil else {
94
+ throw NSError(domain: "SpotifyAuth", code: -1, userInfo: [NSLocalizedDescriptionKey: "Failed to initialize WebView"])
95
+ }
96
+ secureLog("WebView successfully initialized")
97
+
85
98
  webView.navigationDelegate = self
86
99
  webView.allowsBackForwardNavigationGestures = true
87
100
  webView.customUserAgent = "SpotifyAuth-iOS/1.0" // Custom UA to identify our app
@@ -123,6 +136,14 @@ class SpotifyOAuthView: ExpoView {
123
136
  }
124
137
 
125
138
  func startOAuthFlow(clientId: String, redirectUri: String, scopes: [String], showDialog: Bool = false, campaign: String? = nil) {
139
+ // Ensure we're on the main thread - WebView setup must be done on the main thread
140
+ if !Thread.isMainThread {
141
+ DispatchQueue.main.async { [weak self] in
142
+ self?.startOAuthFlow(clientId: clientId, redirectUri: redirectUri, scopes: scopes, showDialog: showDialog, campaign: campaign)
143
+ }
144
+ return
145
+ }
146
+
126
147
  guard !isAuthenticating else { return }
127
148
  isAuthenticating = true
128
149
 
@@ -138,18 +159,24 @@ class SpotifyOAuthView: ExpoView {
138
159
  startAuthTimeout()
139
160
 
140
161
  // Clear any existing cookies/data to ensure a fresh login
162
+ // Wait for completion before initiating the auth request
141
163
  WKWebsiteDataStore.default().removeData(
142
164
  ofTypes: [WKWebsiteDataTypeCookies, WKWebsiteDataTypeSessionStorage],
143
165
  modifiedSince: Date(timeIntervalSince1970: 0)
144
- ) { }
145
-
146
- self.initiateAuthRequest(
147
- clientId: clientId,
148
- redirectUri: redirectUri,
149
- scopes: scopes,
150
- showDialog: showDialog,
151
- campaign: campaign
152
- )
166
+ ) { [weak self] in
167
+ // Ensure we're still in a valid state after the async operation
168
+ guard let self = self, self.isAuthenticating else { return }
169
+
170
+ DispatchQueue.main.async {
171
+ self.initiateAuthRequest(
172
+ clientId: clientId,
173
+ redirectUri: redirectUri,
174
+ scopes: scopes,
175
+ showDialog: showDialog,
176
+ campaign: campaign
177
+ )
178
+ }
179
+ }
153
180
  }
154
181
 
155
182
  private func startAuthTimeout() {
@@ -185,6 +212,14 @@ class SpotifyOAuthView: ExpoView {
185
212
  return
186
213
  }
187
214
 
215
+ // Verify webView is properly initialized
216
+ guard let webView = self.webView else {
217
+ secureLog("Error: WebView not initialized when attempting to load auth request")
218
+ isAuthenticating = false
219
+ delegate?.oauthView(self, didFailWithError: SpotifyOAuthError.authorizationError("WebView not initialized"))
220
+ return
221
+ }
222
+
188
223
  var queryItems = [
189
224
  URLQueryItem(name: "client_id", value: clientId),
190
225
  URLQueryItem(name: "response_type", value: "code"),
@@ -207,7 +242,22 @@ class SpotifyOAuthView: ExpoView {
207
242
  }
208
243
 
209
244
  let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData)
210
- webView.load(request)
245
+ secureLog("Initiating auth request to URL: \(url.absoluteString)")
246
+
247
+ DispatchQueue.main.async {
248
+ guard Thread.isMainThread else {
249
+ assertionFailure("WebView load not on main thread despite DispatchQueue.main.async")
250
+ return
251
+ }
252
+
253
+ if webView.isLoading {
254
+ secureLog("Warning: WebView is already loading content, stopping previous load")
255
+ webView.stopLoading()
256
+ }
257
+
258
+ webView.load(request)
259
+ self.secureLog("Auth request load initiated")
260
+ }
211
261
  }
212
262
 
213
263
  deinit {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superfan-app/spotify-auth",
3
- "version": "0.1.44",
3
+ "version": "0.1.46",
4
4
  "description": "Spotify OAuth module for Expo",
5
5
  "main": "src/index.tsx",
6
6
  "types": "build/index.d.ts",