Last active
February 12, 2020 09:26
-
-
Save potato4d/0116206c1e40e0653cb966d8d4fe2521 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vue, { CreateElement, VNode } from 'vue' | |
import * as tsx from 'vue-tsx-support' | |
export const AppRequireAuth = tsx.component({ | |
name: 'AppRequireAuth', | |
watch: { | |
async isInitializedAuth() { | |
const user = this.$auth.currentUser | |
if (user) { | |
try { | |
await this.$store.dispatch('fetchUserData', { user }) | |
} catch (e) {} | |
} | |
}, | |
isFetchedUserData(next: boolean) { | |
if (this.isFetchedUserData) { | |
if (this.$store.getters['userData'].createdAt) { | |
if (this.$route.path === '/') { | |
this.$router.push('/user/edit') | |
} | |
} | |
} else { | |
this.$router.push('/') | |
} | |
} | |
}, | |
computed: { | |
isFetchedUserData() { | |
return !!this.$store.getters['userData'] | |
}, | |
isInitializedAuth() { | |
return this.$auth.currentUser !== undefined | |
} | |
}, | |
render(h: CreateElement): VNode { | |
if (!this.isInitializedAuth || !this.$auth.currentUser) { | |
return this.$slots.fallback as any | |
} | |
if (!this.isFetchedUserData) { | |
return this.$slots.fallback as any | |
} | |
return this.$slots.completed as any | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment