Skip to content

Commit

Permalink
Merge pull request #2 from SwiftfulThinking/google
Browse files Browse the repository at this point in the history
Google
  • Loading branch information
SwiftfulThinking authored Nov 7, 2023
2 parents bca85c5 + e609c6b commit 732632b
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 11 deletions.
6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ let package = Package(
.product(name: "FirebaseAuth", package: "firebase-ios-sdk"),
.product(name: "GoogleSignIn", package: "GoogleSignIn-iOS"),
.product(name: "GoogleSignInSwift", package: "GoogleSignIn-iOS")
]),
],
resources: [
.process("Assets")
]
),
.testTarget(
name: "SwiftfulFirebaseAuthTests",
dependencies: ["SwiftfulFirebaseAuth"]),
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftfulFirebaseAuth/Core/AuthManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public final class AuthManager {
}
}

public func signInGoogle() async throws -> (user: UserAuthInfo, isNewUser: Bool) {
try await provider.authenticateUser_Google()
public func signInGoogle(GIDClientID: String) async throws -> (user: UserAuthInfo, isNewUser: Bool) {
try await provider.authenticateUser_Google(GIDClientID: GIDClientID)
}

public func signInApple() async throws -> (user: UserAuthInfo, isNewUser: Bool) {
Expand Down
21 changes: 18 additions & 3 deletions Sources/SwiftfulFirebaseAuth/Core/SignInWithAppleButtonView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,22 @@ private struct SignInWithAppleButtonViewRepresentable: UIViewRepresentable {
}

#Preview("SignInWithAppleButtonView") {
SignInWithAppleButtonView(cornerRadius: 30)
.frame(height: 60)
.padding()
ZStack {
Color.black

VStack(spacing: 4) {
SignInWithAppleButtonView(
type: .signIn,
style: .white, cornerRadius: 30)
.frame(height: 50)
.background(Color.red)

SignInWithGoogleButtonView(
type: .signIn,
style: .white, cornerRadius: 30)
.frame(height: 50)
.background(Color.red)
}
.padding(40)
}
}
126 changes: 126 additions & 0 deletions Sources/SwiftfulFirebaseAuth/Core/SignInWithGoogleButtonView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
//
// SignInWithGoogleButtonView.swift
//
//
// Created by Nicholas Sarno on 11/6/23.
//

import Foundation
import SwiftUI
import AuthenticationServices

fileprivate extension ASAuthorizationAppleIDButton.Style {
var backgroundColor: Color {
switch self {
case .white:
return .white
case .whiteOutline:
return .white
default:
return .black
}
}

var foregroundColor: Color {
switch self {
case .white:
return .black
case .whiteOutline:
return .black
default:
return .white
}
}

var borderColor: Color {
switch self {
case .white:
return .white
case .whiteOutline:
return .black
default:
return .black
}
}
}

fileprivate extension ASAuthorizationAppleIDButton.ButtonType {
var buttonText: String {
switch self {
case .signIn:
return "Sign in with"
case .continue:
return "Continue with"
case .signUp:
return "Sign up with"
default:
return "Sign in with"
}
}
}

public struct SignInWithGoogleButtonView: View {

private var backgroundColor: Color
private var foregroundColor: Color
private var borderColor: Color
private var buttonText: String
private var cornerRadius: CGFloat

public init(
type: ASAuthorizationAppleIDButton.ButtonType = .signIn,
style: ASAuthorizationAppleIDButton.Style = .black,
cornerRadius: CGFloat = 10
) {
self.cornerRadius = cornerRadius
self.backgroundColor = style.backgroundColor
self.foregroundColor = style.foregroundColor
self.borderColor = style.borderColor
self.buttonText = type.buttonText
}

public init(
type: ASAuthorizationAppleIDButton.ButtonType = .signIn,
backgroundColor: Color = .black,
borderColor: Color = .black,
foregroundColor: Color = .white,
cornerRadius: CGFloat = 10
) {
self.cornerRadius = cornerRadius
self.backgroundColor = backgroundColor
self.borderColor = borderColor
self.foregroundColor = foregroundColor
self.buttonText = type.buttonText
}

public var body: some View {
ZStack {
RoundedRectangle(cornerRadius: cornerRadius)
.fill(borderColor)

RoundedRectangle(cornerRadius: cornerRadius)
.fill(backgroundColor)
.padding(0.8)

HStack(spacing: 6) {
Image("GoogleIcon", bundle: .module)
.resizable()
.scaledToFit()
.frame(width: 16, height: 16)

Text("\(buttonText) Google")
.font(.system(size: 20))
.fontWeight(.medium)
}
.foregroundColor(foregroundColor)
}
.padding(.vertical, 1)
.disabled(true)
}
}

#Preview("SignInWithGoogleButtonView") {
SignInWithGoogleButtonView()
.frame(height: 60)
.padding()
}
7 changes: 6 additions & 1 deletion Sources/SwiftfulFirebaseAuth/Helpers/SignInWithGoogle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@ struct GoogleSignInResult {
}

final class SignInWithGoogleHelper {

init(GIDClientID: String) {
let config = GIDConfiguration(clientID: GIDClientID)
GIDSignIn.sharedInstance.configuration = config
}

@MainActor
func signIn(viewController: UIViewController? = nil) async throws -> GoogleSignInResult {
guard let topViewController = viewController ?? UIApplication.topViewController() else {
throw GoogleSignInError.noViewController
}

let gidSignInResult = try await GIDSignIn.sharedInstance.signIn(withPresenting: topViewController)

guard let result = GoogleSignInResult(result: gidSignInResult) else {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftfulFirebaseAuth/Providers/AuthProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import FirebaseAuth
public protocol AuthProvider {
func getAuthenticatedUser() -> UserAuthInfo?
@MainActor func authenticationDidChangeStream() -> AsyncStream<UserAuthInfo?>
@MainActor func authenticateUser_Google() async throws -> (user: UserAuthInfo, isNewUser: Bool)
@MainActor func authenticateUser_Google(GIDClientID: String) async throws -> (user: UserAuthInfo, isNewUser: Bool)
@MainActor func authenticateUser_Apple() async throws -> (user: UserAuthInfo, isNewUser: Bool)
func signOut() throws
func deleteAccount() async throws
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ final class FirebaseAuthProvider: AuthProvider {
}

@MainActor
func authenticateUser_Google() async throws -> (user: UserAuthInfo, isNewUser: Bool) {
let helper = SignInWithGoogleHelper()
func authenticateUser_Google(GIDClientID: String) async throws -> (user: UserAuthInfo, isNewUser: Bool) {
let helper = SignInWithGoogleHelper(GIDClientID: GIDClientID)

// Sign in to Google account
let googleResponse = try await helper.signIn()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final class MockAuthProvider: AuthProvider {
}
}

func authenticateUser_Google() async throws -> (user: UserAuthInfo, isNewUser: Bool) {
func authenticateUser_Google(GIDClientID: String) async throws -> (user: UserAuthInfo, isNewUser: Bool) {
try? await Task.sleep(nanoseconds: 1_000_000_000)
return signInMockUser()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"images" : [
{
"filename" : "GoogleIcon.png",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"template-rendering-intent" : "template"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 732632b

Please sign in to comment.