mirror of
https://github.com/Radiquum/AniX.git
synced 2025-04-09 09:44:39 +00:00
31 lines
753 B
JavaScript
31 lines
753 B
JavaScript
"use client";
|
|
import { create } from "zustand";
|
|
import { getJWT, setJWT, removeJWT, getMe } from "@/app/api/api-utils";
|
|
import { endpoints } from "@/app/api/config";
|
|
|
|
export const useUserStore = create((set, get) => ({
|
|
isAuth: false,
|
|
user: null,
|
|
token: null,
|
|
login: (user, token) => {
|
|
set({ isAuth: true, user, token });
|
|
setJWT(token);
|
|
},
|
|
logout: () => {
|
|
set({ isAuth: false, user: null, token: null });
|
|
removeJWT();
|
|
},
|
|
checkAuth: async (user_id) => {
|
|
const jwt = getJWT();
|
|
if (jwt) {
|
|
const me = await getMe(`${endpoints.profile}/${user_id}`, jwt);
|
|
if (me.is_my_profile) {
|
|
get().login(me, jwt);
|
|
} else {
|
|
get().logout();
|
|
}
|
|
} else {
|
|
get().logout();
|
|
}
|
|
},
|
|
}));
|