Skip to content

Commit 733e6ed

Browse files
jenpersonsamtstern
authored andcommitted
Adding Node.js Auth Token Claims (#35)
1 parent 20d7b8c commit 733e6ed

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

auth/create_custom_tokens.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
const admin = require('firebase-admin');
3+
4+
// Initialize the Admin app with the default appication credentials
5+
// [START initialize_sdk_with_default_config]
6+
admin.initializeApp();
7+
// [END initialize_sdk_with_default_config]
8+
9+
// Initialize the Admin app by providing a service accoune key
10+
// [START initialize_sdk_with_service_account_id]
11+
admin.initializeApp({
12+
serviceAccountId: '[email protected]',
13+
});
14+
// [END initialize_sdk_with_service_account_id]
15+
16+
// [START custom_token]
17+
var uid = 'some-uid';
18+
19+
admin.auth().createCustomToken(uid)
20+
.then(function(customToken) {
21+
// Send token back to client
22+
})
23+
.catch(function(error) {
24+
console.log('Error creating custom token:', error);
25+
});
26+
// [END custom_token]
27+
28+
// [START custom_token_with_claims]
29+
var userId = 'some-uid';
30+
var additionalClaims = {
31+
premiumAccount: true
32+
};
33+
34+
admin.auth().createCustomToken(userId, additionalClaims)
35+
.then(function(customToken) {
36+
// Send token back to client
37+
})
38+
.catch(function(error) {
39+
console.log('Error creating custom token:', error);
40+
});
41+
// [END custom_token_with_claims]

firestore/main/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ function initializeApp() {
1515
credential: admin.credential.applicationDefault()
1616
});
1717

18-
var db = admin.firestore();
18+
const db = admin.firestore();
19+
// [START_EXCLUDE]
20+
const settings = {timestampsInSnapshots: true};
21+
db.settings(settings);
22+
// [END_EXCLUDE]
1923

2024
// [END initialize_app]
2125
return db;

firestore/solution-aggregation/functions/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ const functions = require('firebase-functions');
22
const admin = require('firebase-admin');
33

44
const db = admin.firestore();
5+
// [START_EXCLUDE]
6+
const settings = {timestampsInSnapshots: true};
7+
db.settings(settings);
8+
// [END_EXCLUDE]
59

610
// [START aggregate_function]
711
exports.aggregateRatings = functions.firestore

firestore/solution-deletes/functions/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const functions = require('firebase-functions');
44

55
admin.initializeApp();
66

7+
78
/**
89
* Callable function that creates a custom auth token with the
910
* custom attribute "admin" set to true.

0 commit comments

Comments
 (0)