I'm using Firestore and Nuxt for my project.
I want my application to redirect the pages based on their user type when they are logged in.
I want if userType = contractor, when login it will redirect to /WPC/DashboardWPC. I want if userType = subcontractor, when login it will redirect to /Form/SectionA.
But currently, when logging in, it will always redirect to /Form/SectionA. I don't want that. I want it to log to different pages based on their user type.
Any advice?
Login() {
let credentials = {
email: this.email,
password: this.password,
};
this.$store
.dispatch("user/Login", credentials)
.then(() => {
const users = firestore.collection('Users')
users.get().then(() => {
if (firestore.collection('Users').where("userType", "==", "subcontractor")) {
window.location = "/Form/SectionA";
} else if (firestore.collection('Users').where("userType", "==", "contractor")) {
//console.log(snapshot.docs[0].data())
window.location = "/WPC/DashboardWPC";
} else {
window.location = "/BWG/DashboardBWG";
}
})
}).catch((err) => {
alert(err.message)
})
}
I am unsure how are you allowing your users to log in, but the signInWithEmailAndPassword is the method Firebase provides.
I have no experience with nuxt.js
but something like this should give you a general idea:
Login() {
firebase.auth().signInWithEmailAndPassword(this.email, this.password)
.then((user) => {
const _userDoc = firestore.collection('Users').doc(user.uid).get();
const userType = _userDoc.data['userType'];
if(userType == "subcontractor"){
window.location = "/Form/SectionA";
}
else if(userType == "contractor"){
window.location = "/WPC/DashboardWPC";
}
else{
window.location = "/BWG/DashboardBWG";
}
},
(err) => {
alert(err.message);
},
);
}
Also, for registering your users you can use the createUserWithEmailAndPassword method.