Skip to content
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

simplifying findTutorsByEmail - @aishah #19

Open
rooberrydev opened this issue Oct 27, 2020 · 1 comment
Open

simplifying findTutorsByEmail - @aishah #19

rooberrydev opened this issue Oct 27, 2020 · 1 comment

Comments

@rooberrydev
Copy link

rooberrydev commented Oct 27, 2020

This is quite a bit of work for a minor optimisation, so don't take this as something you have to implement!

Assuming here that a tutor document is the same as the person/email that logs in.

Not sure how tutor documents are being created at the moment.

But if login accounts and tutors are the same email, then when you sign up(create a new user) you might want to create a tutor document with the same uid as the login uid.

const tutorData = {// whatever other properties a tutor document needs}
auth.createUserWithEmailAndPassword(email,password)
  .then((credentials)=>{
    db.collection("Users").add(credentials.user.uid).set({email,...tutorData)

this then means that the uid of a tutor document will be equal to auth.currentUser.uid, so instead of

db
    .collection('tutors')
    .where('email', '==', emailaddress)
   .get()

you can do

db
    .collection('tutors')
    .doc(user.uid)
    .get()

having auth uid as the id for tutor documents will also help in other places, if you have content that is private to a user, you can set the auth uid as a "owner" or "createdBy" key on a document (say a lesson) and then you can write a firebase rule to only allow reading when the logged in user is the same as the user who created the document

allow read,write: if request.auth.uid == resource.createdBy

@rooberrydev
Copy link
Author

btw the .then() chaining is kind of inevitable as this is a promise based workflow, things do not return values immediately.

you can use async-await if you want but it's probably better to keep things as they are so your database read/writes aren't blocking an other code from running.

you can always set state inside a .then() chained on a db call, and then use that state elsewhere in the react component

@rooberrydev rooberrydev changed the title findTutorsbyEmail simplifying findTutorsByEmail Oct 27, 2020
@rooberrydev rooberrydev changed the title simplifying findTutorsByEmail simplifying findTutorsByEmail - @aishah Oct 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant