small bug fix with sockets and auth

This commit is contained in:
Zoe
2023-04-24 20:38:18 -05:00
parent 1c66cb3512
commit 54cd5b8654
3 changed files with 39 additions and 3 deletions

View File

@@ -5,18 +5,42 @@ export const useUserStore = defineStore('userStore', {
user: null as SafeUser | null,
isLoggedIn: false,
}),
getters: {
// computed property that returns a promise that resolves when the user logs in
userLoggedIn() {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const that = this;
return new Promise<boolean>(resolve => {
function checkFlag() {
if(that.isLoggedIn === true) {
resolve(true);
}
setTimeout(checkFlag, 100);
}
checkFlag();
});
},
},
actions: {
setUser(user: SafeUser) {
this.user = user;
this.isLoggedIn = true;
},
async logout() {
const { $io, $emit } = useNuxtApp();
(await $io).disconnect();
await $fetch('/api/user/logout');
useCookie('sessionToken').value = null;
useCookie('userId').value = null;
this.user = null;
this.isLoggedIn = false;
$emit('userLogout', true);
return navigateTo('/login');
}
}