@capgo/capacitor-social-login 8.5.19 → 8.7.0

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.
@@ -189,11 +189,8 @@ class AppleProvider: NSObject, ASAuthorizationControllerDelegate, ASAuthorizatio
189
189
  // Convert the object to JSON data
190
190
  let jsonData = try JSONSerialization.data(withJSONObject: object, options: [])
191
191
 
192
- // Convert JSON data to a string for logging
192
+ // Convert JSON data to a string. It holds the tokens, so it is not logged.
193
193
  if let jsonString = String(data: jsonData, encoding: .utf8) {
194
- // Log the object
195
- print("Apple persistState: \(jsonString)")
196
-
197
194
  // Save the JSON string to UserDefaults or use your helper method
198
195
  UserDefaults.standard.set(jsonString, forKey: SHARED_PREFERENCE_NAME)
199
196
  } else {
@@ -232,9 +229,6 @@ class AppleProvider: NSObject, ASAuthorizationControllerDelegate, ASAuthorizatio
232
229
  self.idToken = idToken
233
230
  self.refreshToken = refreshToken
234
231
  self.accessToken = accessToken
235
-
236
- // Log the retrieved object
237
- print("Apple retrieveState: \(object)")
238
232
  }
239
233
 
240
234
  func login(payload: [String: Any], completion: @escaping (Result<AppleProviderResponse, Error>) -> Void) {
@@ -266,7 +260,12 @@ class AppleProvider: NSObject, ASAuthorizationControllerDelegate, ASAuthorizatio
266
260
  }
267
261
  }
268
262
 
269
- func logout(completion: @escaping (Result<Void, Error>) -> Void) {
263
+ func logout(clearCachedUserInfo: Bool = false, completion: @escaping (Result<Void, Error>) -> Void) {
264
+ // First, so it also happens without a session: the cached name outlives the tokens.
265
+ if clearCachedUserInfo {
266
+ UserDefaults.standard.removeObject(forKey: USER_INFO_KEY)
267
+ }
268
+
270
269
  // we check only idtoken, because with apple, refresh token MIGHT not be set
271
270
  if self.idToken == nil || ((self.idToken?.isEmpty) == true) {
272
271
 
@@ -336,7 +335,6 @@ class AppleProvider: NSObject, ASAuthorizationControllerDelegate, ASAuthorizatio
336
335
 
337
336
  if let decodedData = Data(base64Encoded: base64String, options: []),
338
337
  let payload = try? JSONSerialization.jsonObject(with: decodedData, options: []) as? [String: Any] {
339
- print("payload", payload)
340
338
  decodedEmail = payload["email"] as? String ?? email
341
339
  }
342
340
  }
@@ -444,15 +442,14 @@ class AppleProvider: NSObject, ASAuthorizationControllerDelegate, ASAuthorizatio
444
442
  // Check if the response is a redirect
445
443
  if (300...399).contains(httpResponse.statusCode) {
446
444
  if let location = httpResponse.value(forHTTPHeaderField: "Location") {
447
- print("Redirect Location: \(location)")
445
+ // Neither the redirect URL nor its query items are logged: they carry
446
+ // the authorization code or the tokens.
448
447
 
449
448
  // Parse the redirect URL
450
449
  if let redirectURL = URL(string: location),
451
450
  let urlComponents = URLComponents(url: redirectURL, resolvingAgainstBaseURL: false),
452
451
  let pathComponents = urlComponents.queryItems {
453
452
 
454
- print("Query items: \(String(describing: urlComponents.queryItems))")
455
-
456
453
  // there are 4 main ways this can go:
457
454
  // 1. it provides the "code" and we fetch apple servers in order to get the JWT (yuck)
458
455
  // 2. It doesn't provide the code but it provides access_token, refresh_token, id_token
@@ -543,10 +540,9 @@ class AppleProvider: NSObject, ASAuthorizationControllerDelegate, ASAuthorizatio
543
540
  return
544
541
  }
545
542
  } else {
546
- // Handle non-redirect responses
547
- if let data = data,
548
- let responseString = String(data: data, encoding: .utf8) {
549
- print("Response: \(responseString)")
543
+ // Handle non-redirect responses. The body is not logged: it can carry tokens.
544
+ if let data = data, !data.isEmpty {
545
+ print("Received a non-redirect response (\(data.count) bytes)")
550
546
  } else {
551
547
  print("No response data received")
552
548
  }
@@ -659,13 +655,6 @@ class AppleProvider: NSObject, ASAuthorizationControllerDelegate, ASAuthorizatio
659
655
  authorizationCode: nil
660
656
  )
661
657
 
662
- // Log the tokens (replace with your logging mechanism)
663
- print("Apple Access Token is: \(accessToken)")
664
- print("Expires in: \(expiresIn)")
665
- print("Refresh token: \(refreshToken)")
666
- print("ID Token: \(idToken)")
667
- print("Apple User ID: \(userId)")
668
-
669
658
  do {
670
659
  try self.persistState(idToken: idToken, refreshToken: refreshToken, accessToken: accessToken)
671
660
  } catch {
@@ -16,7 +16,7 @@ import GoogleSignIn
16
16
  */
17
17
  @objc(SocialLoginPlugin)
18
18
  public class SocialLoginPlugin: CAPPlugin, CAPBridgedPlugin {
19
- private let pluginVersion: String = "8.5.19"
19
+ private let pluginVersion: String = "8.7.0"
20
20
  public let identifier = "SocialLoginPlugin"
21
21
  public let jsName = "SocialLogin"
22
22
  private static let userCancelledCode = "USER_CANCELLED"
@@ -40,7 +40,8 @@ public class SocialLoginPlugin: CAPPlugin, CAPBridgedPlugin {
40
40
  CAPPluginMethod(name: "isRefreshTokenAvailable", returnType: CAPPluginReturnPromise),
41
41
  CAPPluginMethod(name: "providerSpecificCall", returnType: CAPPluginReturnPromise),
42
42
  CAPPluginMethod(name: "getPluginVersion", returnType: CAPPluginReturnPromise),
43
- CAPPluginMethod(name: "openSecureWindow", returnType: CAPPluginReturnPromise)
43
+ CAPPluginMethod(name: "openSecureWindow", returnType: CAPPluginReturnPromise),
44
+ CAPPluginMethod(name: "openAuthSession", returnType: CAPPluginReturnPromise)
44
45
  ]
45
46
 
46
47
  // Providers - conditionally initialized based on available dependencies
@@ -62,7 +63,9 @@ public class SocialLoginPlugin: CAPPlugin, CAPBridgedPlugin {
62
63
  private let twitter = TwitterProvider()
63
64
  private let oauth2 = OAuth2Provider()
64
65
  private let telegram = TelegramProvider()
65
- private var openSecureWindowCall: CAPPluginCall?
66
+ private var webAuthSessionCall: CAPPluginCall?
67
+ private var webAuthSessionRedirectPrefix: String?
68
+ private var webAuthSessionResultKey: String = "callbackURL"
66
69
 
67
70
  // Helper to get Facebook provider (returns nil if unavailable)
68
71
  private var facebookProvider: FacebookProvider? {
@@ -586,7 +589,7 @@ public class SocialLoginPlugin: CAPPlugin, CAPBridgedPlugin {
586
589
  call.reject("Apple Sign-In provider is disabled in capacitor.config")
587
590
  return
588
591
  }
589
- apple.logout { result in
592
+ apple.logout(clearCachedUserInfo: call.getBool("clearCachedUserInfo") ?? false) { result in
590
593
  self.handleLogoutResult(result, call: call)
591
594
  }
592
595
  case "twitter":
@@ -777,43 +780,110 @@ public class SocialLoginPlugin: CAPPlugin, CAPBridgedPlugin {
777
780
  return
778
781
  }
779
782
 
780
- // Store the call for later resolution
781
- self.openSecureWindowCall = call
783
+ guard let callbackScheme = URL(string: redirectUri)?.scheme, !callbackScheme.isEmpty else {
784
+ call.reject("redirectUri must include a URL scheme")
785
+ return
786
+ }
787
+
788
+ startWebAuthSession(
789
+ call: call,
790
+ url: url,
791
+ callbackURLScheme: callbackScheme,
792
+ redirectPrefix: redirectUri,
793
+ prefersEphemeral: false,
794
+ resultKey: "redirectedUri"
795
+ )
796
+ }
797
+
798
+ @objc func openAuthSession(_ call: CAPPluginCall) {
799
+ guard let urlString = call.getString("url") else {
800
+ call.reject("url is required")
801
+ return
802
+ }
803
+
804
+ guard let url = URL(string: urlString) else {
805
+ call.reject("Invalid URL")
806
+ return
807
+ }
808
+
809
+ guard let callbackURLScheme = call.getString("callbackURLScheme"), !callbackURLScheme.isEmpty else {
810
+ call.reject("callbackURLScheme is required")
811
+ return
812
+ }
813
+
814
+ let prefersEphemeral = call.getBool("prefersEphemeralSession") ?? false
815
+
816
+ startWebAuthSession(
817
+ call: call,
818
+ url: url,
819
+ callbackURLScheme: callbackURLScheme,
820
+ redirectPrefix: nil,
821
+ prefersEphemeral: prefersEphemeral,
822
+ resultKey: "callbackURL"
823
+ )
824
+ }
825
+
826
+ private func startWebAuthSession(
827
+ call: CAPPluginCall,
828
+ url: URL,
829
+ callbackURLScheme: String,
830
+ redirectPrefix: String?,
831
+ prefersEphemeral: Bool,
832
+ resultKey: String
833
+ ) {
834
+ if webAuthSessionCall != nil {
835
+ call.reject("Another auth session is already in progress")
836
+ return
837
+ }
838
+
839
+ webAuthSessionCall = call
840
+ webAuthSessionRedirectPrefix = redirectPrefix
841
+ webAuthSessionResultKey = resultKey
782
842
 
783
- // Open the URL in a secure browser window
784
843
  DispatchQueue.main.async {
785
- let session = ASWebAuthenticationSession(url: url, callbackURLScheme: url.scheme) {
786
- callbackURL, error in
844
+ let session = ASWebAuthenticationSession(url: url, callbackURLScheme: callbackURLScheme) { callbackURL, error in
845
+ let activeCall = self.webAuthSessionCall
846
+ let expectedPrefix = self.webAuthSessionRedirectPrefix
847
+ let resolveKey = self.webAuthSessionResultKey
787
848
 
788
- // Clean up the stored call
789
- self.openSecureWindowCall = nil
849
+ self.webAuthSessionCall = nil
850
+ self.webAuthSessionRedirectPrefix = nil
851
+
852
+ guard let activeCall = activeCall else {
853
+ return
854
+ }
790
855
 
791
856
  if let error = error {
792
- // Handle error (e.g., user cancelled)
793
- call.reject(error.localizedDescription)
857
+ self.rejectWebAuthSessionCall(activeCall, error: error)
794
858
  return
795
859
  }
796
860
 
797
861
  guard let callbackURL = callbackURL else {
798
- call.reject("No callback URL received")
862
+ activeCall.reject("No callback URL received")
799
863
  return
800
864
  }
801
865
 
802
- if !callbackURL.absoluteString.hasPrefix(redirectUri) {
803
- call.reject("Redirect URI does not match, expected " + redirectUri + " but got " + callbackURL.absoluteString)
866
+ if let expectedPrefix = expectedPrefix, !callbackURL.absoluteString.hasPrefix(expectedPrefix) {
867
+ activeCall.reject(
868
+ "Redirect URI does not match, expected " + expectedPrefix + " but got " + callbackURL.absoluteString
869
+ )
804
870
  return
805
871
  }
806
872
 
807
- // Resolve the call with the callback URL
808
- call.resolve(["redirectedUri": callbackURL.absoluteString])
873
+ activeCall.resolve([resolveKey: callbackURL.absoluteString])
809
874
  }
810
875
 
811
- // Present the session
812
876
  session.presentationContextProvider = self
877
+ session.prefersEphemeralWebBrowserSession = prefersEphemeral
813
878
  session.start()
814
879
  }
815
880
  }
816
881
 
882
+ private func rejectWebAuthSessionCall(_ call: CAPPluginCall, error: Error) {
883
+ let cancelled = isUserCancelledError(error)
884
+ call.reject(error.localizedDescription, cancelled ? SocialLoginPlugin.userCancelledCode : nil, error)
885
+ }
886
+
817
887
  private func decodeJwtClaims(idToken: String) throws -> [String: Any] {
818
888
  let parts = idToken.split(separator: ".")
819
889
  if parts.count < 2 {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/capacitor-social-login",
3
- "version": "8.5.19",
3
+ "version": "8.7.0",
4
4
  "description": "All social logins in one plugin",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -48,7 +48,7 @@
48
48
  "verify": "bun run verify:ios && bun run verify:android && bun run verify:web",
49
49
  "verify:ios": "bash .github/scripts/verify-ios-plugin-build.sh",
50
50
  "verify:android": "cd android && ./gradlew clean build test && cd ..",
51
- "verify:web": "bun run build",
51
+ "verify:web": "bun run build && bun test",
52
52
  "lint": "bun run eslint && bun run prettier -- --check && bun run swiftlint -- lint",
53
53
  "fmt": "bun run eslint -- --fix && bun run prettier -- --write && bun run swiftlint -- --fix --format",
54
54
  "eslint": "eslint",