From b80898c4d227f65fd54a35f8165c5a98fb7ef8cd Mon Sep 17 00:00:00 2001 From: Guilherme Souza Date: Fri, 22 Dec 2023 17:23:50 -0300 Subject: [PATCH] docs: add Google Sign in Example (#212) * docs: add Google Sign in Example * Revert AuthClient to Actor --- Examples/Examples.xcodeproj/project.pbxproj | 4 ++ Examples/Examples/Auth/AuthView.swift | 3 ++ Examples/Examples/Auth/GoogleSignIn.swift | 40 +++++++++++++++++++ Sources/PostgREST/PostgrestQueryBuilder.swift | 18 +++++++-- .../PostgRESTTests/BuildURLRequestTests.swift | 2 +- 5 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 Examples/Examples/Auth/GoogleSignIn.swift diff --git a/Examples/Examples.xcodeproj/project.pbxproj b/Examples/Examples.xcodeproj/project.pbxproj index 1735ccf8..01285046 100644 --- a/Examples/Examples.xcodeproj/project.pbxproj +++ b/Examples/Examples.xcodeproj/project.pbxproj @@ -19,6 +19,7 @@ 793E03092B2CED5D00AC7DED /* Contants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 793E03082B2CED5D00AC7DED /* Contants.swift */; }; 793E030B2B2CEDDA00AC7DED /* ActionState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 793E030A2B2CEDDA00AC7DED /* ActionState.swift */; }; 793E030D2B2DAB5700AC7DED /* SignInWithApple.swift in Sources */ = {isa = PBXBuildFile; fileRef = 793E030C2B2DAB5700AC7DED /* SignInWithApple.swift */; }; + 7940E3152B36187A0089BEE1 /* GoogleSignIn.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7940E3142B36187A0089BEE1 /* GoogleSignIn.swift */; }; 794EF1222955F26A008C9526 /* AddTodoListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 794EF1212955F26A008C9526 /* AddTodoListView.swift */; }; 794EF1242955F3DE008C9526 /* TodoListRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 794EF1232955F3DE008C9526 /* TodoListRow.swift */; }; 7956405E2954ADE00088A06F /* Secrets.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7956405D2954ADE00088A06F /* Secrets.swift */; }; @@ -65,6 +66,7 @@ 793E03082B2CED5D00AC7DED /* Contants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Contants.swift; sourceTree = ""; }; 793E030A2B2CEDDA00AC7DED /* ActionState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionState.swift; sourceTree = ""; }; 793E030C2B2DAB5700AC7DED /* SignInWithApple.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInWithApple.swift; sourceTree = ""; }; + 7940E3142B36187A0089BEE1 /* GoogleSignIn.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoogleSignIn.swift; sourceTree = ""; }; 794EF1212955F26A008C9526 /* AddTodoListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddTodoListView.swift; sourceTree = ""; }; 794EF1232955F3DE008C9526 /* TodoListRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TodoListRow.swift; sourceTree = ""; }; 7956405D2954ADE00088A06F /* Secrets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Secrets.swift; sourceTree = ""; }; @@ -214,6 +216,7 @@ 79AF047E2B2CE207008761AD /* AuthWithEmailAndPassword.swift */, 79AF04832B2CE408008761AD /* AuthWithMagicLink.swift */, 793E030C2B2DAB5700AC7DED /* SignInWithApple.swift */, + 7940E3142B36187A0089BEE1 /* GoogleSignIn.swift */, ); path = Auth; sourceTree = ""; @@ -416,6 +419,7 @@ 795640602954AE140088A06F /* AuthController.swift in Sources */, 79AF047F2B2CE207008761AD /* AuthWithEmailAndPassword.swift in Sources */, 795640622955AD2B0088A06F /* HomeView.swift in Sources */, + 7940E3152B36187A0089BEE1 /* GoogleSignIn.swift in Sources */, 793895CA2954ABFF0044F2B8 /* ExamplesApp.swift in Sources */, 793E030D2B2DAB5700AC7DED /* SignInWithApple.swift in Sources */, 793E030B2B2CEDDA00AC7DED /* ActionState.swift in Sources */, diff --git a/Examples/Examples/Auth/AuthView.swift b/Examples/Examples/Auth/AuthView.swift index 1ba0e1b0..eb5094b0 100644 --- a/Examples/Examples/Auth/AuthView.swift +++ b/Examples/Examples/Auth/AuthView.swift @@ -12,12 +12,14 @@ struct AuthView: View { case emailAndPassword case magicLink case signInWithApple + case googleSignIn var title: String { switch self { case .emailAndPassword: "Auth with Email & Password" case .magicLink: "Auth with Magic Link" case .signInWithApple: "Sign in with Apple" + case .googleSignIn: "Google Sign in" } } } @@ -42,6 +44,7 @@ extension AuthView.Option: View { case .emailAndPassword: AuthWithEmailAndPassword() case .magicLink: AuthWithMagicLink() case .signInWithApple: SignInWithApple() + case .googleSignIn: GoogleSignIn() } } } diff --git a/Examples/Examples/Auth/GoogleSignIn.swift b/Examples/Examples/Auth/GoogleSignIn.swift new file mode 100644 index 00000000..41a23058 --- /dev/null +++ b/Examples/Examples/Auth/GoogleSignIn.swift @@ -0,0 +1,40 @@ +// +// GoogleSignIn.swift +// Examples +// +// Created by Guilherme Souza on 22/12/23. +// + +import SwiftUI + +struct GoogleSignIn: View { + @Environment(\.webAuthenticationSession) var webAuthenticationSession + + var body: some View { + Button("Sign in with Google") { + Task { + await signInWithGoogleButtonTapped() + } + } + } + + private func signInWithGoogleButtonTapped() async { + do { + let url = try await supabase.auth.getOAuthSignInURL( + provider: .google, + redirectTo: Constants.redirectToURL + ) + let urlWithToken = try await webAuthenticationSession.authenticate( + using: url, + callbackURLScheme: Constants.redirectToURL.scheme! + ) + try await supabase.auth.session(from: urlWithToken) + } catch { + print("failed to sign in with Google: \(error)") + } + } +} + +#Preview { + GoogleSignIn() +} diff --git a/Sources/PostgREST/PostgrestQueryBuilder.swift b/Sources/PostgREST/PostgrestQueryBuilder.swift index 1a20263b..ea4045f0 100644 --- a/Sources/PostgREST/PostgrestQueryBuilder.swift +++ b/Sources/PostgREST/PostgrestQueryBuilder.swift @@ -69,10 +69,15 @@ public final class PostgrestQueryBuilder: PostgrestBuilder { if !prefersHeaders.isEmpty { $0.request.headers["Prefer"] = prefersHeaders.joined(separator: ",") } - if let body = $0.request.body, let jsonObject = try JSONSerialization.jsonObject(with: body) as? [[String: Any]] { + if let body = $0.request.body, + let jsonObject = try JSONSerialization.jsonObject(with: body) as? [[String: Any]] + { let allKeys = jsonObject.flatMap(\.keys) let uniqueKeys = Set(allKeys).sorted() - $0.request.query.append(URLQueryItem(name: "columns", value: uniqueKeys.joined(separator: ","))) + $0.request.query.append(URLQueryItem( + name: "columns", + value: uniqueKeys.joined(separator: ",") + )) } } @@ -115,10 +120,15 @@ public final class PostgrestQueryBuilder: PostgrestBuilder { $0.request.headers["Prefer"] = prefersHeaders.joined(separator: ",") } - if let body = $0.request.body, let jsonObject = try JSONSerialization.jsonObject(with: body) as? [[String: Any]] { + if let body = $0.request.body, + let jsonObject = try JSONSerialization.jsonObject(with: body) as? [[String: Any]] + { let allKeys = jsonObject.flatMap(\.keys) let uniqueKeys = Set(allKeys).sorted() - $0.request.query.append(URLQueryItem(name: "columns", value: uniqueKeys.joined(separator: ","))) + $0.request.query.append(URLQueryItem( + name: "columns", + value: uniqueKeys.joined(separator: ",") + )) } } return PostgrestFilterBuilder(self) diff --git a/Tests/PostgRESTTests/BuildURLRequestTests.swift b/Tests/PostgRESTTests/BuildURLRequestTests.swift index 4753ba04..7eb36bc8 100644 --- a/Tests/PostgRESTTests/BuildURLRequestTests.swift +++ b/Tests/PostgRESTTests/BuildURLRequestTests.swift @@ -63,7 +63,7 @@ final class BuildURLRequestTests: XCTestCase { as: .curl, named: runningTestCase.name, record: runningTestCase.record, - file: runningTestCase.file, + file: runningTestCase.file, testName: "testBuildRequest()", line: runningTestCase.line )