Last active
July 24, 2023 13:48
-
-
Save zhiephie/e6c8a67d70f9b1522bf1133703d30c50 to your computer and use it in GitHub Desktop.
Vue Auto Logout for Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div v-if="warningZone">Are you still with us?</div> | |
</template> | |
<script> | |
export default { | |
name: 'AutoLogout', | |
data: function () { | |
return { | |
events ['click', 'mousemove', 'mousedown', 'scroll', 'keypress', 'load'], | |
warningTimer: null, | |
logoutTimer: null, | |
warningZone: false, | |
} | |
}, | |
mounted() { | |
this.events.forEach(function (event) { | |
window.addEventListener(event, this.resetTimer); | |
}, this); | |
this.setTimers(); | |
}, | |
destroyed() { | |
this.events.forEach(function (event) { | |
window.removeEventListener(event, this.resetTimer); | |
}, this); | |
this.resetTimer(); | |
}, | |
methods: { | |
setTimers: function() { | |
this.warningTimer = setTimeout(this.warningMessage, 4 * 1000); // 14 minutes - 14 * 60 * 1000 | |
this.logoutTimer = setTimeout(this.logoutUser, 15 * 1000); // 15 minutes - 15 * 60 * 1000 | |
this.warningZone = false; | |
}, | |
warningMessage: function() { | |
this.warningZone = true; | |
}, | |
logoutUser: function() { | |
// Laravel | |
document.getElementById('logout-form').submit(); | |
}, | |
resetTimer: function() { | |
clearTimeout(this.warningTimer); | |
clearTimeout(this.logoutTimer); | |
this.setTimers(); | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use