fixed cache Refactoring from app router to page router Refactoring from app router to page router Add authentication with JWT base ui done protect the dashboard added transitions styling type trick added cleanup Add enable-disable logic add transactional emails Improvements on delay and frontend added description on select implement auth with magic link migrate to mariadb webhook signature working Adding async processing with queues Implemented webhook reply functionality Add son execution logs update status logic Add recent activity monitoring to sons show son performance in dashboard add readme implement listmonk connector listmonk-connector-v1 Create CODE_OF_CONDUCT.md Create LICENSE
67 lines
1.8 KiB
TypeScript
67 lines
1.8 KiB
TypeScript
import { createContext, useContext, useState, useEffect } from "react";
|
|
import { apiClient } from "@/lib/api-client";
|
|
|
|
interface User {
|
|
id: string;
|
|
email: string;
|
|
}
|
|
|
|
interface AuthContextType {
|
|
user: User | null;
|
|
login: (token: string, user: User) => Promise<void>;
|
|
logout: () => Promise<void>;
|
|
loading: boolean;
|
|
}
|
|
|
|
const AuthContext = createContext<AuthContextType | null>(null);
|
|
|
|
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
|
|
children,
|
|
}) => {
|
|
const [user, setUser] = useState<User | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
const token = localStorage.getItem("authToken");
|
|
const storedUser = localStorage.getItem("user");
|
|
if (token && storedUser) {
|
|
setUser(JSON.parse(storedUser));
|
|
apiClient.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
|
}
|
|
setLoading(false);
|
|
}, []);
|
|
|
|
const login = async (token: string, user: User) => {
|
|
localStorage.setItem("authToken", token);
|
|
localStorage.setItem("user", JSON.stringify(user));
|
|
apiClient.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
|
setUser(user);
|
|
};
|
|
|
|
const logout = async () => {
|
|
try {
|
|
await apiClient.post("/auth/logout");
|
|
} catch (error) {
|
|
console.error("Logout failed:", error);
|
|
} finally {
|
|
localStorage.removeItem("authToken");
|
|
localStorage.removeItem("user");
|
|
delete apiClient.defaults.headers.common["Authorization"];
|
|
setUser(null);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<AuthContext.Provider value={{ user, login, logout, loading }}>
|
|
{children}
|
|
</AuthContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const useAuthContext = () => {
|
|
const context = useContext(AuthContext);
|
|
if (!context) {
|
|
throw new Error("useAuth must be used within an AuthProvider");
|
|
}
|
|
return context;
|
|
};
|