Skip to content

Instantly share code, notes, and snippets.

@sayhicoelho
Created September 27, 2019 06:54
Show Gist options
  • Save sayhicoelho/42266b739c8b30782d4d074362b8fd0a to your computer and use it in GitHub Desktop.
Save sayhicoelho/42266b739c8b30782d4d074362b8fd0a to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>example-app</title>
<style>
html,
body {
height: 100%;
}
.centered {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
</style>
</head>
<body>
<!-- NOTE: The 'centered' class will be removed after new Vue(...) -->
<div id="app" class="centered">
<img src="/static/loader.svg" alt="Loading...">
</div>
<!-- built files will be auto injected -->
</body>
</html>
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import store from './store'
Vue.config.productionTip = false
function checkAuth() {
return new Promise(resolve => {
// We're getting the current user from localStorage
const user = window.localStorage.getItem('user')
if (user) {
// We're simulating the request
setTimeout(() => {
store.replaceState({ currentUser: JSON.parse(user) })
resolve()
}, 1000)
} else {
resolve()
}
})
}
function createApp() {
/* eslint-disable no-new */
new Vue({
el: '#app',
store,
components: { App },
template: '<App/>'
})
}
checkAuth()
.then(createApp)
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
currentUser: null
},
mutations: {
SET_AUTH(state, user) {
window.localStorage.setItem('user', JSON.stringify(user))
state.currentUser = user
},
REMOVE_AUTH(state) {
window.localStorage.removeItem('user')
state.currentUser = null
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment