Skip to content

Commit e20efdc

Browse files
Improve Firebase authentication and cookie handling (#14603)
* Improve Firebase authentication and cookie handling Fix Firebase Auth UI race condition in App Engine tutorial * Update appengine/standard_python3/building-an-app/building-an-app-3/static/script.js Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update appengine/standard_python3/building-an-app/building-an-app-3/static/script.js Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 15652be commit e20efdc

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

  • appengine/standard_python3/building-an-app/building-an-app-3/static

‎appengine/standard_python3/building-an-app/building-an-app-3/static/script.js‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* See the License for the specific language governing permissions and
1313
* limitations under the License.
1414
*/
15-
1615
'use strict';
1716

1817
// [START gae_python38_auth_javascript]
@@ -22,6 +21,10 @@ window.addEventListener('load', function () {
2221
firebase.auth().signOut();
2322
};
2423

24+
// Initialize the FirebaseUI Widget outside of onAuthStateChanged
25+
// to avoid multiple instantiation errors when logging out and back in.
26+
var ui = firebaseui.auth.AuthUI.getInstance() || new firebaseui.auth.AuthUI(firebase.auth());
27+
2528
// FirebaseUI config.
2629
var uiConfig = {
2730
signInSuccessUrl: '/',
@@ -34,10 +37,22 @@ window.addEventListener('load', function () {
3437
//firebase.auth.TwitterAuthProvider.PROVIDER_ID,
3538
//firebase.auth.GithubAuthProvider.PROVIDER_ID,
3639
//firebase.auth.PhoneAuthProvider.PROVIDER_ID
37-
3840
],
3941
// Terms of service url.
40-
tosUrl: '<your-tos-url>'
42+
tosUrl: '<your-tos-url>',
43+
// Add callbacks to handle successful sign in before redirecting
44+
callbacks: {
45+
signInSuccessWithAuthResult: function (authResult, redirectUrl) {
46+
// Wait for the token to be fetched before redirecting to ensure the backend gets it.
47+
authResult.user.getIdToken().then(function (token) {
48+
// Set the cookie with a path so it applies globally
49+
document.cookie = "token=" + token + ";path=/";
50+
window.location.assign(redirectUrl || '/');
51+
});
52+
// Return false to prevent Firebase UI from redirecting automatically
53+
return false;
54+
}
55+
}
4156
};
4257

4358
firebase.auth().onAuthStateChanged(function (user) {
@@ -52,19 +67,17 @@ window.addEventListener('load', function () {
5267
// SECURITY NOTE: As cookies can easily be modified, only put the
5368
// token (which is verified server-side) in a cookie; do not add other
5469
// user information.
55-
document.cookie = "token=" + token;
70+
document.cookie = "token=" + token + ";path=/;SameSite=Lax";
5671
});
5772
} else {
5873
// User is signed out.
59-
// Initialize the FirebaseUI Widget using Firebase.
60-
var ui = new firebaseui.auth.AuthUI(firebase.auth());
6174
// Show the Firebase login button.
6275
ui.start('#firebaseui-auth-container', uiConfig);
6376
// Update the login state indicators.
6477
document.getElementById('sign-out').hidden = true;
6578
document.getElementById('login-info').hidden = true;
66-
// Clear the token cookie.
67-
document.cookie = "token=";
79+
// Clear the token cookie safely.
80+
document.cookie = "token=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;SameSite=Lax";
6881
}
6982
}, function (error) {
7083
console.log(error);

0 commit comments

Comments
 (0)