mirror of
https://github.com/Radiquum/AniX.git
synced 2025-04-30 18:09:40 +05:00
Merge remote-tracking branch 'origin/feat_player'
This commit is contained in:
parent
bb437fe7ca
commit
25e31a7799
62 changed files with 1508 additions and 701 deletions
41
app/store/auth.ts
Normal file
41
app/store/auth.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
"use client";
|
||||
import { create } from "zustand";
|
||||
import { getJWT, removeJWT, fetchDataViaGet } from "#/api/utils";
|
||||
|
||||
interface userState {
|
||||
isAuth: boolean
|
||||
user: Object | null
|
||||
token: string | null
|
||||
login: (user: Object, token: string) => void
|
||||
logout: () => void
|
||||
checkAuth: () => void
|
||||
}
|
||||
|
||||
export const useUserStore = create<userState>((set, get) => ({
|
||||
isAuth: false,
|
||||
user: null,
|
||||
token: null,
|
||||
|
||||
login: (user: Object, token: string) => {
|
||||
set({ isAuth: true, user: user, token: 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.profile, jwt.jwt);
|
||||
} else {
|
||||
get().logout();
|
||||
}
|
||||
} else {
|
||||
get().logout();
|
||||
}
|
||||
},
|
||||
}));
|
Loading…
Add table
Add a link
Reference in a new issue