Khi thư viện Firebase PNV xác minh thành công số điện thoại của một thiết bị, thư viện này sẽ trả về số điện thoại đã xác minh và một mã thông báo đã ký chứa số điện thoại đó. Nếu sử dụng số điện thoại đã xác minh bên ngoài ứng dụng khách, bạn nên chuyển mã thông báo thay vì chính số điện thoại để có thể xác minh tính toàn vẹn của số điện thoại khi sử dụng. Để xác minh mã thông báo, bạn có thể sử dụng bất kỳ thư viện xác minh JWT nào. Sử dụng thư viện để xác minh tất cả những điều sau:
Tiêu đề
typđược đặt thànhJWT.Mã thông báo được ký bằng một trong các khoá được xuất bản tại điểm cuối Firebase PNV JWKS bằng thuật toán
ES256:https://fpnv.googleapis.com/v1beta/jwksXác nhận quyền sở hữu của nhà phát hành chứa số dự án Firebase của bạn và có định dạng như sau:
https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBERBạn có thể tìm thấy số dự án Firebase của mình trên thẻ
Cài đặt > Chung của bảng điều khiển Firebase.Xác nhận quyền sở hữu đối tượng là một danh sách chứa mã dự án và mã số dự án Firebase của bạn, đồng thời có định dạng như sau:
[ https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBER, https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_ID, ]Mã thông báo chưa hết hạn.
Ví dụ
Ví dụ: ứng dụng Express.js sau đây nhận mã thông báo Firebase PNV từ yêu cầu HTTP POST và sử dụng thư viện xác minh JWT để kiểm tra chữ ký và thông tin xác nhận quyền sở hữu của mã thông báo:
Node.js
import express from "express";
import { JwtVerifier } from "aws-jwt-verify";
// 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 behavior)
const fpnvVerifier = JwtVerifier.create({ issuer, audience, jwksUri });
const app = express();
app.post('/verifiedPhoneNumber', async (req, res) => {
if (!req.body) return res.sendStatus(400);
// Get the token from the body of the request.
const fpnvToken = req.body;
try {
// Attempt to verify the token using the verifier configured
previously.
const verifiedPayload = await fpnvVerifier.verify(fpnvToken);
// If verification succeeds, the subject claim of the token contains the
// verified phone number. You can use this value however it's needed by
// your app.
const verifiedPhoneNumber = verifiedPayload.sub;
// (Do something with it...)
return res.sendStatus(200);
} catch {
// If verification fails, reject the token.
return res.sendStatus(400);
}
});
app.listen(3000);