Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
FedorArbuzov committed Mar 25, 2020
1 parent 3345789 commit b779061
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 37 deletions.
6 changes: 5 additions & 1 deletion src/components/CoursesPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
<script>
import firebase from "firebase";
import getUserInfo from "../utils/getUserInfo";
import { getUserInfo, checkRouter } from "../utils/getUserInfo";
export default {
data() {
Expand Down Expand Up @@ -188,6 +188,10 @@ export default {
},
beforeCreate: function() {
getUserInfo();
firebase.auth().onAuthStateChanged(user => {
this.$store.dispatch("fetchUser", user);
checkRouter();
});
let self = this;
const db = firebase.firestore();
if (this.$store.state.courses.length > 0) {
Expand Down
8 changes: 2 additions & 6 deletions src/components/EnrollCourse.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import firebase from "firebase";
import { mapGetters } from "vuex";
import getUserInfo from "../utils/getUserInfo";
import {getUserInfo, checkRouter } from "../utils/getUserInfo";
export default {
computed: {
Expand All @@ -33,11 +33,7 @@ export default {
getUserInfo();
firebase.auth().onAuthStateChanged(user => {
this.$store.dispatch("fetchUser", user);
console.log(this.$store.state.user.loggedIn); // eslint-disable-line no-console
if (!this.$store.state.user.loggedIn) {
this.$router.push({ path: "/login" });
return;
}
checkRouter();
let self = this;
const db = firebase.firestore();
let course = db
Expand Down
12 changes: 12 additions & 0 deletions src/components/FeedbackPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@ div {
</style>

<script>
import firebase from "firebase";
import Feedback from "./Feedback";
import { getUserInfo, checkRouter } from "../utils/getUserInfo";
export default {
components: {
Feedback
},
beforeCreate: function() {
getUserInfo();
firebase.auth().onAuthStateChanged(user => {
this.$store.dispatch("fetchUser", user);
checkRouter();
});
}
};
</script>
12 changes: 11 additions & 1 deletion src/components/ForgetPassword.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<input type="email" name="email" id="email" v-model="form.email" />
</div>
<div style="text-align:center">
<input type="submit" class="btn" value="Отправить" /><br />
<input type="submit" class="btn" value="Отправить" />
<br />
</div>
<div class="reset-password">
<router-link to="/login">Вспомнили пароль?</router-link>
Expand Down Expand Up @@ -173,6 +174,8 @@ import firebase from "firebase";
import modal from "./Modal";
import { getUserInfo, checkRouter } from "../utils/getUserInfo";
export default {
components: {
modal
Expand Down Expand Up @@ -213,6 +216,13 @@ export default {
self.showModal();
});
}
},
beforeCreate: function() {
getUserInfo();
firebase.auth().onAuthStateChanged(user => {
this.$store.dispatch("fetchUser", user);
checkRouter();
});
}
};
</script>
14 changes: 13 additions & 1 deletion src/components/InDevelopment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,17 @@ p {
</style>

<script>
export default {};
import firebase from "firebase";
import { getUserInfo, checkRouter } from "../utils/getUserInfo";
export default {
beforeCreate: function() {
getUserInfo();
firebase.auth().onAuthStateChanged(user => {
this.$store.dispatch("fetchUser", user);
checkRouter();
});
}
};
</script>
9 changes: 4 additions & 5 deletions src/components/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ import firebase from "firebase";
import modal from "./Modal";
import {getUserInfo, checkRouter } from "../utils/getUserInfo";
export default {
components: {
modal
Expand All @@ -194,7 +196,6 @@ export default {
methods: {
submit() {
let self = this;
console.log(123); // eslint-disable-line no-console
firebase
.auth()
.signInWithEmailAndPassword(this.form.email, this.form.password)
Expand All @@ -217,12 +218,10 @@ export default {
}
},
beforeCreate: function() {
getUserInfo()
firebase.auth().onAuthStateChanged(user => {
this.$store.dispatch("fetchUser", user);
if (this.$store.state.user.loggedIn) {
this.$router.push({ path: "/user/courses" });
return;
}
checkRouter();
});
}
};
Expand Down
9 changes: 9 additions & 0 deletions src/components/Register.vue
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@
import firebase from "firebase";
import modal from "./Modal";
import {getUserInfo, checkRouter } from "../utils/getUserInfo";
export default {
components: {
modal
Expand Down Expand Up @@ -254,6 +256,13 @@ export default {
closeModal() {
this.isModalVisible = false;
}
},
beforeCreate: function() {
getUserInfo()
firebase.auth().onAuthStateChanged(user => {
this.$store.dispatch("fetchUser", user);
checkRouter();
});
}
};
</script>
14 changes: 13 additions & 1 deletion src/components/ReviewsPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,17 @@
</style>

<script>
export default {};
import firebase from "firebase";
import { getUserInfo, checkRouter } from "../utils/getUserInfo";
export default {
beforeCreate: function() {
getUserInfo();
firebase.auth().onAuthStateChanged(user => {
this.$store.dispatch("fetchUser", user);
checkRouter();
});
}
};
</script>
13 changes: 10 additions & 3 deletions src/components/UserCourses.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,24 @@ a {
</style>

<script>
import firebase from "firebase";
import UserInfo from "./UserInfo";
import UserTabs from "./UserTabs";
import { getUserInfo, checkRouter } from "../utils/getUserInfo";
export default {
components: {
UserInfo,
UserTabs
},
beforeCreate: function() {
if (!this.$store.state.user.loggedIn) {
this.$router.push({ path: "/login" });
}
getUserInfo();
firebase.auth().onAuthStateChanged(user => {
this.$store.dispatch("fetchUser", user);
checkRouter();
});
}
};
</script>
8 changes: 4 additions & 4 deletions src/components/UserSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ import UserInfo from "./UserInfo";
import UserTabs from "./UserTabs";
import UserInputs from "./UserInputs";
import { getUserInfo, checkRouter } from "../utils/getUserInfo";
export default {
components: {
UserInfo,
UserTabs,
UserInputs
},
beforeCreate: function() {
getUserInfo();
firebase.auth().onAuthStateChanged(user => {
this.$store.dispatch("fetchUser", user);
if (!this.$store.state.user.loggedIn) {
this.$router.push({ path: "/login" });
return;
}
checkRouter();
});
}
};
Expand Down
28 changes: 13 additions & 15 deletions src/utils/getUserInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@ import firebase from "firebase";
import store from "../store";
import router from "../routes";

function checkRouter() {
let authRoutes = ["Login", "Register", "ForgetPassword"];
let independentRoutes = ["Main", "CoursesPage", "ReviewsPage", "FeedbackPage", "InDevelopment", "DescriptionCourse"];
export function checkRouter() {
let authRoutes = ["Login", "Register", "ForgetPassword"];
let lmsRoutes = ["UserCourses", "UserSettings", "Enroll", "Task"];
if (independentRoutes.includes(router.currentRoute.name)){
return;
}
else if (authRoutes.includes(router.currentRoute.name) && store.user.loggedIn) {
this.$router.push({ name: "CoursesPage" });
}
else if (lmsRoutes.includes(router.currentRoute.name) && !store.user.loggedIn) {
this.$router.push({ name: "Login" });
if (
authRoutes.includes(router.currentRoute.name) &&
store.state.user.loggedIn
) {
router.push({ name: "CoursesPage" });
} else if (
lmsRoutes.includes(router.currentRoute.name) &&
!store.state.user.loggedIn
) {
router.push({ name: "Login" });
}
}

export default function getUserInfo() {
console.log(router.currentRoute); // eslint-disable-line no-console
export function getUserInfo() {
if (store.state.user.getDataFromFirebase) {
checkRouter();
return;
}
firebase.auth().onAuthStateChanged(user => {
Expand All @@ -34,6 +33,5 @@ export default function getUserInfo() {
store.commit("saveUserInfo", doc.data());
});
}
checkRouter();
});
}

0 comments on commit b779061

Please sign in to comment.