0

I want to conditionally show my authentication modal based on whether a token exists in my Vuex store. It's very straightforward and I have this working almost 100%.

The part I've been spinning my wheels on is that when the token is present, I get a brief flash of the auth modal before the rest of the content is rendered. Ideally, the modal shouldn't render at all because !token is false.

Here's the relevant code:

// layouts/default
<template>
  <div v-if="!loading">
    <Nav :token="token"/>
    <div class="ui container" v-if="token">
      <nuxt/>
    </div>
    <Auth :open="!token"/>
  </div>
</template>

<script>
import { mapState } from "vuex";
import Auth from "@/components/Auth";
import Nav from "@/components/Nav";
export default {
  computed: mapState({
    token: state => state.auth.token,
    loading: state => state.auth === "loading"
  }),
  components: {
    Nav,
    Auth
  },
  beforeCreate() {
    this.$store.dispatch("auth/checkLocalAuth");
  }
};
</script>
// store/auth.js

const store = require("store");

export const state = {
  token: store.get("userToken") || "",
  status: "loading"
};
export const getters = {
  isAuthenticated: state => state.token,
  authStatus: state => state.status
};
export const mutations = {
  // ..
  success: (state, token) => {
    state.status = "success";
    state.token = token;
  },
  //...
};

export const actions = {
  checkLocalAuth: ({ commit }) => {
    const token = store.get("userToken") || "";
    commit("success", token);
  },
  //...
...
// components/Auth.vue
<template>
  <div>
    <sui-modal v-model="open">
    //...
  </div>
</template>

<script>
export default {
  props: {
    open: {
      default: false
    }
  },
  //...

If I explicitly set <Auth :open="false" />, I do not get the modal flash.

  • Seems like there should be a `v-if` on the `Auth` component. Nothing is telling it not to render at the moment, even if you don't see anything at certain times. `` – Dan May 14 '19 at 19:38
  • @Dan see my comment to Andrei below please. Thanks for the reply. – Eric D. Fields May 14 '19 at 19:53
  • What doesn't work? Still flashing or something else? Next I would try `v-if="open"` on the outer `div` in the `Auth` template – Dan May 14 '19 at 20:24
  • Also [`v-cloak`](https://stackoverflow.com/questions/34870926/v-cloak-does-not-work-in-vue-js) – Dan May 14 '19 at 20:31
  • Still flashing. If I clear caches with each render, it's not perceptible 100% of the time. Maybe some odd race condition. It's just strange that a `true` condition is never passed to the v-if or :open param, and the Auth modal defaults to 'false', so… – Eric D. Fields May 14 '19 at 20:32

2 Answers2

1

If token = "", then !token is not false, but true. So this might be causing the issue.

Andrei Gătej
  • 11,116
  • 1
  • 14
  • 31
0

Got it. The issue was ssr related. Once I disabled ssr it's fine.

It's not 100% the right solution if we do want ssr, but its not a requirement for now. Will address if it becomes one!