Skip to content

Commit

Permalink
fix: logged in state
Browse files Browse the repository at this point in the history
  • Loading branch information
jkklapp committed Jun 5, 2022
1 parent 9a85f1e commit 873336c
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
},
};
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/components/Dashboard/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@ export default {
isLoggedIn: 'isLoggedIn',
}),
},
mounted() {
if (!this.isLoggedIn) {
this.$router.push('/login');
}
},
};
</script>
3 changes: 1 addition & 2 deletions frontend/src/components/NavBar/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</router-link>
<div class="mx-auto w-100"></div>
<div class="flex items-end md:order-2">
<UserMenu />
<UserMenu v-if="isLoggedIn" />
<ThemeToggle />
</div>
</div>
Expand All @@ -28,7 +28,6 @@ export default {
computed: {
appName: () => process.env.VUE_APP_NAME,
...mapGetters({
user: 'user',
isLoggedIn: 'isLoggedIn',
}),
},
Expand Down
9 changes: 0 additions & 9 deletions frontend/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
}
}
});

Expand Down
1 change: 1 addition & 0 deletions frontend/src/store/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion frontend/src/store/actions/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/store/getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default {
return state.user;
},
isLoggedIn(state) {
return state.user !== null;
return state.loggedIn;
},
getPosts(state) {
return state.posts;
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/store/getters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ describe('getters', () => {
uid: '123',
displayName: 'John Doe',
},
loggedIn: true,
};
const result = getters.isLoggedIn(state);

expect(result).toBe(true);
});
it('should return false if user is null', () => {
const state = {
user: null,
user: { displayName: '', email: '' },
loggedIn: false,
};
const result = getters.isLoggedIn(state);

Expand Down
1 change: 1 addition & 0 deletions frontend/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const store = createStore({
message: null,
loadingPosts: false,
creatingPost: false,
loggedIn: false,
},
getters,
mutations,
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/store/mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down

0 comments on commit 873336c

Please sign in to comment.