การยืนยันตัวตนแบบไม่ระบุตัวตนและแบบกำหนดเอง
สำรวจการยืนยันตัวตนแบบไม่ระบุตัวตนสำหรับผู้ใช้ชั่วคราว และเรียนรู้การสร้างระบบยืนยันตัวตนแบบกำหนดเองด้วยไฟร์เบส
การยืนยันตัวตนแบบไม่ระบุตัวตนและแบบกำหนดเอง เป็นบทเรียน Firebase Auth & Realtime Database Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Firebase Auth & Realtime Database Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Firebase Auth & Realtime Database Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Anonymous & Custom Auth
Welcome to a lesson on advanced authentication methods! We'll explore two powerful Firebase features:
- Anonymous Authentication: For temporary users who don't want to sign up yet.
- Custom Authentication: For integrating Firebase Auth with your existing user systems.
Let's dive in!
What is Anonymous Authentication?
Anonymous authentication allows users to sign in without providing any credentials like email or social accounts. Firebase creates a temporary anonymous account for them.
This is perfect for:
- Guest modes in apps.
- Allowing users to try out features before committing to a full signup.
- Saving progress temporarily without requiring an account.
Implementing Anonymous Sign-In
Signing in anonymously is straightforward. The Firebase SDK handles creating the temporary user for you.
Try running this example to see a simulated anonymous sign-in:
class FirebaseAuth {
static FirebaseAuth getInstance() { return new FirebaseAuth(); }
void signInAnonymously() {
System.out.println("Attempting anonymous sign-in...");
try { Thread.sleep(100); } catch (InterruptedException e) {}
System.out.println("Anonymous user signed in!");
System.out.println("Temporary User ID: guest_123abc");
}
}
public class Main {
public static void main(String[] args) {
FirebaseAuth auth = FirebaseAuth.getInstance();
auth.signInAnonymously();
}
}Upgrading Anonymous Accounts
Anonymous accounts are temporary. What if your user decides to sign up later?
Firebase allows you to link an anonymous account to a permanent one (e.g., email/password, Google). The user's data and progress from their anonymous session will then be associated with their new permanent account.
This is done using methods like linkWithCredential() or linkWithPopup() once the user provides permanent credentials.
Introduction to Custom Authentication
Custom authentication is for when you already have your own user management system or want to integrate Firebase Auth with a complex backend.
Instead of Firebase handling user sign-up directly, your backend authenticates the user and then mints a special custom token. Your client application then uses this token to sign into Firebase.
Custom Auth: The Flow
Here's how custom authentication works:
- User logs in via your app, sending credentials to your backend.
- Your backend verifies credentials (e.g., against your own database).
- If successful, your backend uses the Firebase Admin SDK to create a custom token for that user.
- Your backend sends this custom token back to your client app.
- Your client app uses the Firebase client SDK to sign in with this custom token.
Generating Custom Tokens (Backend)
The critical step on your server is to generate the custom token using the Firebase Admin SDK. This SDK must be initialized with your Firebase project's service account credentials for security.
Here's a conceptual look at how your backend might generate a token (this code is for your server, not your app):
// This code runs on your secure backend server,
// NOT in your client-side application.
// It requires the Firebase Admin SDK.
// import com.google.firebase.auth.FirebaseAuth;
public class BackendCustomToken {
// This method would be part of your server logic.
public static String generateToken(String userId) {
try {
// In a real app:
// return FirebaseAuth.getInstance().createCustomToken(userId);
// For this example, we simulate it:
String token = "mock_token_for_" + userId + "_123xyz";
System.out.println("Backend generated token for " + userId);
return token;
} catch (Exception e) {
System.err.println("Backend error: " + e.getMessage());
return null;
}
}
public static void main(String[] args) {
// This main method is just to illustrate the concept.
// In reality, this logic is triggered by an API call.
String userId = "userFromYourDB";
String customToken = generateToken(userId);
if (customToken != null) {
System.out.println("Backend is ready to send this token: " + customToken);
}
}
}Signing In with a Custom Token (Client)
Once your client app receives the custom token from your backend, it uses the Firebase client SDK's signInWithCustomToken() method to complete the authentication process.
Run this example to see how your app uses the token to sign in:
class FirebaseAuth {
static FirebaseAuth getInstance() { return new FirebaseAuth(); }
void signInWithCustomToken(String token) {
System.out.println("Attempting sign-in with custom token...");
if (token != null && !token.isEmpty()) {
try { Thread.sleep(100); } catch (InterruptedException e) {}
System.out.println("Signed in with custom token successfully!");
System.out.println("User ID: custom_user_xyz");
} else {
System.out.println("Error: Custom token is missing.");
}
}
}
public class Main {
public static void main(String[] args) {
FirebaseAuth auth = FirebaseAuth.getInstance();
// Imagine this token comes from your backend
String customToken = "YOUR_CUSTOM_TOKEN_FROM_BACKEND";
auth.signInWithCustomToken(customToken);
}
}Benefits of Custom Auth
Custom authentication offers significant advantages:
- Flexibility: Integrate with virtually any existing user database or identity provider.
- Control: Maintain full control over your user registration and login logic on your backend.
- Migration: Easily migrate existing users to Firebase without forcing them to re-register.
- Complex Workflows: Implement intricate authentication flows that might not be directly supported by Firebase's built-in providers.
Quick Check: Auth Methods
Which of the following statements about Anonymous and Custom Authentication in Firebase are TRUE?
Recap: Temporary & Flexible Auth
Great job! In this lesson, we explored two powerful ways to manage user authentication:
- Anonymous Authentication: Ideal for guest users and initial app exploration, with the option to upgrade to a permanent account.
- Custom Authentication: Provides maximum flexibility by letting your backend handle user verification and issue Firebase custom tokens.
These methods allow you to tailor your app's authentication experience to a wide range of needs. Keep building amazing apps!
คำถามที่พบบ่อย
บทเรียน “การยืนยันตัวตนแบบไม่ระบุตัวตนและแบบกำหนดเอง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การยืนยันตัวตนแบบไม่ระบุตัวตนและแบบกำหนดเอง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Firebase Auth & Realtime Database Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Firebase Auth & Realtime Database Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การยืนยันตัวตนแบบไม่ระบุตัวตนและแบบกำหนดเอง”
สำรวจการยืนยันตัวตนแบบไม่ระบุตัวตนสำหรับผู้ใช้ชั่วคราว และเรียนรู้การสร้างระบบยืนยันตัวตนแบบกำหนดเองด้วยไฟร์เบส คุณปฏิบัติ Firebase Auth & Realtime Database Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Firebase Auth & Realtime Database Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Firebase Auth & Realtime Database Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การยืนยันตัวตนแบบไม่ระบุตัวตนและแบบกำหนดเอง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Firebase Auth & Realtime Database Apps นี้ได้ไหม
ได้ บทเรียน Firebase Auth & Realtime Database Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การผสานรวมการเข้าสู่ระบบด้วย Google
- การยืนยันตัวตนด้วย Facebook และ GitHub
- การยืนยันตัวตนแบบไม่ระบุตัวตนและแบบกำหนดเอง
- การผสานการเข้าสู่ระบบด้วย Apple