Back and Front: User Login

This commit is contained in:
Kentai Radiquum 2024-04-23 23:59:24 +05:00
parent a03deddbc0
commit 32a4da1a31
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
8 changed files with 141 additions and 36 deletions

View file

@ -7,20 +7,24 @@ export const useUserStore = create((set, get) => ({
isAuth: false,
user: null,
token: null,
login: (user, token) => {
login: (user, token, user_id) => {
set({ isAuth: true, user, token });
setJWT(token);
setJWT(token, user_id);
},
logout: () => {
set({ isAuth: false, user: null, token: null });
removeJWT();
},
checkAuth: async (user_id) => {
checkAuth: async () => {
const jwt = getJWT();
if (jwt) {
const me = await getMe(`${endpoints.profile}/${user_id}`, jwt);
const me = await getMe(
`${endpoints.user.profile}/${jwt.user_id}`,
jwt.jwt,
);
if (me.is_my_profile) {
get().login(me, jwt);
get().login(me, jwt.jwt, jwt.user_id);
} else {
get().logout();
}