front & back: getting ready for user auth

This commit is contained in:
Kentai Radiquum 2024-04-21 02:09:40 +05:00
parent 732799703d
commit 17b5693f34
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
4 changed files with 91 additions and 14 deletions

View file

@ -0,0 +1,31 @@
"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();
}
},
}));