0

I've found this video (in code), and made my Auth Service just like this, except my User Interface is like:

interface User {
  uid: string;
  email: string;
  photoURL: string;
  displayName: string;
  status?: string;
  karma?: number;
}

Status should be the typical online/busy/away...etc, and karma would be a number, and the users could rate each other.
So my updateUserData function (where he saves the user's data into Firestore) is like this:

private updateUserData({ uid, email, displayName, photoURL }) {
    const userRef: AngularFirestoreDocument<User> = this.afs.doc(
      `users/${uid}`
    );

    const data: User = {
      uid,
      email,
      displayName,
      photoURL,
      status: 'online',
      karma: 0
    };

    return userRef.set(data, { merge: true });
  }

My problem is that in each sign-in this resets karma to 0.
How should I check the user is already in the Firestore database, and if he is already in, I wouldn't put there the karma: 0

I have tried to put a boolean called "amINew" into the constructor, and if it finds the user data in Firestore it should be false, and else true.

amInew: boolean;

  constructor(
    private afAuth: AngularFireAuth,
    private afs: AngularFirestore,
    private router: Router,
    public ngZone: NgZone
  ) {
    this.user$ = this.afAuth.authState.pipe(
      switchMap(user => {
        if (user) {
          this.amInew = false;
          return this.afs.doc<User>(`users/${user.uid}`).valueChanges();
        } else {
          this.amInew = true;
          return of(null);
        }
      })
    );
  }

Problem with that was that "amInew" was always true, even when I tried it with a user that havent been registered.

How should I?

6
  • if you don't want to change the value of karma, why are you setting it as re-payload? Commented Aug 29, 2019 at 16:44
  • I want to, every user can change every users karma once, by +1 or -1 Commented Aug 29, 2019 at 16:45
  • Okay, but still I don't understand why you are updating it to 0 in the first place if you don't want to ;) Commented Aug 29, 2019 at 16:46
  • well that would look weird if you just look up someones profile and his karma is missing, 0 is still better then nothing, Plus later it would be easier to acces karma, which is already there, then create a new, I guess :D Commented Aug 29, 2019 at 16:48
  • I mean, just why in your code are you doing karma: 0 if you don't want to set it as 0 when updating user, simple answer, just don't do it :D Commented Aug 29, 2019 at 16:51

2 Answers 2

1

Try updating karma like this:

const data: User = {
  uid,
  email,
  displayName,
  photoURL,
  status: 'online',
  karma: currentKarma + 1;
};

And you can check if the value karma to determine first signin.

During registration set the karma to 0

user.karma = 0;

Then on login

if (user.karma === 0) {
    // This means first signin
    user.karma = 1;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can have different functions for actually updating a user and "updating" a user when signing in for the first time. When signing up for the first time you would want to of course set the karma as 0, but when user just signs in, you can call a function to update one or several fields with update. I see that you would probably want to call update on at least the status. So after login (not signing up) just update the status field:

private updateStatus(uid: string) {
  const userRef: AngularFirestoreDocument<User> = this.afs.doc(
    `users/${uid}`
  );
  return userRef.update({status: 'online'});
}

1 Comment

By the way, building a presence service like this, I would still recommend the "old" firebase, i.e Realtime database. It has handy things for this usecase like onDisconnect() which automatically detects when user disconnects. But just how I feel like doing it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.