Skip to content

Commit

Permalink
fix: logged in state transition between pages
Browse files Browse the repository at this point in the history
  • Loading branch information
jkklapp committed Jun 5, 2022
1 parent 873336c commit 5d1d475
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
22 changes: 22 additions & 0 deletions frontend/src/components/Dashboard/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,30 @@ import { shallowMount } from '@vue/test-utils';
import Dashboard from '.';
import Input from './Input.vue';
import Posts from './Posts.vue';
import { getAuth } from '../../auth';

jest.mock('../../auth');

describe('Dashboard', () => {
let old_env;
beforeEach(() => {
old_env = process.env;
process.env = {
VUE_APP_API_BASE: 'https://my-api.com',
VUE_APP_INPUT_LABEL: 'Fluunk',
VUE_APP_INPUT_CTA_LABEL: 'Fluu',
};
getAuth.mockReturnValue({
onAuthStateChanged: jest.fn(() => ({
email: 'test',
displayName: 'John Doe',
})),
});
});
afterEach(() => {
process.env = old_env;
jest.resetAllMocks();
});
it('displays Input and Posts components when "isLoggedIn" is true', () => {
const wrapper = shallowMount(Dashboard, {
computed: {
Expand Down
14 changes: 11 additions & 3 deletions frontend/src/components/Dashboard/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import Input from './Input';
import Posts from './Posts';
import { mapGetters } from 'vuex';
import { getAuth } from '../../auth';
export default {
components: {
Expand All @@ -24,9 +25,16 @@ export default {
}),
},
mounted() {
if (!this.isLoggedIn) {
this.$router.push('/login');
}
getAuth().onAuthStateChanged((user) => {
if (user) {
this.$store.dispatch('setUser', user);
this.$store.dispatch('fetchPosts', this.$store.state);
} else {
if (this.$router.currentRoute._value.name === 'Dashboard') {
this.$router.replace({ name: 'Login' });
}
}
});
},
};
</script>
8 changes: 7 additions & 1 deletion frontend/src/components/NavBar/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
>
{{ appName }}
</router-link>
<router-link
to="/about"
class="align-center block py-2 px-4 text-sm text-gray-700 hover:bg-gray-100 dark:hover:bg-gray-600 dark:text-gray-200 dark:hover:text-white"
>
About
</router-link>
<div class="mx-auto w-100"></div>
<div class="flex items-end md:order-2">
<UserMenu v-if="isLoggedIn" />
<UserMenu v-show="isLoggedIn" />
<ThemeToggle />
</div>
</div>
Expand Down
8 changes: 0 additions & 8 deletions frontend/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import App from './App.vue';
import store from './store';
import router from './routes/index';
import './index.css';
import { getAuth } from './auth';
import VueToast from 'vue-toast-notification';
import 'vue-toast-notification/dist/theme-default.css';

Expand All @@ -22,13 +21,6 @@ const firebaseConfig = {

firebase.initializeApp(firebaseConfig);

getAuth().onAuthStateChanged((user) => {
if (user) {
store.dispatch('setUser', user);
store.dispatch('fetchPosts', store.state);
}
});

const app = createApp(App);
app.use(store);
app.use(router);
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Register from '../components/Register';
import Dashboard from '../components/Dashboard';
import Terms from '../components/misc/Terms';
import PrivacyPolicy from '../components/misc/PrivacyPolicy';
import About from '../components/misc/About';

const router = createRouter({
mode: 'history',
Expand All @@ -30,6 +31,11 @@ const router = createRouter({
name: 'PrivacyPolicy',
component: PrivacyPolicy,
},
{
path: '/about',
name: 'About',
component: About,
},
{
path: '/',
name: 'Dashboard',
Expand Down

0 comments on commit 5d1d475

Please sign in to comment.