Skip to content

Commit

Permalink
Merge pull request #6 from SwiftfulThinking/SwiftfulThinking-patch-1
Browse files Browse the repository at this point in the history
Swiftful thinking patch 1
  • Loading branch information
SwiftfulThinking authored Apr 23, 2024
2 parents 46209b2 + 9b6cace commit eb93995
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 86 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let package = Package(
.product(name: "GoogleSignInSwift", package: "GoogleSignIn-iOS")
],
resources: [
.process("Assets")
.process("Support/Assets")
]
),
.testTarget(
Expand Down
148 changes: 123 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Convenience methods to manage Firebase Authentication in Swift projects.

- ✅ Sign In With Apple
- ✅ Sign In With Google
- ✅ Sign In With Phone

```swift
Task {
Expand All @@ -22,70 +23,166 @@ Task {
}
```

## Usage
Sample project: https://github.com/SwiftfulThinking/SwiftfulFirebasePackagesExample

Import the package to your project.
## Setup

<details>
<summary> Details (Click to expand) </summary>
<br>
### 1. Import the package to your project.
* File -> Swift Packages -> Add Package Dependency
* Add URL for this repository: https://github.com/SwiftfulThinking/SwiftfulFirebaseAuth.git

#### Import the package to your file.
### 2. Import the package to your file.
```swift
import SwiftfulFirebaseAuth
```

#### Create one instance of AuthManager for your application.
### 3. Create one instance of AuthManager for your application.
```swift
let authManager = AuthManager(configuration: .firebase)

// Use Mock configuration to avoid running Firebase while developing (ex. for SwiftUI Previews).
let authManager = AuthManager(configuration: .mock)
```

### 4. Configure your Firebase project.
Add the Firebase SDK to your application and configure() the SDK on launch.

#### Use Mock configuration to avoid running Firebase while developing (ex. for SwiftUI Previews).
```swift
let authManager = AuthManager(configuration: .mock)
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
```
```swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}
```

#### Configure your Firebase project.
Add the Firebase SDK to your application and configure() the SDK on launch.
</details>

## Sign In With Apple

##### Sign In With Apple
1. Enable Apple as a Sign-In Method in Firebase Authentication console.
2. Add Sign in with Apple Signing Capability to your Xcode project.
<details>
<summary> Details (Click to expand) </summary>
<br>

https://firebase.google.com/docs/auth/ios/apple
Firebase docs: https://firebase.google.com/docs/auth/ios/apple

### 1. Enable Apple as a Sign-In Method in Firebase Authentication console.
* Firebase Console -> Authentication -> Sign-in method -> Add new provider

### 2. Add Sign in with Apple Signing Capability to your Xcode project.
* Xcode Project Navigator -> Target -> Signing & Capabilities -> + Capability -> Sign in with Apple (requires Apple Developer Account)

### 3. Add Apple Button (optional)
```swift
try await authManager.signInApple()
SignInWithAppleButtonView()
.frame(height: 50)
```

### 4. Sign in

```swift
SignInWithAppleButtonView(
type: .signUp,
style: .black,
cornerRadius: 10
)
.frame(height: 50)
try await authManager.signInApple()
```
</details>


## Sign In With Google

##### Sign In With Google
1. Enable Apple as a Sign-In Method in Firebase Authentication console & update the info.plist file.
2. Add custom URL scheme (URL Types -> REVERSED_CLIENT_ID)
<details>
<summary> Details (Click to expand) </summary>
<br>

https://firebase.google.com/docs/auth/ios/google-signin
Firebase docs: https://firebase.google.com/docs/auth/ios/google-signin

### 1. Enable Google as a Sign-In Method in Firebase Authentication console.
* Firebase Console -> Authentication -> Sign-in method -> Add new provider

### 2. Update you app's the info.plist file.
* Firebase Console -> Project Settings -> Your apps -> GoogleService-Info.plist

### 3. Add custom URL scheme (URL Types -> REVERSED_CLIENT_ID)
* GoogleService-Info.plist -> REVERSED_CLIENT_ID
* Xcode Project Navigator -> Target -> Info -> URL Types -> add REVERSED_CLIENT_ID as URL Schemes value

### 4. Add Google Button (optional)
```swift
SignInWithGoogleButtonView()
.frame(height: 50)
```

### 5. Sign in
```swift
let clientId = FirebaseApp.app()?.options.clientId
try await authManager.signInGoogle(GIDClientID: clientId)
```

</details>

## Sign In With Phone

<details>
<summary> Details (Click to expand) </summary>
<br>

Firebase docs: https://firebase.google.com/docs/auth/ios/phone-auth

### 1. Enable Phone Number as a Sign-In Method in Firebase Authentication console.
* Firebase Console -> Authentication -> Sign-in method -> Add new provider

### 2. Enable APNs notifications (silent push notifications).
* Create an APNs Authentication Key in [Apple Developer Member Center](https://developer.apple.com/membercenter/index.action) (requires Apple Developer Account)
* Certificates, Identifiers & Profiles -> New Key for Apple Push Notifications service (APNs) -> download .p8 file

### 3. Upload APNs key to Firebase.
* Firebase Console -> Project Settings -> Cloud Messaging -> APNs Authentication Key

### 4. Enable reCAPTCHA verification (optional?).
* Firebase Console -> Project Settings -> Encoded App ID
* Xcode Project Navigator -> Target -> Info -> URL Types -> add Encoded App ID as URL Schemes value

### 5. Get the user's phone number
* This SDK does NOT format phone numbers or provide UI for this. You must provide a string in the correct format.
* Phone numbers have to be correctly formatted, such as "+1 650-555-3434" for US numbers.
* See [Firebase Docs](https://firebase.google.com/docs/auth/ios/phone-auth) for details about phone number implementation
* Possible resources for phone number formatting:
- https://stackoverflow.com/questions/32364055/formatting-phone-number-in-swift
- https://github.com/iziz/libPhoneNumber-iOS
- https://github.com/MojtabaHs/iPhoneNumberField

### 6. Add Google Button (optional)

```swift
SignInWithGoogleButtonView(
SignInWithPhoneButtonView(
type: .signUp,
style: .black,
cornerRadius: 10
)
.frame(height: 50)
```

### 7. Send verification code to user's phone.

```swift
try await authManager.signInPhone_Start(phoneNumber: phoneNumber)
```

### 8. Verify code and sign in
```swift
try await authManager.signInPhone_Verify(code: code)
```

</details>

## Already Signed In

<details>
<summary> Details (Click to expand) </summary>
<br>

#### Synchronously get user's authentication info.
```swift
Expand All @@ -106,7 +203,6 @@ Task {
}
```


#### Sign out or delete user's authentication.
```swift
try authManager.signOut()
Expand All @@ -115,5 +211,7 @@ try authManager.signOut()
try await authManager.deleteAuthentication()
```

</details>

## Want to contribute?
Open a PR! New Sign-In Methods must use Swift Concurrency (async/await).
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// ASAuthorizationAppleIDButton.swift
//
//
// Created by Nick Sarno on 4/22/24.
//

import Foundation
import SwiftUI
import AuthenticationServices

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
}
}
}

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"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ private struct SignInWithAppleButtonViewRepresentable: UIViewRepresentable {
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)
}
Expand Down
Loading

0 comments on commit eb93995

Please sign in to comment.