Merge remote-tracking branch 'origin/feat_player'

This commit is contained in:
Kentai Radiquum 2024-07-29 21:39:37 +05:00
parent bb437fe7ca
commit 25e31a7799
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
62 changed files with 1508 additions and 701 deletions

41
app/store/auth.ts Normal file
View file

@ -0,0 +1,41 @@
"use client";
import { create } from "zustand";
import { getJWT, removeJWT, fetchDataViaGet } from "#/api/utils";
interface userState {
isAuth: boolean
user: Object | null
token: string | null
login: (user: Object, token: string) => void
logout: () => void
checkAuth: () => void
}
export const useUserStore = create<userState>((set, get) => ({
isAuth: false,
user: null,
token: null,
login: (user: Object, token: string) => {
set({ isAuth: true, user: user, token: token });
},
logout: () => {
set({ isAuth: false, user: null, token: null });
removeJWT();
},
checkAuth: async () => {
const jwt = getJWT();
if (jwt) {
const data = await fetchDataViaGet(
`/api/profile/${jwt.user_id}?token=${jwt.jwt}`
);
if (data && data.is_my_profile) {
get().login(data.profile, jwt.jwt);
} else {
get().logout();
}
} else {
get().logout();
}
},
}));