diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 6054109..39316f1 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -16,7 +16,7 @@ export default { }, computed: { showNavBar() { - return this.$route.name !== 'Login' && this.$route.name !== 'Register'; + return ['Login', 'Register'].indexOf(this.$route.name) === -1; }, }, }; diff --git a/frontend/src/components/Dashboard/index.vue b/frontend/src/components/Dashboard/index.vue index e33a375..e5079e3 100644 --- a/frontend/src/components/Dashboard/index.vue +++ b/frontend/src/components/Dashboard/index.vue @@ -23,5 +23,10 @@ export default { isLoggedIn: 'isLoggedIn', }), }, + mounted() { + if (!this.isLoggedIn) { + this.$router.push('/login'); + } + }, }; diff --git a/frontend/src/components/NavBar/index.vue b/frontend/src/components/NavBar/index.vue index 24facac..bf992d9 100644 --- a/frontend/src/components/NavBar/index.vue +++ b/frontend/src/components/NavBar/index.vue @@ -9,7 +9,7 @@
- +
@@ -28,7 +28,6 @@ export default { computed: { appName: () => process.env.VUE_APP_NAME, ...mapGetters({ - user: 'user', isLoggedIn: 'isLoggedIn', }), }, diff --git a/frontend/src/main.js b/frontend/src/main.js index 63969b1..d002da2 100644 --- a/frontend/src/main.js +++ b/frontend/src/main.js @@ -26,15 +26,6 @@ getAuth().onAuthStateChanged((user) => { if (user) { store.dispatch('setUser', user); store.dispatch('fetchPosts', store.state); - } else { - console.log(router); - if ( - ['Login', 'Register', 'Terms', 'PrivacyPolicy'].indexOf( - router.currentRoute._value.name, - ) === -1 - ) { - router.replace({ name: 'Login' }); - } } }); diff --git a/frontend/src/store/actions/index.js b/frontend/src/store/actions/index.js index 1966838..38e796b 100644 --- a/frontend/src/store/actions/index.js +++ b/frontend/src/store/actions/index.js @@ -3,6 +3,7 @@ import { apiRequest } from './api'; export default { setUser({ commit }, user) { commit('SET_USER', user); + commit('SET_LOGGED_IN', user.email && user.displayName); }, async fetchPosts({ commit }, { startAfter, limit }) { // make get request with bearer token authentication diff --git a/frontend/src/store/actions/index.spec.js b/frontend/src/store/actions/index.spec.js index 8e406c9..488d854 100644 --- a/frontend/src/store/actions/index.spec.js +++ b/frontend/src/store/actions/index.spec.js @@ -45,17 +45,22 @@ describe('actions', () => { it('makes the expected mutations', (done) => { const userFixture = { email: 'john@doe.com', + displayName: 'John Doe', uid: 123, }; testAction( actions.setUser, userFixture, - { user: null }, + { user: { displayName: '', email: '' }, loggedIn: false }, [ { type: 'SET_USER', payload: userFixture, }, + { + type: 'SET_LOGGED_IN', + payload: userFixture.email && userFixture.displayName, + }, ], done, ); diff --git a/frontend/src/store/getters.js b/frontend/src/store/getters.js index 1547379..298204f 100644 --- a/frontend/src/store/getters.js +++ b/frontend/src/store/getters.js @@ -3,7 +3,7 @@ export default { return state.user; }, isLoggedIn(state) { - return state.user !== null; + return state.loggedIn; }, getPosts(state) { return state.posts; diff --git a/frontend/src/store/getters.spec.js b/frontend/src/store/getters.spec.js index 70a45ec..e6b2673 100644 --- a/frontend/src/store/getters.spec.js +++ b/frontend/src/store/getters.spec.js @@ -21,6 +21,7 @@ describe('getters', () => { uid: '123', displayName: 'John Doe', }, + loggedIn: true, }; const result = getters.isLoggedIn(state); @@ -28,7 +29,8 @@ describe('getters', () => { }); it('should return false if user is null', () => { const state = { - user: null, + user: { displayName: '', email: '' }, + loggedIn: false, }; const result = getters.isLoggedIn(state); diff --git a/frontend/src/store/index.js b/frontend/src/store/index.js index f911b29..36c1f1e 100644 --- a/frontend/src/store/index.js +++ b/frontend/src/store/index.js @@ -13,6 +13,7 @@ const store = createStore({ message: null, loadingPosts: false, creatingPost: false, + loggedIn: false, }, getters, mutations, diff --git a/frontend/src/store/mutations.js b/frontend/src/store/mutations.js index c995715..ac52786 100644 --- a/frontend/src/store/mutations.js +++ b/frontend/src/store/mutations.js @@ -2,6 +2,9 @@ export default { SET_USER(state, data) { state.user = data; }, + SET_LOGGED_IN(state, data) { + state.loggedIn = data; + }, SET_POSTS(state, data) { state.posts = data; },