เมื่อFirebase PNV ไลบรารียืนยันหมายเลขโทรศัพท์ของอุปกรณ์เรียบร้อยแล้ว ระบบจะ แสดงหมายเลขโทรศัพท์ที่ยืนยันแล้วและโทเค็นที่ลงชื่อซึ่งมีหมายเลขดังกล่าว หากใช้หมายเลขโทรศัพท์ที่ยืนยันแล้วนอกไคลเอ็นต์แอป คุณควรส่งต่อโทเค็นแทนหมายเลขโทรศัพท์เองเพื่อให้ยืนยันความสมบูรณ์ของโทเค็นได้เมื่อใช้งาน คุณใช้ไลบรารีการยืนยัน JWT ใดก็ได้เพื่อยืนยันโทเค็น ใช้ไลบรารีเพื่อยืนยันข้อมูลต่อไปนี้ทั้งหมด
ตั้งค่าส่วนหัว
typเป็นJWTโทเค็นลงนามโดยใช้คีย์ใดคีย์หนึ่งที่เผยแพร่ในFirebase PNVปลายทาง JWKS ที่มี
ES256อัลกอริทึมhttps://fpnv.googleapis.com/v1beta/jwksการอ้างสิทธิ์ของผู้ออกจะมีหมายเลขโปรเจ็กต์ Firebase ของคุณและอยู่ในรูปแบบต่อไปนี้
https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBERคุณดูหมายเลขโปรเจ็กต์ Firebase ได้ในหน้าการตั้งค่าโปรเจ็กต์ ของคอนโซล Firebase
การอ้างสิทธิ์กลุ่มเป้าหมายคือรายการที่มีหมายเลขโปรเจ็กต์ Firebase และ รหัสโปรเจ็กต์ และอยู่ในรูปแบบต่อไปนี้
[ https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBER, https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_ID, ]โทเค็นยังไม่หมดอายุ
ตัวอย่าง
ตัวอย่างสั้นๆ เช่น แอป Express.js ต่อไปนี้จะรับโทเค็น Firebase PNV จากคำขอ HTTP POST และใช้ไลบรารีการยืนยัน JWT เพื่อตรวจสอบลายเซ็นและการอ้างสิทธิ์ของโทเค็น
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);