Skip to content

Commit 86618be

Browse files
committed
Fixing Indentation
Fixing indentation of then() so it matches for all snippets
1 parent 28a9c42 commit 86618be

File tree

3 files changed

+107
-86
lines changed

3 files changed

+107
-86
lines changed

auth/import_users.js

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ admin.auth().importUsers([{
5959
// Must be provided in a byte buffer.
6060
key: Buffer.from('secret')
6161
}
62-
}).then(function(results) {
63-
results.errors.forEach(function(indexedError) {
64-
console.log('Error importing user ' + indexedError.index);
62+
})
63+
.then(function(results) {
64+
results.errors.forEach(function(indexedError) {
65+
console.log('Error importing user ' + indexedError.index);
66+
});
67+
})
68+
.catch(function(error) {
69+
console.log('Error importing users:', error);
6570
});
66-
}).catch(function(error) {
67-
console.log('Error importing users:', error);
68-
});
6971
// [END import_with_hmac]
7072

7173
// [START import_with_pbkdf]
@@ -81,13 +83,15 @@ admin.auth().importUsers([{
8183
algorithm: 'PBKDF2_SHA256',
8284
rounds: 100000
8385
}
84-
}).then(function(results) {
85-
results.errors.forEach(function(indexedError) {
86-
console.log('Error importing user ' + indexedError.index);
86+
})
87+
.then(function(results) {
88+
results.errors.forEach(function(indexedError) {
89+
console.log('Error importing user ' + indexedError.index);
90+
});
91+
})
92+
.catch(function(error) {
93+
console.log('Error importing users:', error);
8794
});
88-
}).catch(function(error) {
89-
console.log('Error importing users:', error);
90-
});
9195
// [END import_with_pbkdf]
9296

9397
// [START import_with_standard_scrypt]
@@ -106,13 +110,15 @@ admin.auth().importUsers([{
106110
blockSize: 8,
107111
derivedKeyLength: 64
108112
}
109-
}).then(function(results) {
110-
results.errors.forEach(function(indexedError) {
111-
console.log('Error importing user ' + indexedError.index);
113+
})
114+
.then(function(results) {
115+
results.errors.forEach(function(indexedError) {
116+
console.log('Error importing user ' + indexedError.index);
117+
});
118+
})
119+
.catch(function(error) {
120+
console.log('Error importing users:', error);
112121
});
113-
}).catch(function(error) {
114-
console.log('Error importing users:', error);
115-
});
116122
// [END import_with_standard_scrypt]
117123

118124
// [START import_with_bcrypt]
@@ -125,13 +131,15 @@ admin.auth().importUsers([{
125131
hash: {
126132
algorithm: 'BCRYPT'
127133
}
128-
}).then(function(results) {
129-
results.errors.forEach(function(indexedError) {
130-
console.log('Error importing user ' + indexedError.index);
134+
})
135+
.then(function(results) {
136+
results.errors.forEach(function(indexedError) {
137+
console.log('Error importing user ' + indexedError.index);
138+
});
139+
})
140+
.catch(function(error) {
141+
console.log('Error importing users:', error);
131142
});
132-
}).catch(function(error) {
133-
console.log('Error importing users:', error);
134-
});
135143
// [END import_with_bcrypt]
136144

137145

@@ -153,13 +161,15 @@ admin.auth().importUsers([{
153161
rounds: 8,
154162
memoryCost: 14
155163
}
156-
}).then(function(results) {
157-
results.errors.forEach(function(indexedError) {
158-
console.log('Error importing user ' + indexedError.index);
164+
})
165+
.then(function(results) {
166+
results.errors.forEach(function(indexedError) {
167+
console.log('Error importing user ' + indexedError.index);
168+
});
169+
})
170+
.catch(function(error) {
171+
console.log('Error importing users:', error);
159172
});
160-
}).catch(function(error) {
161-
console.log('Error importing users:', error);
162-
});
163173
// [END import_with_scrypt]
164174

165175
// [START import_without_password]
@@ -180,13 +190,15 @@ admin.auth().importUsers([{
180190
photoURL: 'http://www.example.com/12345678/photo.png',
181191
providerId: 'google.com'
182192
}]
183-
}]).then(function(results) {
184-
results.errors.forEach(function(indexedError) {
185-
console.log('Error importing user ' + indexedError.index);
193+
}])
194+
.then(function(results) {
195+
results.errors.forEach(function(indexedError) {
196+
console.log('Error importing user ' + indexedError.index);
197+
});
198+
})
199+
.catch(function(error) {
200+
console.log('Error importing users:', error);
186201
});
187-
}).catch(function(error) {
188-
console.log('Error importing users:', error);
189-
});
190202
// [END import_without_password]
191203

192204

auth/manage_cookies.js

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ app.post('/sessionLogin', (req, res) => {
2020
// The session cookie will have the same claims as the ID token.
2121
// To only allow session cookie setting on recent sign-in, auth_time in ID token
2222
// can be checked to ensure user was recently signed in before creating a session cookie.
23-
admin.auth().createSessionCookie(idToken, {expiresIn}).then((sessionCookie) => {
24-
// Set cookie policy for session cookie.
25-
const options = {maxAge: expiresIn, httpOnly: true, secure: true};
26-
res.cookie('session', sessionCookie, options);
27-
res.end(JSON.stringify({status: 'success'}));
28-
}, error => {
29-
res.status(401).send('UNAUTHORIZED REQUEST!');
30-
});
23+
admin.auth().createSessionCookie(idToken, {expiresIn})
24+
.then((sessionCookie) => {
25+
// Set cookie policy for session cookie.
26+
const options = {maxAge: expiresIn, httpOnly: true, secure: true};
27+
res.cookie('session', sessionCookie, options);
28+
res.end(JSON.stringify({status: 'success'}));
29+
}, error => {
30+
res.status(401).send('UNAUTHORIZED REQUEST!');
31+
});
3132
});
3233
// [END session_login]
3334

@@ -37,16 +38,17 @@ app.post('/verifyToken', (req, res) => {
3738
// Set session expiration to 5 days.
3839
const expiresIn = 60 * 60 * 24 * 5 * 1000;
3940
// [START check_auth_time]
40-
admin.auth().verifyIdToken(idToken).then((decodedIdToken) => {
41-
// Only process if the user just signed in in the last 5 minutes.
42-
if (new Date().getTime() / 1000 - decodedIdToken.auth_time < 5 * 60) {
43-
// Create session cookie and set it.
44-
return admin.auth().createSessionCookie(idToken, {expiresIn});
45-
}
46-
// A user that was not recently signed in is trying to set a session cookie.
47-
// To guard against ID token theft, require re-authentication.
48-
res.status(401).send('Recent sign in required!');
49-
});
41+
admin.auth().verifyIdToken(idToken)
42+
.then((decodedIdToken) => {
43+
// Only process if the user just signed in in the last 5 minutes.
44+
if (new Date().getTime() / 1000 - decodedIdToken.auth_time < 5 * 60) {
45+
// Create session cookie and set it.
46+
return admin.auth().createSessionCookie(idToken, {expiresIn});
47+
}
48+
// A user that was not recently signed in is trying to set a session cookie.
49+
// To guard against ID token theft, require re-authentication.
50+
res.status(401).send('Recent sign in required!');
51+
});
5052
// [END check_auth_time]
5153
});
5254

@@ -57,28 +59,32 @@ app.post('/profile', (req, res) => {
5759
// Verify the session cookie. In this case an additional check is added to detect
5860
// if the user's Firebase session was revoked, user deleted/disabled, etc.
5961
admin.auth().verifySessionCookie(
60-
sessionCookie, true /** checkRevoked */).then((decodedClaims) => {
61-
serveContentForUser('/profile', req, res, decodedClaims);
62-
}).catch(error => {
63-
// Session cookie is unavailable or invalid. Force user to login.
64-
res.redirect('/login');
65-
});
62+
sessionCookie, true /** checkRevoked */)
63+
.then((decodedClaims) => {
64+
serveContentForUser('/profile', req, res, decodedClaims);
65+
})
66+
.catch(error => {
67+
// Session cookie is unavailable or invalid. Force user to login.
68+
res.redirect('/login');
69+
});
6670
});
6771
// [END session_verify]
6872

6973
app.post('/verifySessionCookie', (req, res) => {
7074
const sessionCookie = req.cookies.session || '';
7175
// [START session_verify_with_permission_check]
72-
admin.auth().verifySessionCookie(sessionCookie, true).then((decodedClaims) => {
73-
// Check custom claims to confirm user is an admin.
74-
if (decodedClaims.admin === true) {
75-
return serveContentForAdmin('/admin', req, res, decodedClaims);
76-
}
77-
res.status(401).send('UNAUTHORIZED REQUEST!');
78-
}).catch(error => {
79-
// Session cookie is unavailable or invalid. Force user to login.
80-
res.redirect('/login');
81-
});
76+
admin.auth().verifySessionCookie(sessionCookie, true)
77+
.then((decodedClaims) => {
78+
// Check custom claims to confirm user is an admin.
79+
if (decodedClaims.admin === true) {
80+
return serveContentForAdmin('/admin', req, res, decodedClaims);
81+
}
82+
res.status(401).send('UNAUTHORIZED REQUEST!');
83+
})
84+
.catch(error => {
85+
// Session cookie is unavailable or invalid. Force user to login.
86+
res.redirect('/login');
87+
});
8288
// [END session_verify_with_permission_check]
8389
});
8490

@@ -94,13 +100,16 @@ app.post('/sessionLogout', (req, res) => {
94100
app.post('/sessionLogout', (req, res) => {
95101
const sessionCookie = req.cookies.session || '';
96102
res.clearCookie('session');
97-
admin.auth().verifySessionCookie(sessionCookie).then((decodedClaims) => {
103+
admin.auth().verifySessionCookie(sessionCookie)
104+
.then((decodedClaims) => {
98105
return admin.auth().revokeRefreshTokens(decodedClaims.sub);
99-
}).then(() => {
100-
res.redirect('/login');
101-
}).catch((error) => {
106+
})
107+
.then(() => {
102108
res.redirect('/login');
103-
});
109+
})
110+
.catch((error) => {
111+
res.redirect('/login');
112+
});
104113
});
105114
// [END session_clear_and_revoke]
106115

auth/manage_sessions.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ admin.initializeApp();
66
// Revoke all refresh tokens for a specified user for whatever reason.
77
// Retrieve the timestamp of the revocation, in seconds since the epoch.
88
admin.auth().revokeRefreshTokens(uid)
9-
.then(() => {
10-
return admin.auth().getUser(uid);
11-
})
12-
.then((userRecord) => {
13-
return new Date(userRecord.tokensValidAfterTime).getTime() / 1000;
14-
})
15-
.then((timestamp) => {
16-
console.log('Tokens revoked at: ', timestamp);
9+
.then(() => {
10+
return admin.auth().getUser(uid);
11+
})
12+
.then((userRecord) => {
13+
return new Date(userRecord.tokensValidAfterTime).getTime() / 1000;
14+
})
15+
.then((timestamp) => {
16+
console.log('Tokens revoked at: ', timestamp);
1717
});
1818
// [END revoke_tokens]
1919

2020
// [START save_revocation_in_db]
2121
const metadataRef = admin.database().ref('metadata/' + uid);
2222
metadataRef.set({revokeTime: utcRevocationTimeSecs})
23-
.then(() => {
24-
console.log('Database updated successfully.');
25-
});
23+
.then(() => {
24+
console.log('Database updated successfully.');
25+
});
2626
// [END save_revocation_in_db]
2727

2828
// [START verify_id_token_check_revoked]

0 commit comments

Comments
 (0)