When you don't want to install firebase-admin-sdk
just to verify id token
and/or app check token
.
npm install firebase-verifier
Obtains project_id
and project_no
from Google Cloud Console
import express from "express";
import {
authVerifier,
appCheckVerifier,
firebaseVerifier,
} from "firebase-verifier";
const project_id = "<project_id>";
const project_no = "<project_no>";
const app = express();
// Verify Id token
app.use(authVerifier(project_id));
app.get("/", (req, res) => {
console.log(res.locals.user);
/* Output
{
"sub": "kxljssoieeJKLe",
"email": "chientrm@gmail.com",
...
}
*/
});
...
// Verify App Check token
app.use(appCheckVerifier(project_no));
app.get("/", (req, res) => {
console.log(res.locals.device);
/* Output
{
...
}
*/
});
...
// Verify both Id token and App Check token
app.use(firebaseVerifier({ project_id, project_no }));
app.get("/", (req, res) => {
console.log(res.locals.user);
console.log(res.locals.device);
});
app.listen(3000);
If any of the verifiers failed, res.locals.user
, res.locals.device
are null and res.locals.error
contains the error.