Skip to content

Commit

Permalink
New Auth Flow with Firebase Functions Submodule (#92)
Browse files Browse the repository at this point in the history
# New Auth Flow with Firebase Functions Submodule

## ♻️ Current situation & Problem
Combines #86 with #89 to hopefully resolve issues faster.


## ⚙️ Release Notes 
*Add a bullet point list summary of the feature and possible migration
guides if this is a breaking change so this section can be added to the
release notes.*
*Include code snippets that provide examples of the feature implemented
or links to the documentation if it appends or changes the public
interface.*


## 📚 Documentation
*Please ensure that you properly document any additions in conformance
to [Spezi Documentation
Guide](https://github.com/StanfordSpezi/.github/blob/main/DOCUMENTATIONGUIDE.md).*
*You can use this section to describe your solution, but we encourage
contributors to document your reasoning and changes using in-line
documentation.*


## ✅ Testing
*Please ensure that the PR meets the testing requirements set by CodeCov
and that new functionality is appropriately tested.*
*This section describes important information about the tests and why
some elements might not be testable.*


### Code of Conduct & Contributing Guidelines 

By submitting creating this pull request, you agree to follow our [Code
of
Conduct](https://github.com/StanfordBDHG/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordBDHG/.github/blob/main/CONTRIBUTING.md):
- [x] I agree to follow the [Code of
Conduct](https://github.com/StanfordBDHG/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordBDHG/.github/blob/main/CONTRIBUTING.md).

---------

Co-authored-by: Andreas Bauer <[email protected]>
Co-authored-by: nriedman <[email protected]>
  • Loading branch information
3 people authored Sep 16, 2024
1 parent 7c1ff46 commit c8cf7ae
Show file tree
Hide file tree
Showing 68 changed files with 796 additions and 8,406 deletions.
5 changes: 0 additions & 5 deletions .firebaserc

This file was deleted.

5 changes: 0 additions & 5 deletions .firebaserc.license

This file was deleted.

5 changes: 4 additions & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ jobs:
with:
artifactname: ENGAGEHF.xcresult
runsonlabels: '["macOS", "self-hosted"]'
setupfirebaseemulator: true
fastlanelane: test
firebaseemulatorimport: ./firebase
firebasejsonpath: ./ENGAGE-HF-Firebase/firebase.json
setupfirebaseemulator: true
checkout_submodules: true
customcommand: npm --prefix ENGAGE-HF-Firebase run prepare && firebase emulators:exec -c ./ENGAGE-HF-Firebase/firebase.json --export-on-exit=./firebase 'npm --prefix ./ENGAGE-HF-Firebase/functions run serve:seed'
secrets:
GOOGLE_APPLICATION_CREDENTIALS_BASE64: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS_BASE64 }}
uploadcoveragereport:
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "ENGAGE-HF-Firebase"]
path = ENGAGE-HF-Firebase
url = https://github.com/StanfordBDHG/ENGAGE-HF-Firebase.git
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This source file is part of the ENGAGE-HF project based on the Stanford Spezi Template Application project

SPDX-FileCopyrightText: 2023 Stanford University
SPDX-FileCopyrightText: 2022 Stanford University and the project authors (see CONTRIBUTORS.md)

SPDX-License-Identifier: MIT
1 change: 1 addition & 0 deletions ENGAGE-HF-Firebase
Submodule ENGAGE-HF-Firebase added at cc09c8
108 changes: 80 additions & 28 deletions ENGAGEHF.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
{
"identity" : "fhirmodels",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/FHIRModels.git",
"location" : "https://github.com/apple/FHIRModels",
"state" : {
"revision" : "861afd5816a98d38f86220eab2f812d76cad84a0",
"version" : "0.5.0"
Expand All @@ -60,8 +60,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/firebase/firebase-ios-sdk.git",
"state" : {
"revision" : "9118aca998dbe2ceac45d64b21a91c6376928df7",
"version" : "11.1.0"
"revision" : "1fc52ab0e172e7c5a961f975a76c2611f4f22852",
"version" : "11.2.0"
}
},
{
Expand Down Expand Up @@ -114,8 +114,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/StanfordBDHG/HealthKitOnFHIR.git",
"state" : {
"revision" : "c24e316311ff9813cb1fe32cd8820bcca6e5e7f2",
"version" : "0.2.10"
"revision" : "87a9257e6fa37407f3437e4a0bf21dd09a4ea7c5",
"version" : "0.2.11"
}
},
{
Expand Down Expand Up @@ -195,8 +195,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/StanfordSpezi/SpeziAccount.git",
"state" : {
"revision" : "6b1603d2dd95b1787cfb0cbdce32a4b4bebb79ae",
"version" : "2.0.0-beta.5"
"revision" : "397a55a7b0f108088d3db510ee48fc974a322c87",
"version" : "2.0.0-beta.8"
}
},
{
Expand All @@ -222,17 +222,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/StanfordSpezi/SpeziDevices.git",
"state" : {
"revision" : "cfba7691b6211bb2f566a8c263ff973921ee555b",
"version" : "1.2.2"
"revision" : "dc8dcd53773b9bb4041870b0f65cec32f7ed5108",
"version" : "1.3.0"
}
},
{
"identity" : "spezifirebase",
"kind" : "remoteSourceControl",
"location" : "https://github.com/StanfordSpezi/SpeziFirebase.git",
"state" : {
"revision" : "12e7b779a3339737ea100b4eb7491072d4e83cd5",
"version" : "2.0.0-beta.2"
"revision" : "5aae93f567091b29f7683863baa2241f7c95290d",
"version" : "2.0.0-beta.4"
}
},
{
Expand Down Expand Up @@ -328,7 +328,7 @@
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"location" : "https://github.com/apple/swift-collections",
"state" : {
"revision" : "9bf03ff58ce34478e66aaee630e491823326fd06",
"version" : "1.1.3"
Expand All @@ -339,17 +339,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "4c4453b489cf76e6b3b0f300aba663eb78182fad",
"version" : "2.70.0"
"revision" : "9746cf80e29edfef2a39924a66731249223f42a3",
"version" : "2.72.0"
}
},
{
"identity" : "swift-package-list",
"kind" : "remoteSourceControl",
"location" : "https://github.com/FelixHerrmann/swift-package-list",
"state" : {
"revision" : "01f2e9860c122dd8c3ae2a7e642b85bbb053efe3",
"version" : "4.2.0"
"revision" : "60b77bea44f5b2f7369aecfcd993f7672e09602a",
"version" : "4.3.0"
}
},
{
Expand Down
4 changes: 2 additions & 2 deletions ENGAGEHF.xcodeproj/xcshareddata/xcschemes/ENGAGEHF.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@
</CommandLineArgument>
<CommandLineArgument
argument = "--assumeOnboardingComplete"
isEnabled = "NO">
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "--setupTestEnvironment"
isEnabled = "NO">
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "--testMockDevices"
Expand Down
58 changes: 0 additions & 58 deletions ENGAGEHF/Account/AccountSetupSheet.swift

This file was deleted.

39 changes: 3 additions & 36 deletions ENGAGEHF/Account/AccountSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,17 @@
// SPDX-License-Identifier: MIT
//

import FirebaseFunctions
@_spi(TestingSupport) import SpeziAccount
import SpeziLicense
import SwiftUI


struct AccountSheet: View {
@Environment(\.dismiss) var dismiss

@Environment(Account.self) private var account
@Environment(\.accountRequired) var accountRequired

@State var isInSetup = false


var body: some View {
NavigationStack {
ZStack {
if account.signedIn && !isInSetup {
AccountOverview(close: .showCloseButton, deletion: .disabled) {
AdditionalAccountSections()
}
} else {
AccountSetup { _ in
dismiss() // we just signed in, dismiss the account setup sheet
} header: {
AccountSetupHeader()
}
.onAppear {
isInSetup = true
}
.toolbar {
if !accountRequired {
closeButton
}
}
}
}
}
}

var closeButton: some ToolbarContent {
ToolbarItem(placement: .cancellationAction) {
Button("CLOSE") {
dismiss()
AccountOverview(close: .showCloseButton, deletion: .disabled) {
AdditionalAccountSections()
}
}
}
Expand Down
1 change: 1 addition & 0 deletions ENGAGEHF/Account/AdditionalAccountSections.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SpeziDevicesUI
import SpeziLicense
import SwiftUI


struct AdditionalAccountSections: View {
var body: some View {
Section {
Expand Down
51 changes: 12 additions & 39 deletions ENGAGEHF/Account/InvitationCodeModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Firebase
import FirebaseAuth
import FirebaseFunctions
import Spezi
import SpeziAccount
Expand All @@ -27,50 +28,23 @@ class InvitationCodeModule: Module, EnvironmentAccessible {
}
}

func clearAccount() async {
do {
try await signOutAccount()
} catch {
logger.debug("Failed to sing out firebase account: \(error)")
}
}

func signOutAccount() async throws {
do {
try await accountService?.logout()
} catch FirebaseAccountError.notSignedIn {
// do nothing
} catch {
throw error
}
}

func verifyOnboardingCode(_ invitationCode: String) async throws {
do {
if FeatureFlags.disableFirebase {
guard invitationCode == "ENGAGEHFTEST1" else {
throw InvitationCodeError.invitationCodeInvalid
}

try? await Task.sleep(for: .seconds(0.25))
} else {
guard let accountService else {
preconditionFailure("The Firebase Account Service was not present even though `disableFirebase` was turned off!")
}

try await signOutAccount()
try await accountService.signUpAnonymously()

let checkInvitationCode = Functions.functions().httpsCallable("checkInvitationCode")

do {
_ = try await checkInvitationCode.call(
[
"invitationCode": invitationCode
]
)
logger.debug("About to enroll user")
let enrollUser = Functions.functions().httpsCallable("enrollUser")
_ = try await enrollUser.call(["invitationCode": invitationCode])
_ = try? await Auth.auth().currentUser?.getIDToken(forcingRefresh: true)
logger.debug("Successfully enrolled user!")
} catch {
logger.error("Failed to check invitation code: \(error)")
logger.error("Failed to enroll user: \(error)")
throw InvitationCodeError.invitationCodeInvalid
}
}
Expand Down Expand Up @@ -114,20 +88,19 @@ class InvitationCodeModule: Module, EnvironmentAccessible {
try await accountService.login(userId: email, password: password)
return // account was already established previously
} catch FirebaseAccountError.invalidCredentials {
// probably doesn't exists. We try to create a new one below
// probably doesn't exist. We try to create a new one below
} catch {
logger.error("Failed logging into test account: \(error)")
return
throw error
}

try await verifyOnboardingCode(invitationCode)


do {
var details = AccountDetails()
details.userId = email
details.password = password
details.name = PersonNameComponents(givenName: "Leland", familyName: "Stanford")
try await accountService.signUp(with: details)
try await verifyOnboardingCode(invitationCode)
} catch {
logger.error("Failed setting up test account : \(error)")
throw error
Expand Down
Loading

0 comments on commit c8cf7ae

Please sign in to comment.