keepme / src /context /AuthProvider.tsx
narinder1231's picture
Add AuthContext and AuthProvider for authentication state management
2efe57e
raw
history blame contribute delete
688 Bytes
import React, { useState } from "react";
import { AuthContext } from "./AuthContext";
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(
!!localStorage.getItem("auth_token")
);
const login = (token: string) => {
localStorage.setItem("auth_token", token);
setIsAuthenticated(true);
};
const logout = () => {
localStorage.removeItem("auth_token");
setIsAuthenticated(false);
window.location.href = "/signin";
};
return (
<AuthContext.Provider value={{ isAuthenticated, login, logout }}>
{children}
</AuthContext.Provider>
);
};