firebase-token-verifier
v0.1.0
Published
Lightweight verification of Firebase JWT
Downloads
9
Maintainers
Readme
firebase-token-verifier
Firebase JWT verifier, implements Verify ID tokens using a third-party JWT library. Use as a lightweight alternative to the Firebase Admin SDK if all you need to do is verify or validate a Firebase JWT.
Install
npm install --save firebase-token-verifier
Usage
firebase-token-verifier
can be used with Promises, async
, or callbacks.
validate()
is just a wrapper around verify()
that also checks the project ID (audience/issuer), and optionally the user ID (subject).
const verifier = require('firebase-token-verifier');
const token = 'eyJhb...';
Promise
verify
verifier.verify(token)
.then((verified) => {
console.log('Verified!');
console.log(JSON.stringify(verified, null, 2));
})
.catch((err) => {
console.log('Error!');
console.log(err);
});
validate
// userId is optional
// verifier.validate(token, projectId)
verifier.validate(token, projectId, userId)
.then((validated) => {
console.log('Validated');
console.log(JSON.stringify(validated, null, 2));
})
.catch((err) => {
console.log('Error!');
console.log(err);
})
async
verify
try {
let verified = await verifier.verify(token);
console.log('Verified!');
console.log(JSON.stringify(verified, null, 2));
} catch(err) {
console.log('Error!');
console.log(err);
}
validate
try {
// userId is optional
// verifier.validate(token, projectId)
let validated = await verifier.validate(token, projectId, userId);
console.log('Validated!');
console.log(JSON.stringify(validated, null, 2));
} catch(err) {
console.log('Error!');
console.log(err);
}
callback
verify
verifier.verify(token, (err, verified) => {
if(err) {
console.log('Error!');
console.log(err);
} else {
console.log('Verified!');
console.log(JSON.stringify(verified, null, 2));
}
})
validate
// userId is optional
// verifier.validate(token, projectId, callback)
verifier.validate(token, projectId, userId, (err, validated) => {
if(err) {
console.log('Error!');
console.log(err);
} else {
console.log('Validated!');
console.log(JSON.stringify(validated, null, 2));
}
})
Examples
Please see demo.sh
and demo/*.js
.
$ sh demo.sh