Skip to content

Instantly share code, notes, and snippets.

@Mikepicker
Created September 26, 2022 17:58
Show Gist options
  • Select an option

  • Save Mikepicker/eb1bb867cee6bbe9747f7f20c8c826a9 to your computer and use it in GitHub Desktop.

Select an option

Save Mikepicker/eb1bb867cee6bbe9747f7f20c8c826a9 to your computer and use it in GitHub Desktop.
<html>
<head>
...
</head>
<body>
...
<script>
document.addEventListener('alpine:init', () => {
Alpine.data('lists', () => ({
// VARIABLES
async init () {
// initialize pocketbase
this.client = new PocketBase('http://127.0.0.1:8090');
// capture invalid token
this.client.afterSend = function (response, data) {
if (response.status === 401) {
this.showLogin = true
}
return data
}
// if user is not logged in, show login / signup page
if (!window.localStorage.getItem('pocketbase_auth')) {
this.showLogin = true
return
}
// resume session
const auth = JSON.parse(window.localStorage.getItem('pocketbase_auth'))
this.client.authStore.save(auth.token, auth.model)
// fetch todo lists
this.getLists()
// suscribe to live update events
this.subscribeToLists()
},
}))
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment