This Node.js Express server will provide you with an endpoint to create an admin attribute on a authorized user when using Google Firebase.
- Clone this repo
git clone https://github.com/williamhelme/firebase-admin-emails-custom-claims.git
- Update the app-config.json file to include the config provided by Firebase (Settings -> Service Accounts -> Firebase Admin SDK -> Gemerate new private key). It should look something like this:
{ "type": "", "project_id": "", "private_key_id": "", "private_key": "", "client_email": "", "client_id": "", "auth_uri": "", "token_uri": "", "auth_provider_x509_cert_url": "", "client_x509_cert_url": "" }
- Also update the config.json to change the databaseURL and the validUsers properties to work with your application. You can also edit the port the Express server is running on.
{ "databaseURL": "https://<your-app-name>.firebaseio.com", "validUsers": ["test@example.com", "example@test.com"], "port": "8000" }
Once the config is setup, all that is required is to run npm start
from the console and now your firebase app can set an admin property to a user.
Below is a javascript example of how you would access this endpoint in the firebase auth process. After the token refresh the admin property should appear.
auth.signInWithPopup(AuthProvider)
.then(result => {
return result.user.getIdToken();
})
.then((idToken) => {
$.post(
"http://localhost:8000/setCustomClaims",
{
idToken: idToken
},
(data, status) => {
if (status == "success" && data) {
const json = JSON.parse(data);
if (json && json.status == "success") {
// Force token refresh.
fb.auth().currentUser.getIdToken(true);
}
}
}
);
})
.catch(error => {
console.log(error);
});
}
- Node.js
- Git
- npm
Currently there are no test available.
Run this project on a Node.js server using npm start
.
Please submit a pull request if you have any improvements.
This project is licensed under the ISC License