Skip to content

Adding Node.js Auth Token Claims #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions auth/create_custom_tokens.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';
const admin = require('firebase-admin');

// Initialize the Admin app with the default appication credentials
// [START initialize_sdk_with_default_config]
admin.initializeApp();
// [END initialize_sdk_with_default_config]

// Initialize the Admin app by providing a service accoune key
// [START initialize_sdk_with_service_account_id]
admin.initializeApp({
serviceAccountId: 'my-client-id@my-project-id.iam.gserviceaccount.com',
});
// [END initialize_sdk_with_service_account_id]

// [START custom_token]
var uid = 'some-uid';

admin.auth().createCustomToken(uid)
.then(function(customToken) {
// Send token back to client
})
.catch(function(error) {
console.log('Error creating custom token:', error);
});
// [END custom_token]

// [START custom_token_with_claims]
var userId = 'some-uid';
var additionalClaims = {
premiumAccount: true
};

admin.auth().createCustomToken(userId, additionalClaims)
.then(function(customToken) {
// Send token back to client
})
.catch(function(error) {
console.log('Error creating custom token:', error);
});
// [END custom_token_with_claims]
6 changes: 5 additions & 1 deletion firestore/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ function initializeApp() {
credential: admin.credential.applicationDefault()
});

var db = admin.firestore();
const db = admin.firestore();
// [START_EXCLUDE]
const settings = {timestampsInSnapshots: true};
db.settings(settings);
// [END_EXCLUDE]

// [END initialize_app]
return db;
Expand Down
4 changes: 4 additions & 0 deletions firestore/solution-aggregation/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ const functions = require('firebase-functions');
const admin = require('firebase-admin');

const db = admin.firestore();
// [START_EXCLUDE]
const settings = {timestampsInSnapshots: true};
db.settings(settings);
// [END_EXCLUDE]

// [START aggregate_function]
exports.aggregateRatings = functions.firestore
Expand Down
1 change: 1 addition & 0 deletions firestore/solution-deletes/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const functions = require('firebase-functions');

admin.initializeApp();


/**
* Callable function that creates a custom auth token with the
* custom attribute "admin" set to true.
Expand Down