Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add Google Sign in Example #212

Merged
merged 2 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Examples/Examples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -65,6 +66,7 @@
793E03082B2CED5D00AC7DED /* Contants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Contants.swift; sourceTree = "<group>"; };
793E030A2B2CEDDA00AC7DED /* ActionState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionState.swift; sourceTree = "<group>"; };
793E030C2B2DAB5700AC7DED /* SignInWithApple.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInWithApple.swift; sourceTree = "<group>"; };
7940E3142B36187A0089BEE1 /* GoogleSignIn.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoogleSignIn.swift; sourceTree = "<group>"; };
794EF1212955F26A008C9526 /* AddTodoListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddTodoListView.swift; sourceTree = "<group>"; };
794EF1232955F3DE008C9526 /* TodoListRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TodoListRow.swift; sourceTree = "<group>"; };
7956405D2954ADE00088A06F /* Secrets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Secrets.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -214,6 +216,7 @@
79AF047E2B2CE207008761AD /* AuthWithEmailAndPassword.swift */,
79AF04832B2CE408008761AD /* AuthWithMagicLink.swift */,
793E030C2B2DAB5700AC7DED /* SignInWithApple.swift */,
7940E3142B36187A0089BEE1 /* GoogleSignIn.swift */,
);
path = Auth;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
3 changes: 3 additions & 0 deletions Examples/Examples/Auth/AuthView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Expand All @@ -42,6 +44,7 @@ extension AuthView.Option: View {
case .emailAndPassword: AuthWithEmailAndPassword()
case .magicLink: AuthWithMagicLink()
case .signInWithApple: SignInWithApple()
case .googleSignIn: GoogleSignIn()
}
}
}
Expand Down
40 changes: 40 additions & 0 deletions Examples/Examples/Auth/GoogleSignIn.swift
Original file line number Diff line number Diff line change
@@ -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()
}
18 changes: 14 additions & 4 deletions Sources/PostgREST/PostgrestQueryBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: ",")
))
}
}

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Tests/PostgRESTTests/BuildURLRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down