Skip to content

fix(firestore): remove deprecated library firestore-gorilla-sessions #5312

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

Conversation

OremGLG
Copy link
Contributor

@OremGLG OremGLG commented May 7, 2025

Description

Add new sample for sessions, this sample has a direct implementation of Firestore for to avoid using the deprecated package firestore-gorilla-sessions package.

Fixes b/403118194

Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.

Follow up from PRs: #5237 and #5287

Checklist

Please merge this PR once approved.

@OremGLG OremGLG marked this pull request as ready for review May 7, 2025 19:26
@product-auto-label product-auto-label bot added the api: firestore Issues related to the Firestore API. label May 7, 2025
@OremGLG OremGLG requested a review from a team as a code owner May 7, 2025 19:26
@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label May 7, 2025
Copy link

snippet-bot bot commented May 7, 2025

Here is the summary of changes.

You are about to add 1 region tag.
You are about to delete 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @OremGLG, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request addresses the deprecation of the firestore-gorilla-sessions library by implementing a direct Firestore session management approach. It includes a new sample demonstrating this implementation, effectively resolving issue b/403118194. The changes primarily involve removing the deprecated library and updating the session handling logic to interact directly with Firestore.

Highlights

  • Dependency Removal: The firestore-gorilla-sessions dependency has been removed from go.mod and go.sum, as it is deprecated.
  • Direct Firestore Implementation: The session management logic in main.go has been updated to directly use Firestore for session storage, eliminating the need for the deprecated library.
  • Cookie-Based Session Handling: The session identifier is now stored in a cookie named session_id, and the session data is retrieved and updated directly in Firestore based on this identifier.
  • Test Update: The test case has been updated to reflect the new session handling mechanism and includes a cleanup function to delete test documents.

Changelog

Click here to see the changelog
  • getting-started/sessions/go.mod
    • Removed github.com/GoogleCloudPlatform/firestore-gorilla-sessions and github.com/gorilla/sessions dependencies.
    • Removed github.com/gorilla/securecookie indirect dependency.
  • getting-started/sessions/go.sum
    • Removed entries related to github.com/GoogleCloudPlatform/firestore-gorilla-sessions and other unused dependencies.
    • Removed a large number of indirect dependencies that are no longer needed.
  • getting-started/sessions/main.go
    • Updated copyright year from 2019 to 2025.
    • Removed import of firestoregorilla and gorilla/sessions.
    • Introduced session struct to store session data.
    • Modified newApp function to accept projectID and collectionID and return an app struct directly.
    • Implemented cookie-based session handling in the index function.
    • Implemented direct Firestore interaction for session retrieval, creation, and update.
  • getting-started/sessions/main_test.go
    • Updated copyright year from 2019 to 2025.
    • Updated TestIndex to align with the new session handling mechanism.
    • Added cleanup function to delete test documents after the test.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


The session's ID,
A cookie's gentle guide,
Firestore's embrace warm.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request removes the deprecated firestore-gorilla-sessions library and implements a direct Firestore session management. The changes include updating dependencies in go.mod and go.sum, modifying the main application logic to handle sessions directly with Firestore, and updating the test file to reflect these changes. Overall, the changes seem well-structured and address the issue of using a deprecated library.

Summary of Findings

  • Error Handling in index function: The index function in main.go contains multiple error handling blocks that use log.Printf followed by http.Error. It would be more efficient to create a helper function to handle these errors consistently.
  • Missing License Header Check in Test File: The test file main_test.go does not have a check for the GOLANG_SAMPLES_FIRESTORE_PROJECT environment variable before proceeding with the tests. This could lead to unexpected behavior or failures if the environment variable is not set.
  • Collection Cleanup: The cleanup function in main_test.go iterates through all documents in the test collection and deletes them. This could be slow and inefficient for large collections. Consider using a more efficient method for cleaning up the test data.

Merge Readiness

The pull request is almost ready for merging. However, I recommend addressing the error handling consistency in the index function and ensuring that the test environment is properly set up before merging. I am unable to directly approve this pull request, and other reviewers should review and approve this code before merging.

Comment on lines +151 to +154
if err != nil {
log.Printf("doc.Get error: %v", err)
http.Error(w, "Error getting session", http.StatusInternalServerError)
return

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider creating a helper function to handle Firestore errors consistently. This will reduce code duplication and improve maintainability.

handleFirestoreError(w, "doc.Get error", err)

// Helper function
func handleFirestoreError(w http.ResponseWriter, message string, err error) {
	log.Printf("%s: %v", message, err)
	http.Error(w, "Error getting session", http.StatusInternalServerError)
}

func TestIndex(t *testing.T) {

projectID := os.Getenv("GOLANG_SAMPLES_FIRESTORE_PROJECT")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider skipping the test if the GOLANG_SAMPLES_FIRESTORE_PROJECT environment variable is not set, similar to the original implementation.

Suggested change
projectID := os.Getenv("GOLANG_SAMPLES_FIRESTORE_PROJECT")
if projectID == "" {
t.Skip("GOLANG_SAMPLES_FIRESTORE_PROJECT not set")
}

Comment on lines +81 to +95
for {
doc, err := iter.Next()
if err != nil {
// Handle the case where the collection might not exist or other errors
if err.Error() == "iterator ended" {
log.Printf("Collection %s cleaned up or did not exist.", collectionID)
return
}
log.Printf("Error iterating documents in %s: %v", collectionID, err)
return
}
_, err = doc.Ref.Delete(ctx)
if err != nil {
log.Printf("Error deleting document %s in %s: %v", doc.Ref.ID, collectionID, err)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding a timeout to the cleanup process to prevent indefinite hanging in case of issues with Firestore. Also, consider using DeleteAll if available for more efficient cleanup.

// Add a timeout to the cleanup process
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()

iter := client.Collection(collectionID).Documents(ctx)

for {
	doc, err := iter.Next()
	if err != nil {
		// Handle the case where the collection might not exist or other errors
		if err == iterator.Done {
			log.Printf("Collection %s cleaned up or did not exist.", collectionID)
			return
		}
		log.Printf("Error iterating documents in %s: %v", collectionID, err)
		return
	}
	_, err = doc.Ref.Delete(ctx)
	if err != nil {
		log.Printf("Error deleting document %s in %s: %v", doc.Ref.ID, collectionID, err)
	}
}

@telpirion telpirion enabled auto-merge (squash) May 7, 2025 19:36
@telpirion telpirion merged commit fab9353 into GoogleCloudPlatform:main May 7, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: firestore Issues related to the Firestore API. samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants