0
0
iOS Swiftmobile~5 mins

Firebase Authentication in iOS Swift

Choose your learning style9 modes available
Introduction

Firebase Authentication helps you easily add user sign-in to your app. It keeps user accounts safe and lets users log in quickly.

When you want users to create accounts and log in to save their data.
When you want to let users sign in with email and password or social accounts like Google.
When you want to protect parts of your app so only signed-in users can access them.
When you want to manage user sessions without building your own backend.
When you want to quickly add secure authentication without much code.
Syntax
iOS Swift
import FirebaseAuth

// To sign up a user with email and password
Auth.auth().createUser(withEmail: "email@example.com", password: "password123") { authResult, error in
  if let error = error {
    print("Error: \(error.localizedDescription)")
    return
  }
  print("User signed up: \(authResult?.user.email ?? "No Email")")
}

// To sign in a user
Auth.auth().signIn(withEmail: "email@example.com", password: "password123") { authResult, error in
  if let error = error {
    print("Error: \(error.localizedDescription)")
    return
  }
  print("User signed in: \(authResult?.user.email ?? "No Email")")
}

Use createUser to register new users.

Use signIn to log in existing users.

Examples
Registers a new user with email and password.
iOS Swift
Auth.auth().createUser(withEmail: "user@example.com", password: "mypassword") { authResult, error in
  if let error = error {
    print("Sign up failed: \(error.localizedDescription)")
  } else {
    print("Signed up: \(authResult?.user.email ?? "No Email")")
  }
}
Logs in an existing user with email and password.
iOS Swift
Auth.auth().signIn(withEmail: "user@example.com", password: "mypassword") { authResult, error in
  if let error = error {
    print("Sign in failed: \(error.localizedDescription)")
  } else {
    print("Signed in: \(authResult?.user.email ?? "No Email")")
  }
}
Sample App

This SwiftUI app lets users enter email and password. They can tap Sign Up to create an account or Sign In to log in. The app shows messages about success or errors below the buttons.

iOS Swift
import SwiftUI
import FirebaseAuth

struct ContentView: View {
  @State private var email = ""
  @State private var password = ""
  @State private var message = ""

  var body: some View {
    VStack(spacing: 20) {
      TextField("Email", text: $email)
        .textContentType(.emailAddress)
        .keyboardType(.emailAddress)
        .autocapitalization(.none)
        .padding()
        .background(Color(.secondarySystemBackground))
        .cornerRadius(8)

      SecureField("Password", text: $password)
        .padding()
        .background(Color(.secondarySystemBackground))
        .cornerRadius(8)

      Button("Sign Up") {
        Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
          if let error = error {
            message = "Sign up failed: \(error.localizedDescription)"
          } else {
            message = "Signed up as: \(authResult?.user.email ?? "No Email")"
          }
        }
      }
      .padding()
      .background(Color.blue)
      .foregroundColor(.white)
      .cornerRadius(8)

      Button("Sign In") {
        Auth.auth().signIn(withEmail: email, password: password) { authResult, error in
          if let error = error {
            message = "Sign in failed: \(error.localizedDescription)"
          } else {
            message = "Signed in as: \(authResult?.user.email ?? "No Email")"
          }
        }
      }
      .padding()
      .background(Color.green)
      .foregroundColor(.white)
      .cornerRadius(8)

      Text(message)
        .padding()
        .foregroundColor(.red)
    }
    .padding()
  }
}
OutputSuccess
Important Notes

Always handle errors to inform users what went wrong.

Use secure password rules to keep accounts safe.

Remember to initialize Firebase in your app before using Auth.

Summary

Firebase Authentication lets you add user sign-in easily.

Use createUser to register and signIn to log in users.

Show clear messages to users about success or errors.