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?
karma, why are you setting it as re-payload?karma: 0if you don't want to set it as 0 when updating user, simple answer, just don't do it :D