Skip to content

Instantly share code, notes, and snippets.

@nriesco
Last active November 8, 2018 14:42
Show Gist options
  • Save nriesco/abbfe784410bc0dabf5074b5d66a2025 to your computer and use it in GitHub Desktop.
Save nriesco/abbfe784410bc0dabf5074b5d66a2025 to your computer and use it in GitHub Desktop.
Detecting logged users
<template>
<div v-for="user in users" v-bind:key="item._id">
{{ user.email }}
</div>
</template>
<script>
import { mapActions } from 'vuex'
const REDIRECT_PATH = '/some/url/to/login';
export default {
name: 'login-example',
data () {
return {
users: []
}
},
mounted: function () {
this.loadItems()
},
methods: {
...mapActions('users', {
findItems: 'find'
}),
loadItems () {
this.findItems({})
.then(res => {
if (res.data.length) {
this.items = res.data
} else {
this.$router.push({ path: REDIRECT_PATH })
}
})
.catch(() => {
this.$router.push({ path: REDIRECT_PATH })
})
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment