0Pricing
Firebase Auth & Realtime Database Apps · Ders

E-posta/Parola Kimlik Doğrulamasını Uygulama

Firebase Authentication kullanarak e-posta ve parola kimlik bilgileriyle kullanıcı kaydı ve oturum açma işlevlerini uygulayın

E-posta/Parola Kimlik Doğrulamasını Uygulama, CoddyKit'te ücretsiz bir Firebase Auth & Realtime Database Apps dersidir. Bu, 4 dersinin 1. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Firebase Auth & Realtime Database Apps öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Firebase Auth & Realtime Database Apps kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

Email/Password Auth Intro

Welcome to implementing user authentication with Firebase! In this lesson, we'll focus on the most common method: Email and Password.

  • It's straightforward and widely understood by users.
  • Firebase handles the secure storage of user credentials.
  • You'll learn to register new users and sign in existing ones.

Enable Email/Password Provider

Before writing code, you need to enable Email/Password authentication in your Firebase project.

Go to the Firebase console:

  1. Navigate to Authentication.
  2. Click on the Sign-in method tab.
  3. Enable the Email/Password provider.

This tells Firebase to accept these credentials.

Registering New Users

To let new users create an account, you'll use a Firebase Authentication method. This method takes an email and a password, then securely creates a new user record in your Firebase project.

It's crucial to gather the user's email and a strong password from your app's UI.

User Registration Code

Here's how you register a new user using the Firebase SDK. We'll use JavaScript syntax, common for web and mobile apps.

createUserWithEmailAndPassword() is the key function.

import { getAuth, createUserWithEmailAndPassword } from "firebase/auth";

const auth = getAuth();

function registerUser(email, password) {
  createUserWithEmailAndPassword(auth, email, password)
    .then((userCredential) => {
      // Signed up successfully
      const user = userCredential.user;
      console.log("User registered:", user.email);
    })
    .catch((error) => {
      const errorCode = error.code;
      const errorMessage = error.message;
      console.error("Registration error:", errorCode, errorMessage);
    });
}

Signing In Existing Users

Once a user has registered, they can sign in using the same email and password. This process verifies their credentials against the stored records in Firebase.

If the credentials match, Firebase provides a user object, indicating a successful login.

User Login Code

To sign in an existing user, you use signInWithEmailAndPassword(). It works similarly to registration, taking an email and password.

This function also returns a Promise, allowing you to handle success or failure.

import { getAuth, signInWithEmailAndPassword } from "firebase/auth";

const auth = getAuth();

function loginUser(email, password) {
  signInWithEmailAndPassword(auth, email, password)
    .then((userCredential) => {
      // Signed in successfully
      const user = userCredential.user;
      console.log("User logged in:", user.email);
    })
    .catch((error) => {
      const errorCode = error.code;
      const errorMessage = error.message;
      console.error("Login error:", errorCode, errorMessage);
    });
}

Accessing Current User Data

After a user successfully registers or logs in, you can access their information via the currentUser object. This object holds details like their unique ID (UID) and email.

It's vital for personalizing app content.

import { getAuth } from "firebase/auth";

const auth = getAuth();

function displayUserInfo() {
  const user = auth.currentUser;

  if (user) {
    // User is signed in
    console.log("User UID:", user.uid);
    console.log("User Email:", user.email);
    // You can also get user.displayName, user.photoURL, etc.
  } else {
    // No user is signed in
    console.log("No user currently logged in.");
  }
}

Logging Users Out

Providing a way for users to sign out is essential for security and user experience. Firebase Auth makes this simple with the signOut() method.

Calling signOut() clears the user's session.

import { getAuth, signOut } from "firebase/auth";

const auth = getAuth();

function logoutUser() {
  signOut(auth).then(() => {
    // Sign-out successful.
    console.log("User signed out successfully!");
  }).catch((error) => {
    // An error happened.
    console.error("Sign out error:", error);
  });
}

Basic Error Handling

When dealing with authentication, errors are common (e.g., wrong password, email already in use). Firebase provides specific error codes and messages.

Always include .catch() blocks to gracefully handle these issues and provide feedback to your users.

import { getAuth, signInWithEmailAndPassword } from "firebase/auth";

const auth = getAuth();

// Example with error handling
signInWithEmailAndPassword(auth, "wrong@example.com", "badpassword")
  .then((userCredential) => {
    console.log("Login successful!");
  })
  .catch((error) => {
    const errorCode = error.code; // e.g., 'auth/user-not-found'
    const errorMessage = error.message; // e.g., 'There is no user record...'
    console.error("Auth failed:", errorCode, errorMessage);
    // Display user-friendly message based on errorCode
  });

Check Your Knowledge

Which Firebase Authentication method is used to create a new user account with an email and password?

Recap: Email/Password Auth

Great job! You've learned the fundamentals of Email/Password authentication with Firebase.

  • You enable the provider in the Firebase console.
  • createUserWithEmailAndPassword() registers new users.
  • signInWithEmailAndPassword() logs in existing users.
  • currentUser gives you access to user data.
  • signOut() logs users out.
  • Always implement error handling for a robust app.

Next, we'll explore managing user sessions and states!

Sıkça Sorulan Sorular

“E-posta/Parola Kimlik Doğrulamasını Uygulama” dersi ücretsiz mi?

Evet — “E-posta/Parola Kimlik Doğrulamasını Uygulama” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Firebase Auth & Realtime Database Apps kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Firebase Auth & Realtime Database Apps kursu toplamda 4 dersten oluşur.

“E-posta/Parola Kimlik Doğrulamasını Uygulama” dersinde ne öğreneceğim?

Firebase Authentication kullanarak e-posta ve parola kimlik bilgileriyle kullanıcı kaydı ve oturum açma işlevlerini uygulayın Firebase Auth & Realtime Database Apps ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Firebase Auth & Realtime Database Apps öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Firebase Auth & Realtime Database Apps, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 1. dersidir.

“E-posta/Parola Kimlik Doğrulamasını Uygulama” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Firebase Auth & Realtime Database Apps dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Firebase Auth & Realtime Database Apps dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. E-posta/Parola Kimlik Doğrulamasını Uygulama
  2. Kullanıcı Oturumlarını ve Durumlarını Yönetme
  3. Kimlik Doğrulama Hatalarını Yönetme
  4. Parola Sıfırlama ve E-posta Doğrulama
← Firebase Auth & Realtime Database Apps Sayfasına Dön