When I try to login It prints "Sign-in error: The supplied auth credential is malformed or has expired. Other error: The supplied auth credential is malformed or has expired. Error Domain: FIRAuthErrorDomain Error Code: 17004 Error Description: The supplied auth credential is malformed or has expired. Login Error: The supplied auth credential is malformed or has expired."
Any help is appreciated
AuthenticationManager:
import FirebaseAuth
import Combine
class AuthenticationManager: ObservableObject {
static let shared = AuthenticationManager()
@Published var user: BoatGuardianUser?
@Published var isAuthenticated: Bool = false
private init() {
// Check if the user is already signed in when the app starts
if let firebaseUser = Auth.auth().currentUser {
self.user = BoatGuardianUser(id: firebaseUser.uid, name: firebaseUser.displayName ?? "", photoURL: firebaseUser.photoURL?.absoluteString ?? "")
self.isAuthenticated = true
}
}
func signIn(email: String, password: String, completion: @escaping (Result<BoatGuardianUser, Error>) -> Void) {
Auth.auth().signIn(withEmail: email, password: password) { [weak self] authResult, error in
if let error = error {
// Log detailed error information
print("Sign-in error: \(error.localizedDescription)")
self?.handleSignInError(error)
completion(.failure(error))
return
}
guard let firebaseUser = authResult?.user else {
// Log error if user not found
let userInfoError = NSError(domain: "Auth", code: -1, userInfo: [NSLocalizedDescriptionKey: "User not found"])
print("User not found error: \(userInfoError.localizedDescription)")
completion(.failure(userInfoError))
return
}
let user = BoatGuardianUser(id: firebaseUser.uid, name: firebaseUser.displayName ?? "", photoURL: firebaseUser.photoURL?.absoluteString ?? "")
self?.user = user
self?.isAuthenticated = true
completion(.success(user))
}
}
func signOut() {
do {
try Auth.auth().signOut()
self.user = nil
self.isAuthenticated = false
} catch {
// Log error during sign out
print("Error signing out: \(error.localizedDescription)")
}
}
private func handleSignInError(_ error: Error) {
let nsError = error as NSError
let authErrorCode = AuthErrorCode.Code(rawValue: nsError.code)
switch authErrorCode {
case .networkError:
print("Network error: \(nsError.localizedDescription)")
case .wrongPassword:
print("Wrong password: \(nsError.localizedDescription)")
case .invalidEmail:
print("Invalid email: \(nsError.localizedDescription)")
case .userNotFound:
print("User not found: \(nsError.localizedDescription)")
case .userDisabled:
print("User disabled: \(nsError.localizedDescription)")
case .expiredActionCode:
print("Expired action code: \(nsError.localizedDescription)")
case .invalidActionCode:
print("Invalid action code: \(nsError.localizedDescription)")
case .emailAlreadyInUse:
print("Email already in use: \(nsError.localizedDescription)")
default:
print("Other error: \(nsError.localizedDescription)")
}
}
}
struct BoatGuardianUser: Identifiable {
let id: String
let name: String
let photoURL: String
}
Firebase Rules
match /databases/{database}/documents {
// Allow read/write on user documents only if authenticated and the user is accessing their own document
match /users/{userId} {
allow read, write: if request.auth != null && request.auth.uid == userId;
}
// Allow read access to public pins and write access only to the owner of the pin
match /pins/{pinId} {
// Allow read access to public pins
allow read: if request.auth != null && (resource.data.isPublic == true || request.auth.uid == resource.data.userId);
// Allow write access only if authenticated and the user is the owner of the pin
allow write: if request.auth != null && request.auth.uid == resource.data.userId;
}
// Other collection rules can be added here
match /otherCollection/{documentId} {
allow read, write: if request.auth != null;
}
}
}