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
useFirestore for docs, collections or queries that include an auth users id in the path #2018
Comments
Something came to mind, so I'll post it here. The above doesn't account for converter usage. Perhaps |
In my opinion,
seems too complicated interface. There are too many patterns and hard to handle them. It's better to keep it simple, just the same interface as |
Thanks for the reply. I agree it feels complicated looking at the array arg possibility. Initial thought was to have something that was identifiable for the
Initially |
I'm not native of English so not completely clear of your reply. but the below interface is possible and maybe what you want. const todos = useFirestore(collection(db, 'todos')) // works as usual
const todo = useFirestore(doc(db, 'todos', 'todo-id')) // works as usual
const postsQuery = computed(() => query( /* ... */ ))
const posts = useFirestore(postsQuery) // works as usual
const { user, isAuthenticated } = useAuth()
// new interface
const todos = useFirestore(() => collection(db, 'todos')) // same as passing directly collection reference
const userData = useFirestore(() => isAuthenticated.value && doc(db, 'users', user.value.id), null)
// isAuthenticated is false: return null (initial value)
// isAuthenticated is true: bind snapshot
|
Yes, thank you, that new interface looks correct. The one additional request is that in the example below,
The flow would be:
Please let me know if this makes sense and thank you again. |
Yes, const userTodosData = useFirestore(() => isAuthenticated.value && collection(db, 'users', user.value.id, 'todos'), []) probably this also can set the initial value again when 1st arg gets falsy value without |
Clear and concise description of the problem
Currently
useFirestore
expects to be given a doc, collection, query or reactive that supplies one of these. It would be nice if it were possible to useuseFirestore
for any of these that are built using an authenticated user id as part of the path without concern for if the user has let authenticated or not. Since this user id is not known until authenticated, currently additional checking must be used to wait until the user has authenticated to calldoc(db, 'users', 'my-user-id')
orcollection(db, 'users', 'my-user-id', 'another-collection')
. It would be nice to be able to calluseFirestore
at any time and the ref it returns would contain the initial value until the user has authenticated.Suggested solution
It would be nice if
useFirestore
or perhaps a new composableuseAuthFirestore
could be called at anytime and a ref would be provided that has its value set to an initial value until the user has authenticated. This would avoid code in various places waiting on authentication. If the user signed out, the ref value would return to the initial value...likely[]
. This composable would also take either the params similar touseAuth
or perhaps the return value of an already calleduseAuth
. It would be able to tell when authenticated and get the user id from this.Perhaps instead of expecting a doc, collection, query or reactive providing one of these, an array could be provided with the db & portions of the paths. A placeholder for where the user id would go could be provided in the array, perhaps something like:
[db, 'users', null, 'some-collection']
It would be known whether to usecollection
ordoc
based on the number of items in the array. Perhaps to handle the query scenario a reactive function could be provided that would return an array with everything to be used as a parameter toquery
after the firstcollection
ordoc
param....since the composable already knows how to create that portion, for example it might return an array with something like is shown in the docs for a reactive query:[orderBy('createdAt', 'desc'), limit(postLimit.value)]
Alternative
Writing additional conditional code that waits until the user has authenticated before calling
useFirestore
. Another alternative that allows a ref to be used beforeuseFirestore
has been called is something like:Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: