Skip to content

Instantly share code, notes, and snippets.

@zhiephie
Last active July 24, 2023 13:48
Show Gist options
  • Save zhiephie/e6c8a67d70f9b1522bf1133703d30c50 to your computer and use it in GitHub Desktop.
Save zhiephie/e6c8a67d70f9b1522bf1133703d30c50 to your computer and use it in GitHub Desktop.
Vue Auto Logout for Laravel
<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>
@zhiephie
Copy link
Author

Use

@auth
<auto-logout></autologout>
@endauth

@MCayirli
Copy link

thanks
line 11 : missing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment