Skip to content

Commit 5a3e4c2

Browse files
authored
docs: add example for GoogleSignIn using GIDSignIn SDK (#254)
* docs: add example for GoogleSignIn using GIDSignIn SDK * Add GIDServerClientID key to Info.plist
1 parent c06aa18 commit 5a3e4c2

File tree

8 files changed

+161
-10
lines changed

8 files changed

+161
-10
lines changed

Examples/Examples.xcodeproj/project.pbxproj

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
793E03092B2CED5D00AC7DED /* Contants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 793E03082B2CED5D00AC7DED /* Contants.swift */; };
1515
793E030B2B2CEDDA00AC7DED /* ActionState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 793E030A2B2CEDDA00AC7DED /* ActionState.swift */; };
1616
793E030D2B2DAB5700AC7DED /* SignInWithApple.swift in Sources */ = {isa = PBXBuildFile; fileRef = 793E030C2B2DAB5700AC7DED /* SignInWithApple.swift */; };
17-
7940E3152B36187A0089BEE1 /* GoogleSignIn.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7940E3142B36187A0089BEE1 /* GoogleSignIn.swift */; };
17+
7940E3152B36187A0089BEE1 /* GoogleSignInWithWebFlow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7940E3142B36187A0089BEE1 /* GoogleSignInWithWebFlow.swift */; };
1818
794EF1222955F26A008C9526 /* AddTodoListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 794EF1212955F26A008C9526 /* AddTodoListView.swift */; };
1919
794EF1242955F3DE008C9526 /* TodoListRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 794EF1232955F3DE008C9526 /* TodoListRow.swift */; };
2020
7956405E2954ADE00088A06F /* Secrets.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7956405D2954ADE00088A06F /* Secrets.swift */; };
@@ -48,6 +48,10 @@
4848
79D884D92B3C18E90009EA4A /* Supabase in Frameworks */ = {isa = PBXBuildFile; productRef = 79D884D82B3C18E90009EA4A /* Supabase */; };
4949
79D884DB2B3C191F0009EA4A /* ChannelListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79D884DA2B3C191F0009EA4A /* ChannelListView.swift */; };
5050
79D884DD2B3C19320009EA4A /* MessagesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79D884DC2B3C19320009EA4A /* MessagesView.swift */; };
51+
79E2B5552B9788BF0042CD21 /* GoogleSignInSDKFlow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79E2B5542B9788BF0042CD21 /* GoogleSignInSDKFlow.swift */; };
52+
79E2B5582B97890F0042CD21 /* GoogleSignIn in Frameworks */ = {isa = PBXBuildFile; productRef = 79E2B5572B97890F0042CD21 /* GoogleSignIn */; };
53+
79E2B55A2B97890F0042CD21 /* GoogleSignInSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 79E2B5592B97890F0042CD21 /* GoogleSignInSwift */; };
54+
79E2B55C2B97A2310042CD21 /* UIApplicationExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79E2B55B2B97A2310042CD21 /* UIApplicationExtensions.swift */; };
5155
79FEFFAF2B07873600D36347 /* UserManagementApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79FEFFAE2B07873600D36347 /* UserManagementApp.swift */; };
5256
79FEFFB12B07873600D36347 /* AppView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79FEFFB02B07873600D36347 /* AppView.swift */; };
5357
79FEFFB32B07873700D36347 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 79FEFFB22B07873700D36347 /* Assets.xcassets */; };
@@ -71,7 +75,7 @@
7175
793E03082B2CED5D00AC7DED /* Contants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Contants.swift; sourceTree = "<group>"; };
7276
793E030A2B2CEDDA00AC7DED /* ActionState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionState.swift; sourceTree = "<group>"; };
7377
793E030C2B2DAB5700AC7DED /* SignInWithApple.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInWithApple.swift; sourceTree = "<group>"; };
74-
7940E3142B36187A0089BEE1 /* GoogleSignIn.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoogleSignIn.swift; sourceTree = "<group>"; };
78+
7940E3142B36187A0089BEE1 /* GoogleSignInWithWebFlow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoogleSignInWithWebFlow.swift; sourceTree = "<group>"; };
7579
794EF1212955F26A008C9526 /* AddTodoListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddTodoListView.swift; sourceTree = "<group>"; };
7680
794EF1232955F3DE008C9526 /* TodoListRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TodoListRow.swift; sourceTree = "<group>"; };
7781
7956405D2954ADE00088A06F /* Secrets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Secrets.swift; sourceTree = "<group>"; };
@@ -103,6 +107,8 @@
103107
79D884D62B3C18DB0009EA4A /* Supabase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Supabase.swift; sourceTree = "<group>"; };
104108
79D884DA2B3C191F0009EA4A /* ChannelListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelListView.swift; sourceTree = "<group>"; };
105109
79D884DC2B3C19320009EA4A /* MessagesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessagesView.swift; sourceTree = "<group>"; };
110+
79E2B5542B9788BF0042CD21 /* GoogleSignInSDKFlow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoogleSignInSDKFlow.swift; sourceTree = "<group>"; };
111+
79E2B55B2B97A2310042CD21 /* UIApplicationExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIApplicationExtensions.swift; sourceTree = "<group>"; };
106112
79FEFFAC2B07873600D36347 /* UserManagement.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = UserManagement.app; sourceTree = BUILT_PRODUCTS_DIR; };
107113
79FEFFAE2B07873600D36347 /* UserManagementApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserManagementApp.swift; sourceTree = "<group>"; };
108114
79FEFFB02B07873600D36347 /* AppView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppView.swift; sourceTree = "<group>"; };
@@ -123,8 +129,10 @@
123129
isa = PBXFrameworksBuildPhase;
124130
buildActionMask = 2147483647;
125131
files = (
132+
79E2B5582B97890F0042CD21 /* GoogleSignIn in Frameworks */,
126133
795640702955B5190088A06F /* IdentifiedCollections in Frameworks */,
127134
7956406D2955B3500088A06F /* SwiftUINavigation in Frameworks */,
135+
79E2B55A2B97890F0042CD21 /* GoogleSignInSwift in Frameworks */,
128136
7962989D2AEBC6F9000AA957 /* SVGView in Frameworks */,
129137
79719ECE2ADF26C400737804 /* Supabase in Frameworks */,
130138
);
@@ -192,6 +200,7 @@
192200
79AF04852B2CE586008761AD /* Debug.swift */,
193201
793E03082B2CED5D00AC7DED /* Contants.swift */,
194202
793E030A2B2CEDDA00AC7DED /* ActionState.swift */,
203+
79E2B55B2B97A2310042CD21 /* UIApplicationExtensions.swift */,
195204
);
196205
path = Examples;
197206
sourceTree = "<group>";
@@ -219,7 +228,8 @@
219228
79AF047E2B2CE207008761AD /* AuthWithEmailAndPassword.swift */,
220229
79AF04832B2CE408008761AD /* AuthWithMagicLink.swift */,
221230
793E030C2B2DAB5700AC7DED /* SignInWithApple.swift */,
222-
7940E3142B36187A0089BEE1 /* GoogleSignIn.swift */,
231+
7940E3142B36187A0089BEE1 /* GoogleSignInWithWebFlow.swift */,
232+
79E2B5542B9788BF0042CD21 /* GoogleSignInSDKFlow.swift */,
223233
);
224234
path = Auth;
225235
sourceTree = "<group>";
@@ -303,6 +313,8 @@
303313
7956406F2955B5190088A06F /* IdentifiedCollections */,
304314
79719ECD2ADF26C400737804 /* Supabase */,
305315
7962989C2AEBC6F9000AA957 /* SVGView */,
316+
79E2B5572B97890F0042CD21 /* GoogleSignIn */,
317+
79E2B5592B97890F0042CD21 /* GoogleSignInSwift */,
306318
);
307319
productName = Examples;
308320
productReference = 793895C62954ABFF0044F2B8 /* Examples.app */;
@@ -383,6 +395,7 @@
383395
7956406B2955B3500088A06F /* XCRemoteSwiftPackageReference "swiftui-navigation" */,
384396
7956406E2955B5190088A06F /* XCRemoteSwiftPackageReference "swift-identified-collections" */,
385397
7962989B2AEBC6F9000AA957 /* XCRemoteSwiftPackageReference "SVGView" */,
398+
79E2B5562B97890F0042CD21 /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */,
386399
);
387400
productRefGroup = 793895C72954ABFF0044F2B8 /* Products */;
388401
projectDirPath = "";
@@ -433,19 +446,21 @@
433446
796298992AEBBA77000AA957 /* MFAFlow.swift in Sources */,
434447
79AF04862B2CE586008761AD /* Debug.swift in Sources */,
435448
79AF04842B2CE408008761AD /* AuthWithMagicLink.swift in Sources */,
449+
79E2B5552B9788BF0042CD21 /* GoogleSignInSDKFlow.swift in Sources */,
436450
793E03092B2CED5D00AC7DED /* Contants.swift in Sources */,
437451
793895CC2954ABFF0044F2B8 /* RootView.swift in Sources */,
438452
7956406A2955AFBD0088A06F /* ErrorText.swift in Sources */,
439453
79AF04812B2CE261008761AD /* AuthView.swift in Sources */,
440454
794EF1242955F3DE008C9526 /* TodoListRow.swift in Sources */,
455+
79E2B55C2B97A2310042CD21 /* UIApplicationExtensions.swift in Sources */,
441456
794EF1222955F26A008C9526 /* AddTodoListView.swift in Sources */,
442457
7956405E2954ADE00088A06F /* Secrets.swift in Sources */,
443458
795640682955AEB30088A06F /* Models.swift in Sources */,
444459
795640662955AE9C0088A06F /* TodoListView.swift in Sources */,
445460
795640602954AE140088A06F /* AuthController.swift in Sources */,
446461
79AF047F2B2CE207008761AD /* AuthWithEmailAndPassword.swift in Sources */,
447462
795640622955AD2B0088A06F /* HomeView.swift in Sources */,
448-
7940E3152B36187A0089BEE1 /* GoogleSignIn.swift in Sources */,
463+
7940E3152B36187A0089BEE1 /* GoogleSignInWithWebFlow.swift in Sources */,
449464
793895CA2954ABFF0044F2B8 /* ExamplesApp.swift in Sources */,
450465
793E030D2B2DAB5700AC7DED /* SignInWithApple.swift in Sources */,
451466
793E030B2B2CEDDA00AC7DED /* ActionState.swift in Sources */,
@@ -896,6 +911,14 @@
896911
minimumVersion = 1.0.6;
897912
};
898913
};
914+
79E2B5562B97890F0042CD21 /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */ = {
915+
isa = XCRemoteSwiftPackageReference;
916+
repositoryURL = "https://github.com/google/GoogleSignIn-iOS";
917+
requirement = {
918+
kind = upToNextMajorVersion;
919+
minimumVersion = 7.0.0;
920+
};
921+
};
899922
/* End XCRemoteSwiftPackageReference section */
900923

901924
/* Begin XCSwiftPackageProductDependency section */
@@ -927,6 +950,16 @@
927950
isa = XCSwiftPackageProductDependency;
928951
productName = Supabase;
929952
};
953+
79E2B5572B97890F0042CD21 /* GoogleSignIn */ = {
954+
isa = XCSwiftPackageProductDependency;
955+
package = 79E2B5562B97890F0042CD21 /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */;
956+
productName = GoogleSignIn;
957+
};
958+
79E2B5592B97890F0042CD21 /* GoogleSignInSwift */ = {
959+
isa = XCSwiftPackageProductDependency;
960+
package = 79E2B5562B97890F0042CD21 /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */;
961+
productName = GoogleSignInSwift;
962+
};
930963
79FEFFBB2B07874000D36347 /* Supabase */ = {
931964
isa = XCSwiftPackageProductDependency;
932965
productName = Supabase;

Examples/Examples/Auth/AuthView.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ struct AuthView: View {
1212
case emailAndPassword
1313
case magicLink
1414
case signInWithApple
15-
case googleSignIn
15+
case googleSignInWebFlow
16+
case googleSignInSDKFlow
1617

1718
var title: String {
1819
switch self {
1920
case .emailAndPassword: "Auth with Email & Password"
2021
case .magicLink: "Auth with Magic Link"
2122
case .signInWithApple: "Sign in with Apple"
22-
case .googleSignIn: "Google Sign in"
23+
case .googleSignInWebFlow: "Google Sign in (Web Flow)"
24+
case .googleSignInSDKFlow: "Google Sign in (GIDSignIn SDK Flow)"
2325
}
2426
}
2527
}
@@ -44,7 +46,8 @@ extension AuthView.Option: View {
4446
case .emailAndPassword: AuthWithEmailAndPassword()
4547
case .magicLink: AuthWithMagicLink()
4648
case .signInWithApple: SignInWithApple()
47-
case .googleSignIn: GoogleSignIn()
49+
case .googleSignInWebFlow: GoogleSignInWithWebFlow()
50+
case .googleSignInSDKFlow: GoogleSignInSDKFlow()
4851
}
4952
}
5053
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// GoogleSignInSDKFlow.swift
3+
// Examples
4+
//
5+
// Created by Guilherme Souza on 05/03/24.
6+
//
7+
8+
import SwiftUI
9+
@preconcurrency import GoogleSignIn
10+
import GoogleSignInSwift
11+
import Supabase
12+
13+
@MainActor
14+
struct GoogleSignInSDKFlow: View {
15+
var body: some View {
16+
GoogleSignInButton(action: handleSignIn)
17+
}
18+
19+
func handleSignIn() {
20+
Task {
21+
do {
22+
let result = try await GIDSignIn.sharedInstance.signIn(withPresenting: rootViewController)
23+
24+
guard let idToken = result.user.idToken?.tokenString else {
25+
debug("No 'idToken' returned by GIDSignIn call.")
26+
return
27+
}
28+
29+
try await supabase.auth.signInWithIdToken(
30+
credentials: OpenIDConnectCredentials(
31+
provider: .google,
32+
idToken: idToken
33+
)
34+
)
35+
} catch {
36+
debug("GoogleSignIn failed: \(error)")
37+
}
38+
}
39+
}
40+
41+
var rootViewController: UIViewController {
42+
UIApplication.shared.firstKeyWindow?.rootViewController ?? UIViewController()
43+
}
44+
}
45+
46+
#Preview {
47+
GoogleSignInSDKFlow()
48+
}

Examples/Examples/Auth/GoogleSignIn.swift renamed to Examples/Examples/Auth/GoogleSignInWithWebFlow.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// GoogleSignIn.swift
2+
// GoogleSignInWithWebFlow.swift
33
// Examples
44
//
55
// Created by Guilherme Souza on 22/12/23.
@@ -8,7 +8,7 @@
88
import AuthenticationServices
99
import SwiftUI
1010

11-
struct GoogleSignIn: View {
11+
struct GoogleSignInWithWebFlow: View {
1212
@Environment(\.webAuthenticationSession) var webAuthenticationSession
1313

1414
var body: some View {
@@ -37,5 +37,5 @@ struct GoogleSignIn: View {
3737
}
3838

3939
#Preview {
40-
GoogleSignIn()
40+
GoogleSignInWithWebFlow()
4141
}

Examples/Examples/ExamplesApp.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import Supabase
99
import SwiftUI
10+
import GoogleSignIn
1011

1112
@main
1213
struct ExamplesApp: App {

Examples/Examples/Info.plist

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5+
<key>GIDClientID</key>
6+
<string>YOUR_IOS_CLIENT_ID</string>
7+
<key>GIDServerClientID</key>
8+
<string>YOUR_SERVER_CLIENT_ID</string>
59
<key>CFBundleURLTypes</key>
610
<array>
711
<dict>
@@ -12,6 +16,14 @@
1216
<string>com.supabase.swift-examples</string>
1317
</array>
1418
</dict>
19+
<dict>
20+
<key>CFBundleTypeRole</key>
21+
<string>Editor</string>
22+
<key>CFBundleURLSchemes</key>
23+
<array>
24+
<string>YOUR_DOT_REVERSED_IOS_CLIENT_ID</string>
25+
</array>
26+
</dict>
1527
</array>
1628
</dict>
1729
</plist>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// UIApplicationExtensions.swift
3+
// Examples
4+
//
5+
// Created by Guilherme Souza on 05/03/24.
6+
//
7+
8+
import UIKit
9+
10+
extension UIApplication {
11+
var firstKeyWindow: UIWindow? {
12+
UIApplication.shared
13+
.connectedScenes
14+
.compactMap { $0 as? UIWindowScene }
15+
.filter { $0.activationState == .foregroundActive }
16+
.first?.keyWindow
17+
}
18+
}

supabase-swift.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)