Created
September 22, 2025 14:18
-
-
Save maximvl/593b16e088481a990855f7a3d47e4fe9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { goto } from '$app/navigation' | |
| import { EventlabBaseUrl } from '$lib/client' | |
| import { | |
| fetchCurrentUserApiUsersCurrentGetOptions, | |
| loginApiLoginPostMutation | |
| } from '$lib/heyapi/@tanstack/svelte-query.gen' | |
| import { createMutation, createQuery } from '@tanstack/svelte-query' | |
| import { derived, get } from 'svelte/store' | |
| export function createMyUserStore() { | |
| const myUserQuery = createQuery({ | |
| ...fetchCurrentUserApiUsersCurrentGetOptions({ | |
| baseUrl: EventlabBaseUrl, | |
| auth: () => localStorage.getItem('auth_token') ?? undefined | |
| }), | |
| retry: false | |
| }) | |
| const loginMutation = createMutation({ | |
| ...loginApiLoginPostMutation({ baseUrl: EventlabBaseUrl }) | |
| }) | |
| const myUser = derived(myUserQuery, ($query) => { | |
| if ($query.isSuccess) { | |
| return $query.data | |
| } | |
| return null | |
| }) | |
| const isLoading = derived(myUserQuery, ($query) => $query.isLoading) | |
| const isAdmin = derived(myUser, ($myUser) => $myUser?.role === 'admin') | |
| const isPlayer = derived(myUser, ($myUser) => $myUser?.role === 'streamer') | |
| const isModerator = derived(myUser, ($myUser) => false) | |
| const login = (username: string, password: string) => { | |
| get(loginMutation) | |
| .mutateAsync({ body: { username, password } }) | |
| .then((response) => { | |
| if (response.token) { | |
| localStorage.setItem('auth_token', response.token) | |
| } | |
| get(myUserQuery) | |
| .refetch() | |
| .then(() => { | |
| goto('/') | |
| }) | |
| }) | |
| } | |
| const logout = () => { | |
| localStorage.removeItem('auth_token') | |
| get(myUserQuery).refetch() | |
| } | |
| return { query: myUserQuery, myUser, isLoading, isAdmin, isPlayer, isModerator, login, logout } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment