-
-
Notifications
You must be signed in to change notification settings - Fork 244
Add support for passkeys #1430
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
Merged
Merged
Add support for passkeys #1430
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
BookPlayer/Profile/Account/AccountPasskeySectionView.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| // | ||
| // AccountPasskeySectionView.swift | ||
| // BookPlayer | ||
| // | ||
| // Created by Claude on 1/10/25. | ||
| // Copyright © 2025 BookPlayer LLC. All rights reserved. | ||
| // | ||
|
|
||
| import BookPlayerKit | ||
| import SwiftUI | ||
|
|
||
| struct AccountPasskeySectionView: View { | ||
| @State private var passkey: PasskeyInfo? | ||
| @State private var isLoading = false | ||
| @State private var loadFailed = false | ||
| @State private var error: Error? | ||
| @State private var showDeleteConfirmation = false | ||
|
|
||
| @EnvironmentObject private var theme: ThemeViewModel | ||
| @Environment(\.accountService) private var accountService | ||
| @Environment(\.passkeyService) private var passkeyService | ||
|
|
||
| var body: some View { | ||
| Section { | ||
| if isLoading { | ||
| loadingView | ||
| } else if let passkey = passkey { | ||
| passkeyRow(passkey) | ||
| } else if loadFailed { | ||
| retryButton | ||
| } else { | ||
| addButton | ||
| } | ||
| } header: { | ||
| Text("Passkey") | ||
| .foregroundStyle(theme.secondaryColor) | ||
| } | ||
| .onAppear { | ||
| Task { | ||
| await loadPasskey() | ||
| } | ||
| } | ||
| .errorAlert(error: $error) | ||
| .alert("passkey_delete_title".localized, isPresented: $showDeleteConfirmation) { | ||
| Button("cancel_button".localized, role: .cancel) {} | ||
| Button("delete_button".localized, role: .destructive) { | ||
| if let passkey = passkey { | ||
| Task { | ||
| await deletePasskey(id: passkey.id) | ||
| } | ||
| } | ||
| } | ||
| } message: { | ||
| Text("passkey_delete_message".localized) | ||
| } | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| private var loadingView: some View { | ||
| HStack { | ||
| Spacer() | ||
| ProgressView() | ||
| Spacer() | ||
| } | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| private func passkeyRow(_ passkey: PasskeyInfo) -> some View { | ||
| HStack(spacing: Spacing.S) { | ||
| Image(systemName: "person.badge.key") | ||
| .font(.title2) | ||
| .foregroundStyle(theme.linkColor) | ||
|
|
||
| VStack(alignment: .leading, spacing: 4) { | ||
| Text(passkey.deviceName ?? "passkey_unnamed_device".localized) | ||
| .font(.body) | ||
| .foregroundStyle(theme.primaryColor) | ||
|
|
||
| Text("passkey_created".localized + " " + passkey.createdAt.formatted(date: .abbreviated, time: .omitted)) | ||
| .font(.caption) | ||
| .foregroundStyle(theme.secondaryColor) | ||
| } | ||
|
|
||
| Spacer() | ||
|
|
||
| Menu { | ||
| Button(role: .destructive) { | ||
| showDeleteConfirmation = true | ||
| } label: { | ||
| Label("delete_button".localized, systemImage: "trash") | ||
| } | ||
| .tint(.red) | ||
| } label: { | ||
| Image(systemName: "ellipsis.circle") | ||
| .font(.title2) | ||
| .foregroundStyle(theme.linkColor) | ||
| .frame(width: 44, height: 44) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| private var retryButton: some View { | ||
| Button { | ||
| Task { | ||
| await loadPasskey() | ||
| } | ||
| } label: { | ||
| Label("network_error_title".localized, systemImage: "arrow.clockwise") | ||
| .foregroundStyle(theme.linkColor) | ||
| } | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| private var addButton: some View { | ||
| Button { | ||
| addPasskey() | ||
| } label: { | ||
| Label("account_add_passkey_title".localized, systemImage: "person.badge.key") | ||
| .foregroundStyle(theme.linkColor) | ||
| } | ||
| } | ||
|
|
||
| private func loadPasskey() async { | ||
| isLoading = true | ||
| loadFailed = false | ||
| do { | ||
| let passkeys = try await passkeyService.listPasskeys() | ||
| // Only use the first passkey (we only support one per account) | ||
| passkey = passkeys.first | ||
| } catch { | ||
| loadFailed = true | ||
| } | ||
| isLoading = false | ||
| } | ||
|
|
||
| private func addPasskey() { | ||
| // Don't allow adding if one already exists | ||
| guard passkey == nil else { return } | ||
|
|
||
| Task { | ||
| do { | ||
| let deviceName = UIDevice.current.name | ||
| let email = accountService.getAccount()?.email | ||
| try await passkeyService.addPasskeyToAccount(deviceName: deviceName, email: email!) | ||
| await loadPasskey() | ||
| } catch PasskeyError.userCancelled { | ||
| // User cancelled, do nothing | ||
| } catch { | ||
| self.error = error | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private func deletePasskey(id: Int) async { | ||
| do { | ||
| try await passkeyService.deletePasskey(id: id) | ||
| await loadPasskey() | ||
| } catch { | ||
| self.error = error | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #Preview { | ||
| Form { | ||
| AccountPasskeySectionView() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // | ||
| // AppleSignInLink.swift | ||
| // BookPlayer | ||
| // | ||
| // Created by Claude on 1/10/25. | ||
| // Copyright © 2025 BookPlayer LLC. All rights reserved. | ||
| // | ||
|
|
||
| import AuthenticationServices | ||
| import BookPlayerKit | ||
| import SwiftUI | ||
|
|
||
| struct AppleSignInLink: View { | ||
| @Environment(\.loadingState) private var loadingState | ||
| @Environment(\.accountService) private var accountService | ||
| @EnvironmentObject private var theme: ThemeViewModel | ||
|
|
||
| @State private var showAppleSignIn = false | ||
|
|
||
| var handleSignIn: (_ hasSubscription: Bool) -> Void | ||
|
|
||
| var body: some View { | ||
| SignInWithAppleButton(.signIn) { request in | ||
| request.requestedScopes = [.email] | ||
| } onCompletion: { result in | ||
| switch result { | ||
| case .success(let authorization): | ||
| Task { | ||
| do { | ||
| loadingState.show = true | ||
|
|
||
| guard | ||
| let creds = authorization.credential as? ASAuthorizationAppleIDCredential, | ||
| let tokenData = creds.identityToken, | ||
| let token = String(data: tokenData, encoding: .utf8) | ||
| else { | ||
| throw AccountError.missingToken | ||
| } | ||
|
|
||
| let account = try await accountService.login( | ||
| with: token, | ||
| userId: creds.user | ||
| ) | ||
|
|
||
| loadingState.show = false | ||
|
|
||
| handleSignIn(account?.hasSubscription == true) | ||
| } catch { | ||
| loadingState.show = false | ||
| loadingState.error = error | ||
| } | ||
| } | ||
| case .failure(let error): | ||
| if (error as? ASAuthorizationError)?.code != .canceled { | ||
| loadingState.error = error | ||
| } | ||
| } | ||
| } | ||
| .frame(height: 46) | ||
| .signInWithAppleButtonStyle(theme.useDarkVariant ? .white : .black) | ||
| .padding(.horizontal, Spacing.M) | ||
| } | ||
| } | ||
|
|
||
| #Preview { | ||
| AppleSignInLink { _ in } | ||
| .environmentObject(ThemeViewModel()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // | ||
| // ContinueWithPasskeyButton.swift | ||
| // BookPlayer | ||
| // | ||
| // Created by Claude on 1/10/25. | ||
| // Copyright © 2025 BookPlayer LLC. All rights reserved. | ||
| // | ||
|
|
||
| import BookPlayerKit | ||
| import SwiftUI | ||
|
|
||
| struct ContinueWithPasskeyButton: View { | ||
| @EnvironmentObject private var theme: ThemeViewModel | ||
|
|
||
| var action: () -> Void | ||
|
|
||
| var body: some View { | ||
| Button(action: action) { | ||
| HStack(spacing: Spacing.S) { | ||
| Image(systemName: "person.badge.key.fill") | ||
| Text("passkey_continue_button".localized) | ||
| } | ||
| .bpFont(Fonts.body) | ||
| .frame(maxWidth: .infinity) | ||
| .foregroundColor(theme.linkColor) | ||
| } | ||
| .padding(.horizontal, Spacing.M) | ||
| } | ||
| } | ||
|
|
||
| #Preview { | ||
| ContinueWithPasskeyButton { } | ||
| .environmentObject(ThemeViewModel()) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Force unwrapping the email with '!' is unsafe. If the account's email is nil, this will crash the app. Consider adding proper error handling or using optional binding to safely unwrap the email value.