In my Angular Project, I'm trying to have a sign in function that signs the user in with google and then updates their user data dependent on whether or not they already have the approved role.
When trying to make this work, neither of my console.log statements fire so I'm not sure what I can be doing to get into those blocks.
async handleSignIn() {
const credential = await this.afAuth.signInWithPopup(new firebase.default.auth.GoogleAuthProvider());
return this.manageUserData(credential.user)
}
private async manageUserData(user: User) {
let data$; // Init Variable
let update$; // Init Variable
const userCollection = this.afs.collection<User[]>('users'); // Sets Path for Data
console.log(user.uid);
data$ = userCollection.doc<User>(user.uid).snapshotChanges().pipe( // Fetches Document
map(action => { // Maps To Be Able To Do Things
const payload = action.payload.data(); // Sets Payload Data as the main `payload` variable
console.log("In place", payload)
if(payload.roles.approved == true || false) {
console.log("main block")
update$ = {
uid: user.uid,
email: user.email,
displayName: user.displayName,
photoURL: user.photoURL
}
} else {
console.log("else block")
update$ = {
uid: user.uid,
email: user.email,
displayName: user.displayName,
photoURL: user.photoURL,
roles: {
approved: false
}
}
}
const userRef: AngularFirestoreDocument<User> = this.afs.doc(`users/${user.uid}`);
return userRef.set(update$, { merge: true });
})
)
}
The console.log(user.uid) statement returns my firebase UID: rKlGL943eqfaPVWLh2EEUVBoOvg2
Edit: I also tried using the ternary operator here and organizing functions better:
function hasApprovedStatus() {
update$ = {
uid: user.uid,
email: user.email,
displayName: user.displayName,
photoURL: user.photoURL
}
userRef.set(update$, { merge: true });
}
function hasNoApprovedStatus() {
update$ = {
uid: user.uid,
email: user.email,
displayName: user.displayName,
photoURL: user.photoURL,
roles: {
approved: false
}
}
userRef.set(update$, { merge: true })
}
data$ = userCollection.doc<User>(user.uid).snapshotChanges().pipe( // Fetches Document
map(action => action.payload.data().roles.approved ? hasApprovedStatus() : hasNoApprovedStatus())
)
}
console.log(user.uid)right before you use it, and include the updated code and its output in your question? Please also include a screenshot of the document that you are trying to load for this user.rolespart of it.map(action =>only be invoked if there is a document in the result? Since you don't have a matching document, I'm not sure it'll run. The easiest way to check that is to see if theconsole.log("In place", payload)is showing in the output.