AuthWall.vue 1.03 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<template>
    <div v-if="$store.state.auth.user.steeringUser" class="tw-w-full">
        <slot></slot>
    </div>

    <div v-else class="tw-w-full tw-text-center">
        {{ $t('loading') }}
    </div>
</template>

<script>
    export default {
        created() {
            const user = this.$store.state.auth.user.steeringUser;

            // We might not have a user at load-time.
            // In this case wait until the store gives us one.
            if (user === null) {
                this.$store.watch(
                    (state) => state.auth.user.steeringUser,
                    (user) => this.redirectToHome(user)
                )

                return;
            }

            this.redirectToHome(user);
        },

        methods: {
            redirectToHome(user) {
                if (!user.is_superuser) {
                    this.$toast.error(this.$t('auth.permissionError'), { position: 'bottom-left' })
                    this.$router.push({ name: 'home' })
                }
            }
        }
    }
</script>