Wednesday 26 January 2022

Vue.js - How to use Set VUEX values in main.js

 The answer to 'How to use Set VUEX values in main.js' is that you don't ! 


Another niche note from myself for a Google search term that I made that this blog would have save me a bit of time chasing around the houses. 


I highly recommend the YouTube video for solving this and other Vuex issues - https://www.youtube.com/watch?v=oxUyIzDbZts&t=629s


The issue I had to solve was that I'd written some code where I was using 'localStorage' when the value should be stored in VUEX .  This was being done in the main.js page. 



firebase.auth().onAuthStateChanged(async function (user) {
console.log('Auth State Checked')
if (user) {
user.getIdTokenResult().then(idTokenResult => {
if (idTokenResult.claims.admin === true) {
localStorage.adminClaim = true
}
});
}
})


The main thing here is, we can't use Vuex from the main.js page and you can't 'commit' to Vuex from here.  That will done in a VUEX 'action'.  

In store/index.js

export default new Vuex.Store({
state: {
adminRole: false,
},
mutations: {
//syncrous
setAdminRole(state, payload) {
state.adminRole = payload;
}
},
actions: {
//asyncronous
async setAdminRole(state) {
firebase.auth().onAuthStateChanged(async function (user) {
console.log('Auth State Checked')
if (user) {
user.getIdTokenResult().then(idTokenResult => {
console.log('IdTokenResult for user', idTokenResult);
console.log('idTokenResult.claims.admin',idTokenResult.claims.admin);
if (idTokenResult.claims.admin === true) {
state.commit("setAdminRole", true);
}
});
}
})
}
},
modules: {},
getters: {
getAdminRole: state => state.adminRole,
},
});


And then in App.vue we can Dispatch our 'action' like this. 

export default {
mounted(){
this.$store.dispatch("setAdminRole");
}
};


Next up is to call it in the page you want to access that data.  In our case the TopHeader page. 


Note in the video above at 8.40, he talks about timing issues.  Which is why mounted doesn’t work and we need to use computed. 



export default {
data() {
return {
},
name: "top-header",
mounted() {
},
computed: {
adminRole(){
return this.$store.getters.getAdminRole;
},
}
};


This will load a data variable 'adminRole' with our Boolean task.  So using 

{{ adminRole }}

in the template, would output either True or False. 







Tuesday 25 January 2022

Firebase Functions Claims setting one makes my other 'claims' undefined !

In my app we have 2 roles that we need to add to the ‘claims’ .

These are

  1. Admin

  2. Coordinator

Which in my Cloud Function I set the claim to either 'true' or 'false' . Which is 'Add' and 'Remove' respectively.


However I was noticing that when setting one it was making the other claim 'undefined' . Here's what one of my functions looked like. exports.addAdminRole = functions.https.onCall((data, context) => { 3 return admin.auth().getUserByEmail(data.email).then(user => { 4 return admin.auth().setCustomUserClaims(user.uid, { 5 admin: true, 7 }); 8 }).then(() => { 10 return { 11 message: `Success! ${data.email} has been made an admin` 12 } 13 }).catch(err => { 14 return err; 15 }) 16})

To fix this issue I had to pass though instructions on what the other Roles claim should be.
I'm using Vue.js by the way. So, the other role is set in the 'data'. Which I can pick up and pass it through like this
removeAdminRole({email: this.email, coordinator_role: this.coordinator_role}).then(() => {
this.successMessage = "The admin role has been removed !";
})
And then Cloud function will look like this.
exports.addAdminRole = functions.https.onCall((data, context) => {
return admin.auth().getUserByEmail(data.email).then(user => {
return admin.auth().setCustomUserClaims(user.uid, {
admin: true,
coordinator: data.coordinator_role
});
}).then(() => {
return {
message: `Success! ${data.email} has been made an admin`
}
}).catch(err => {
return err;
})
})


This now works as it should for me.

Tuesday 18 January 2022

Firebase Functions - Callable Function returning 'CORS' error.

firebase function has been blocked by CORS policy 


I experienced this issue while following a tutorial by The Net Ninja - Firebase Functions


function/index.js

1exports.sayHello = functions.https.onCall((data, context) => { 2 return `Yo dudes and dudettes`; 3})


DEPLOY this to Google .

And then in our button action ( fired by javascript )

1const sayHello = firebase.functions().httpsCallable('sayHello'); 2 sayHello().then(result => { 3 console.log(result.data); 4 });

 

I’d then get a CORS issue firebase function has been blocked by CORS policy

The following call to assign the region worked for me in the Google Function itself.

exports.sayHello = functions.region('us-central1').https.onCall((data, context) => {
return `Yo dudes and dudettes`;
})


Thursday 6 January 2022

Google Data Studio ( GDS ) - Removing 'No data' from Scorecard Display

On my Scorecards where I have single values being pulled in, it is showing the text 'No data' underneath the value.  Like this. 



To remove that it's pretty simple.  Click on 'Style' on the 'Scorecard' editor. 

You then have a dropdown for 'Missing Data' 

Here select 'Show * ( blank ) ' 

That's it :)