mirror of
https://github.com/Buriburizaem0n/admin-frontend-domain.git
synced 2026-05-06 05:38:51 +00:00
Dashboard Redesign (#48)
* feat: add user_template setting * style: header * style: page padding * style: header * feat: header now time * style: login page * feat: nav indicator * style: button inset shadow * style: footer text size * feat: header show login_ip * fix: error toast * fix: frontend_templates setting * fix: lint * feat: pr auto format * chore: auto-fix linting and formatting issues --------- Co-authored-by: hamster1963 <hamster1963@users.noreply.github.com>
This commit is contained in:
+41
-37
@@ -1,51 +1,55 @@
|
||||
import { createContext, useContext, useEffect, useMemo } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useMainStore } from "./useMainStore";
|
||||
import { AuthContextProps } from "@/types";
|
||||
import { getProfile, login as loginRequest } from "@/api/user";
|
||||
import { toast } from "sonner";
|
||||
import { getProfile, login as loginRequest } from "@/api/user"
|
||||
import { AuthContextProps } from "@/types"
|
||||
import { createContext, useContext, useEffect, useMemo } from "react"
|
||||
import { useNavigate } from "react-router-dom"
|
||||
import { toast } from "sonner"
|
||||
|
||||
import { useMainStore } from "./useMainStore"
|
||||
|
||||
const AuthContext = createContext<AuthContextProps>({
|
||||
profile: undefined,
|
||||
login: () => { },
|
||||
logout: () => { },
|
||||
});
|
||||
login: () => {},
|
||||
logout: () => {},
|
||||
})
|
||||
|
||||
export const AuthProvider = ({ children }: {
|
||||
children: React.ReactNode;
|
||||
}) => {
|
||||
const profile = useMainStore(store => store.profile)
|
||||
const setProfile = useMainStore(store => store.setProfile)
|
||||
export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
const profile = useMainStore((store) => store.profile)
|
||||
const setProfile = useMainStore((store) => store.setProfile)
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
;(async () => {
|
||||
try {
|
||||
const user = await getProfile();
|
||||
setProfile(user);
|
||||
} catch (error) {
|
||||
setProfile(undefined);
|
||||
const user = await getProfile()
|
||||
setProfile(user)
|
||||
} catch (error: any) {
|
||||
setProfile(undefined)
|
||||
console.log("Error fetching profile", error)
|
||||
}
|
||||
})();
|
||||
})()
|
||||
}, [])
|
||||
|
||||
const navigate = useNavigate();
|
||||
const navigate = useNavigate()
|
||||
|
||||
const login = async (username: string, password: string) => {
|
||||
try {
|
||||
await loginRequest(username, password);
|
||||
const user = await getProfile();
|
||||
setProfile(user);
|
||||
navigate("/dashboard");
|
||||
await loginRequest(username, password)
|
||||
const user = await getProfile()
|
||||
setProfile(user)
|
||||
navigate("/dashboard")
|
||||
} catch (error: any) {
|
||||
toast(error.message);
|
||||
toast(error.message)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const logout = () => {
|
||||
document.cookie.split(";").forEach(function (c) { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); });
|
||||
setProfile(undefined);
|
||||
navigate("/dashboard/login", { replace: true });
|
||||
};
|
||||
document.cookie.split(";").forEach(function (c) {
|
||||
document.cookie = c
|
||||
.replace(/^ +/, "")
|
||||
.replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/")
|
||||
})
|
||||
setProfile(undefined)
|
||||
navigate("/dashboard/login", { replace: true })
|
||||
}
|
||||
|
||||
const value = useMemo(
|
||||
() => ({
|
||||
@@ -53,11 +57,11 @@ export const AuthProvider = ({ children }: {
|
||||
login,
|
||||
logout,
|
||||
}),
|
||||
[profile]
|
||||
);
|
||||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
||||
};
|
||||
[profile],
|
||||
)
|
||||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>
|
||||
}
|
||||
|
||||
export const useAuth = () => {
|
||||
return useContext(AuthContext);
|
||||
};
|
||||
return useContext(AuthContext)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user