Skip to content

Instantly share code, notes, and snippets.

@moredure
Last active April 28, 2020 13:29
Show Gist options
  • Select an option

  • Save moredure/7d7c1d8a6bb8b074674e659dd6738f52 to your computer and use it in GitHub Desktop.

Select an option

Save moredure/7d7c1d8a6bb8b074674e659dd6738f52 to your computer and use it in GitHub Desktop.
Authentication Across tabs with VueJs and Vuex plugin
import router from '@/router'
const userKey = 'user'
const refreshAction = 'sessions/refresh'
const logoutAction = 'sessions/logout'
const updateMutations = [
'sessions/LOGIN',
'sessions/LOGOUT',
'sessions/SIGNUP'
]
export default function (store) {
if (store.state.sessions[userKey]) {
const data = JSON.stringify(store.state.sessions[userKey])
localStorage.setItem(userKey, data)
} else {
localStorage.removeItem(userKey)
}
const onUser = ({ key, newValue, oldValue }) => {
if (newValue && oldValue === null) {
const value = JSON.parse(newValue)
store.dispatch(refreshAction, value)
router.replace({ name: 'projects' })
} else if (newValue === null && oldValue) {
store.dispatch(logoutAction)
router.replace({ name: 'login' })
} else {
const value = JSON.parse(newValue)
store.dispatch(refreshAction, value)
}
}
const onStorage = (event) => {
if (event.key === userKey) onUser(event)
}
if (window.chrome || window.webkit || window.opera) {
window.addEventListener('storage', onStorage, false)
} else {
document.body.addEventListener('storage', onStorage, false)
}
store.subscribe((mutation, state) => {
if (updateMutations.includes(mutation.type)) {
if (!mutation.payload) {
localStorage.removeItem(userKey)
} else {
const data = JSON.stringify(mutation.payload)
localStorage.setItem(userKey, data)
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment