Firebase ফোন নম্বর যাচাইকরণ ব্যবহার করে Android-এ Firebase-এর মাধ্যমে যাচাই করা

Firebase Authentication সরাসরি সাইন-ইন করার জন্য Firebase PNV টোকেন গ্রহণ করতে পারে না; তবে, আপনি Firebase Authentication-এর কাস্টম যাচাইকরণ ফিচার ব্যবহার করে ব্যবহারকারীদের Firebase PNV-এর মাধ্যমে সাইন-ইন করার সুবিধা চালু করতে পারেন।

টোকেন এক্সচেঞ্জ এন্ডপয়েন্ট তৈরি করুন

Firebase-এ কাস্টম যাচাইকরণ সমাধান প্রয়োগ করার মূল ধাপ হল এমন একটি এন্ডপয়েন্ট তৈরি করা যা Firebase PNV টোকেন গ্রহণ করতে, এটি যাচাই করতে এবং তারপর Firebase কাস্টম যাচাইকরণ টোকেন ইস্যু করতে পারে। আপনার অ্যাপ্লিকেশন তখন ব্যবহারকারীকে সাইন-ইন করতে এই কাস্টম টোকেন ব্যবহার করতে পারে।

এই এন্ডপয়েন্ট যেকোনও প্ল্যাটফর্মে হোস্ট করা যেতে পারে, তবে নিম্নলিখিত উদাহরণে, এন্ডপয়েন্টটি 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)
        }
    }