feat: add authorization!

This commit is contained in:
Kentai Radiquum 2024-07-14 03:01:25 +05:00
parent f3667eb209
commit 3a800a4933
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
7 changed files with 248 additions and 1 deletions

View file

@ -0,0 +1,32 @@
"use client";
import { create } from "zustand";
import { getJWT, setJWT, removeJWT, fetchDataViaGet } from "@/app/api/utils";
export const useUserStore = create((set, get) => ({
isAuth: false,
user: null,
token: null,
login: (user, token) => {
set({ isAuth: true, user, 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, jwt.user_id, jwt.jwt);
} else {
get().logout();
}
} else {
get().logout();
}
},
}));