<template>
    <div>
        <router-view></router-view>
    </div>
</template>

<script>
    export default {

        // Auth stuff
        ready() {
            this.$on('userHasLoggedOut', function () {
                this.destroyLogin()
            })

            this.$on('userHasLoggedIn', function (user) {
                console.log('user has logged in')
                this.setLogin(user)
            })

            var token = localStorage.getItem('jwt-token')
            if (token !== null && token !== 'undefined') {
                var that = this
                client({path: '/users/me'}).then(
                        function (response) {
                            // User has successfully logged in using the token from storage
                            that.setLogin(response.entity.user)
                            // broadcast an event telling our children that the data is ready and views can be rendered
                            that.$broadcast('data-loaded')
                        },
                        function (response) {
                            // Login with our token failed, do some cleanup and redirect if we're on an authenticated route
                            that.destroyLogin()
                        }
                )
            }
        },

        data() {
            return {
                user: null,
                token: null,
                authenticated: false
            }
        },
        methods: {
            setLogin(user) {
                this.user = user;
                this.authenticated = true;
                this.token = localStorage.getItem('jwt-token')
            },
            destroyLogin(user) {
                this.user = null;
                this.token = null;
                this.authenticated = false;
                localStorage.removeItem('jwt-token')
                if(this.$route.auth) this.$route.router.go('/auth/login')
            }
        }
    }
</script>
<template>
    <div>
        <router-view></router-view>
    </div>
</template>

<script>
    export default {

        // Auth stuff
        ready() {
            this.$on('userHasLoggedOut', function () {
                this.destroyLogin()
            })

            this.$on('userHasLoggedIn', function (user) {
                console.log('user has logged in')
                this.setLogin(user)
            })

            var token = localStorage.getItem('jwt-token')
            if (token !== null && token !== 'undefined') {
                var that = this
                client({path: '/users/me'}).then(
                        function (response) {
                            // User has successfully logged in using the token from storage
                            that.setLogin(response.entity.user)
                            // broadcast an event telling our children that the data is ready and views can be rendered
                            that.$broadcast('data-loaded')
                        },
                        function (response) {
                            // Login with our token failed, do some cleanup and redirect if we're on an authenticated route
                            that.destroyLogin()
                        }
                )
            }
        },

        data() {
            return {
                user: null,
                token: null,
                authenticated: false
            }
        },
        methods: {
            setLogin(user) {
                this.user = user;
                this.authenticated = true;
                this.token = localStorage.getItem('jwt-token')
            },
            destroyLogin(user) {
                this.user = null;
                this.token = null;
                this.authenticated = false;
                localStorage.removeItem('jwt-token');
                localStorage.removeItem('lang');
                if(this.$route.auth) this.$route.router.go('/auth/login')
            }
        }
    }
</script>
Remove stored lang-data on logout
Remove stored lang-data on logout
