Recently, I came across this video (in code), and implemented my Auth Service based on the example provided. However, my User Interface structure is slightly different:
interface User {
uid: string;
email: string;
photoURL: string;
displayName: string;
status?: string;
karma?: number;
}
I intend for 'status' to represent typical online/busy/away statuses, while 'karma' will be a numerical value that allows users to rate each other.
The updateUserData function, responsible for saving the user's data in Firestore, is as follows:
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 dilemma lies in the fact that each time a user signs in, the 'karma' value gets reset to 0.
How can I ensure that the 'karma' value is retained if the user already exists in the Firestore database?
I attempted to use a boolean variable named "amINew" in the constructor, with the idea that it would be set to false if the user data is found in Firestore, and true if not.
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);
}
})
);
}
However, I encountered an issue where the "amINew" variable always remained true, even when testing with a new user registration.
What should be my approach to solve this issue effectively?