Skip to content

Commit b80898c

Browse files
authored
docs: add Google Sign in Example (#212)
* docs: add Google Sign in Example * Revert AuthClient to Actor
1 parent edeb20e commit b80898c

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

Examples/Examples.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
793E03092B2CED5D00AC7DED /* Contants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 793E03082B2CED5D00AC7DED /* Contants.swift */; };
2020
793E030B2B2CEDDA00AC7DED /* ActionState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 793E030A2B2CEDDA00AC7DED /* ActionState.swift */; };
2121
793E030D2B2DAB5700AC7DED /* SignInWithApple.swift in Sources */ = {isa = PBXBuildFile; fileRef = 793E030C2B2DAB5700AC7DED /* SignInWithApple.swift */; };
22+
7940E3152B36187A0089BEE1 /* GoogleSignIn.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7940E3142B36187A0089BEE1 /* GoogleSignIn.swift */; };
2223
794EF1222955F26A008C9526 /* AddTodoListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 794EF1212955F26A008C9526 /* AddTodoListView.swift */; };
2324
794EF1242955F3DE008C9526 /* TodoListRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 794EF1232955F3DE008C9526 /* TodoListRow.swift */; };
2425
7956405E2954ADE00088A06F /* Secrets.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7956405D2954ADE00088A06F /* Secrets.swift */; };
@@ -65,6 +66,7 @@
6566
793E03082B2CED5D00AC7DED /* Contants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Contants.swift; sourceTree = "<group>"; };
6667
793E030A2B2CEDDA00AC7DED /* ActionState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionState.swift; sourceTree = "<group>"; };
6768
793E030C2B2DAB5700AC7DED /* SignInWithApple.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInWithApple.swift; sourceTree = "<group>"; };
69+
7940E3142B36187A0089BEE1 /* GoogleSignIn.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoogleSignIn.swift; sourceTree = "<group>"; };
6870
794EF1212955F26A008C9526 /* AddTodoListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddTodoListView.swift; sourceTree = "<group>"; };
6971
794EF1232955F3DE008C9526 /* TodoListRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TodoListRow.swift; sourceTree = "<group>"; };
7072
7956405D2954ADE00088A06F /* Secrets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Secrets.swift; sourceTree = "<group>"; };
@@ -214,6 +216,7 @@
214216
79AF047E2B2CE207008761AD /* AuthWithEmailAndPassword.swift */,
215217
79AF04832B2CE408008761AD /* AuthWithMagicLink.swift */,
216218
793E030C2B2DAB5700AC7DED /* SignInWithApple.swift */,
219+
7940E3142B36187A0089BEE1 /* GoogleSignIn.swift */,
217220
);
218221
path = Auth;
219222
sourceTree = "<group>";
@@ -416,6 +419,7 @@
416419
795640602954AE140088A06F /* AuthController.swift in Sources */,
417420
79AF047F2B2CE207008761AD /* AuthWithEmailAndPassword.swift in Sources */,
418421
795640622955AD2B0088A06F /* HomeView.swift in Sources */,
422+
7940E3152B36187A0089BEE1 /* GoogleSignIn.swift in Sources */,
419423
793895CA2954ABFF0044F2B8 /* ExamplesApp.swift in Sources */,
420424
793E030D2B2DAB5700AC7DED /* SignInWithApple.swift in Sources */,
421425
793E030B2B2CEDDA00AC7DED /* ActionState.swift in Sources */,

Examples/Examples/Auth/AuthView.swift

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

1617
var title: String {
1718
switch self {
1819
case .emailAndPassword: "Auth with Email & Password"
1920
case .magicLink: "Auth with Magic Link"
2021
case .signInWithApple: "Sign in with Apple"
22+
case .googleSignIn: "Google Sign in"
2123
}
2224
}
2325
}
@@ -42,6 +44,7 @@ extension AuthView.Option: View {
4244
case .emailAndPassword: AuthWithEmailAndPassword()
4345
case .magicLink: AuthWithMagicLink()
4446
case .signInWithApple: SignInWithApple()
47+
case .googleSignIn: GoogleSignIn()
4548
}
4649
}
4750
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// GoogleSignIn.swift
3+
// Examples
4+
//
5+
// Created by Guilherme Souza on 22/12/23.
6+
//
7+
8+
import SwiftUI
9+
10+
struct GoogleSignIn: View {
11+
@Environment(\.webAuthenticationSession) var webAuthenticationSession
12+
13+
var body: some View {
14+
Button("Sign in with Google") {
15+
Task {
16+
await signInWithGoogleButtonTapped()
17+
}
18+
}
19+
}
20+
21+
private func signInWithGoogleButtonTapped() async {
22+
do {
23+
let url = try await supabase.auth.getOAuthSignInURL(
24+
provider: .google,
25+
redirectTo: Constants.redirectToURL
26+
)
27+
let urlWithToken = try await webAuthenticationSession.authenticate(
28+
using: url,
29+
callbackURLScheme: Constants.redirectToURL.scheme!
30+
)
31+
try await supabase.auth.session(from: urlWithToken)
32+
} catch {
33+
print("failed to sign in with Google: \(error)")
34+
}
35+
}
36+
}
37+
38+
#Preview {
39+
GoogleSignIn()
40+
}

Sources/PostgREST/PostgrestQueryBuilder.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,15 @@ public final class PostgrestQueryBuilder: PostgrestBuilder {
6969
if !prefersHeaders.isEmpty {
7070
$0.request.headers["Prefer"] = prefersHeaders.joined(separator: ",")
7171
}
72-
if let body = $0.request.body, let jsonObject = try JSONSerialization.jsonObject(with: body) as? [[String: Any]] {
72+
if let body = $0.request.body,
73+
let jsonObject = try JSONSerialization.jsonObject(with: body) as? [[String: Any]]
74+
{
7375
let allKeys = jsonObject.flatMap(\.keys)
7476
let uniqueKeys = Set(allKeys).sorted()
75-
$0.request.query.append(URLQueryItem(name: "columns", value: uniqueKeys.joined(separator: ",")))
77+
$0.request.query.append(URLQueryItem(
78+
name: "columns",
79+
value: uniqueKeys.joined(separator: ",")
80+
))
7681
}
7782
}
7883

@@ -115,10 +120,15 @@ public final class PostgrestQueryBuilder: PostgrestBuilder {
115120
$0.request.headers["Prefer"] = prefersHeaders.joined(separator: ",")
116121
}
117122

118-
if let body = $0.request.body, let jsonObject = try JSONSerialization.jsonObject(with: body) as? [[String: Any]] {
123+
if let body = $0.request.body,
124+
let jsonObject = try JSONSerialization.jsonObject(with: body) as? [[String: Any]]
125+
{
119126
let allKeys = jsonObject.flatMap(\.keys)
120127
let uniqueKeys = Set(allKeys).sorted()
121-
$0.request.query.append(URLQueryItem(name: "columns", value: uniqueKeys.joined(separator: ",")))
128+
$0.request.query.append(URLQueryItem(
129+
name: "columns",
130+
value: uniqueKeys.joined(separator: ",")
131+
))
122132
}
123133
}
124134
return PostgrestFilterBuilder(self)

Tests/PostgRESTTests/BuildURLRequestTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ final class BuildURLRequestTests: XCTestCase {
6363
as: .curl,
6464
named: runningTestCase.name,
6565
record: runningTestCase.record,
66-
file: runningTestCase.file,
66+
file: runningTestCase.file,
6767
testName: "testBuildRequest()",
6868
line: runningTestCase.line
6969
)

0 commit comments

Comments
 (0)