2024-12-01 20:54:21 -05:00
|
|
|
import {acceptHMRUpdate, defineStore} from 'pinia'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export interface User {
|
|
|
|
|
name: string,
|
|
|
|
|
sticker_ids: string[],
|
|
|
|
|
username: string | null,
|
2024-12-01 23:59:26 -05:00
|
|
|
}
|
2024-12-01 20:54:21 -05:00
|
|
|
|
|
|
|
|
const anonymous: User = {
|
|
|
|
|
name: 'Anonymous',
|
|
|
|
|
sticker_ids: [],
|
2024-12-01 23:59:26 -05:00
|
|
|
username: 'anonymous',
|
2024-12-01 20:54:21 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export interface State {
|
|
|
|
|
user: User,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const useSessionStore = defineStore('session', {
|
|
|
|
|
actions: {
|
|
|
|
|
async login(username: string, password: string) {
|
|
|
|
|
const response = await fetch(
|
2024-12-01 23:59:26 -05:00
|
|
|
`/api/login`,
|
2024-12-01 20:54:21 -05:00
|
|
|
{
|
|
|
|
|
method: 'POST',
|
2024-12-01 23:59:26 -05:00
|
|
|
body: JSON.stringify({username, password}),
|
2024-12-01 20:54:21 -05:00
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
const json = await response.json();
|
|
|
|
|
console.log('login', json);
|
|
|
|
|
// this.user.name = json.name;
|
|
|
|
|
this.user = {
|
|
|
|
|
name: json.name,
|
|
|
|
|
sticker_ids: json.sticker_ids,
|
|
|
|
|
username: json.username,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
logout() {
|
|
|
|
|
console.log('logout');
|
|
|
|
|
// TODO: Tell server.
|
|
|
|
|
this.user = anonymous;
|
|
|
|
|
// this.hasChanged = true;
|
|
|
|
|
// this.user.name = 'Anonymous';
|
|
|
|
|
// this.$reset();
|
|
|
|
|
// this.$patch({user: anonymous});
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
getters: {
|
|
|
|
|
initials: (state: State): string => {
|
|
|
|
|
return state.user.name[0] || '';
|
|
|
|
|
},
|
|
|
|
|
isAnonymous: (state: State): boolean => {
|
2024-12-01 23:59:26 -05:00
|
|
|
return state.user.username === 'anonymous';
|
2024-12-01 20:54:21 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
state: (): State => {
|
|
|
|
|
return {
|
|
|
|
|
user: anonymous,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (import.meta.hot) {
|
|
|
|
|
import.meta.hot.accept(acceptHMRUpdate(useSessionStore, import.meta.hot))
|
|
|
|
|
}
|