Firebase Phone Number Verification का इस्तेमाल करके, Android पर Firebase से पुष्टि करना

Firebase PNV की सार्वजनिक झलक में, Firebase Authentication सीधे तौर पर साइन-इन के लिए Firebase PNV टोकन स्वीकार नहीं कर सकता. हालांकि, Firebase Authentication की कस्टम पुष्टि करने की सुविधा का इस्तेमाल करके, उपयोगकर्ताओं को Firebase PNV से साइन इन करने की अनुमति दी जा सकती है.

टोकन एक्सचेंज एंडपॉइंट बनाना

Firebase में कस्टम ऑथेंटिकेशन की सुविधा लागू करने का मुख्य चरण यह है कि एक ऐसा एंडपॉइंट बनाया जाए जो Firebase PNV टोकन को स्वीकार कर सके, उसकी पुष्टि कर सके, और फिर Firebase कस्टम ऑथेंटिकेशन टोकन जारी कर सके. इसके बाद, आपका ऐप्लिकेशन इस कस्टम टोकन का इस्तेमाल करके, उपयोगकर्ता को साइन इन करा सकता है.

इस एंडपॉइंट को किसी भी प्लैटफ़ॉर्म पर होस्ट किया जा सकता है. हालांकि, यहां दिए गए उदाहरण में, Cloud Functions for Firebase का इस्तेमाल करके एंडपॉइंट को होस्ट किया गया है:

Node.js

import { JwtVerifier } from "aws-jwt-verify";
import { getApp } from "firebase-admin/app";
import { getAuth, UserRecord } from "firebase-admin/auth";
import { onRequest } from "firebase-functions/https";

// Because we're deploying to Cloud Functions for Firebase, admin credentials
// are automatically available.
const app = getApp();
const authAdmin = getAuth(app);

// Find your Firebase project number in the Firebase console.
const FIREBASE_PROJECT_NUMBER = "123456789";

// The issuer and audience claims of the FPNV token are specific to your
// project.
const issuer = `https://fpnv.googleapis.com/projects/${FIREBASE_PROJECT_NUMBER}`;
const audience = `https://fpnv.googleapis.com/projects/${FIREBASE_PROJECT_NUMBER}`;

// The JWKS URL contains the current public signing keys for FPNV tokens.
const jwksUri = "https://fpnv.googleapis.com/v1beta/jwks";

// Configure a JWT verifier to check the following:
// - The token is signed by Google
// - The issuer and audience claims match your project
// - The token has not yet expired (default begavior)
const fpnvVerifier = JwtVerifier.create({ issuer, audience, jwksUri });

// This Cloud Function is your token exchange endpoint. You pass the endpoint an
// FPNV token, and the Cloud Function verifies it and exchanges it for a
// Firebase Auth token corresponding to the same user.
export const signInWithFpnv = onRequest(async (req, res) => {
    // Get the FPNV token from the request body.
    const fpnvToken = req.body?;
    if (!fpnvToken) {
        res.sendStatus(400);
        return;
    }

    let verifiedPhoneNumber;
    try {
        // Attempt to verify the token using the verifier configured above.
        const verifiedPayload = await fpnvVerifier.verify(fpnvToken);

        // If verification succeeds, the subject claim of the token contains the
        // verified phone number.
        verifiedPhoneNumber = verifiedPayload.sub;
    } catch {
        // If verification fails, reject the token.
        res.sendStatus(403);
        return;
    }

    // Now that you have a verified phone number, look it up in your Firebase
    // project's user database.
    let user: UserRecord;
    try {
        // If a user account already exists with the phone number, retrieve it.
        user = await authAdmin.getUserByPhoneNumber(verifiedPhoneNumber);
    } catch {
        // Otherwise, create a new user account using the phone number.
        user = await authAdmin.createUser({phoneNumber: verifiedPhoneNumber});
    }

    // Finally, mint a Firebase custom auth token containing the UID of the user
    // you looked up or created. Return this token to the caller.
    const authToken = await authAdmin.createCustomToken(user.uid);
    res.status(200).send(authToken);
    return;
});

कस्टम पुष्टि करने वाले टोकन से साइन इन करना

एंडपॉइंट डिप्लॉय होने के बाद, उपयोगकर्ताओं को Firebase में साइन इन करने के लिए, यह तरीका अपनाएं:

  1. Firebase Phone Number Verification का इस्तेमाल शुरू करें पेज पर बताए गए तरीके का इस्तेमाल करके, Firebase PNV टोकन पाएं.

  2. इस टोकन को Cloud Functions एंडपॉइंट पर पास करें. एंडपॉइंट, आपके ऐप्लिकेशन को कस्टम ऑथेंटिकेशन टोकन भेजेगा. इसे signInWithCustomToken() को भेजा जा सकता है.

    उदाहरण के लिए, Retrofit का इस्तेमाल करके, signInWithFpnvToken() नाम की एक ऐसी विधि लिखी जा सकती है जिसका इंटरफ़ेस, Firebase की signin- विधियों में से किसी एक के जैसा हो:

    Kotlin

    class FpnvSigninExample {
        interface FPNVTokenExchangeService {
            @POST("signInWithFpnv")
            suspend fun signInWithFpnv(@Body fpnvToken: String): String
        }
    
        val retrofit = Retrofit.Builder()
            .baseUrl("https://example-project.cloudfunctions.net/")
            .build()
        val service: FPNVTokenExchangeService = retrofit.create(FPNVTokenExchangeService::class.java)
    
        suspend fun signInWithFpnvToken(fpnvToken: String): Task<AuthResult?> = coroutineScope {
            val authToken = service.signInWithFpnv(fpnvToken)
            return@coroutineScope Firebase.auth.signInWithCustomToken(authToken)
        }
    }