mirror of
https://github.com/Radiquum/AniX.git
synced 2025-05-01 18:39:40 +05:00
front & back: getting ready for user auth
This commit is contained in:
parent
732799703d
commit
17b5693f34
4 changed files with 91 additions and 14 deletions
31
frontend/app/store/user-store.js
Normal file
31
frontend/app/store/user-store.js
Normal 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();
|
||||
}
|
||||
},
|
||||
}));
|
Loading…
Add table
Add a link
Reference in a new issue