feat(authContext.tsx): add support for storing and retrieving API key in the auth context
fix(StoreApiKeyModal/index.tsx): set API key in the auth context when it is saved fix(StorePage/index.tsx): display a message to add an API key if it is not present in the auth context fix(StorePage/index.tsx): display a loading message while fetching data if API key is present in the auth context
This commit is contained in:
parent
0e9ce93f49
commit
df267c065a
4 changed files with 46 additions and 1 deletions
|
|
@ -19,6 +19,9 @@ const initialValue: AuthContextType = {
|
||||||
authenticationErrorCount: 0,
|
authenticationErrorCount: 0,
|
||||||
autoLogin: false,
|
autoLogin: false,
|
||||||
setAutoLogin: () => {},
|
setAutoLogin: () => {},
|
||||||
|
setApiKey: () => {},
|
||||||
|
apiKey: null,
|
||||||
|
hasApiKey: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const AuthContext = createContext<AuthContextType>(initialValue);
|
export const AuthContext = createContext<AuthContextType>(initialValue);
|
||||||
|
|
@ -36,6 +39,11 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
const [userData, setUserData] = useState<Users | null>(null);
|
const [userData, setUserData] = useState<Users | null>(null);
|
||||||
const [autoLogin, setAutoLogin] = useState<boolean>(false);
|
const [autoLogin, setAutoLogin] = useState<boolean>(false);
|
||||||
const { setLoading } = useContext(alertContext);
|
const { setLoading } = useContext(alertContext);
|
||||||
|
const [apiKey, setApiKey] = useState<string | null>(
|
||||||
|
cookies.get("apikey_tkn_lflw")
|
||||||
|
);
|
||||||
|
const [hasApiKey, setHasApiKey] = useState<boolean>(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const storedAccessToken = cookies.get("access_tkn_lflw");
|
const storedAccessToken = cookies.get("access_tkn_lflw");
|
||||||
if (storedAccessToken) {
|
if (storedAccessToken) {
|
||||||
|
|
@ -43,6 +51,13 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const apiKey = cookies.get("apikey_tkn_lflw");
|
||||||
|
if (apiKey) {
|
||||||
|
setApiKey(apiKey);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const isLoginPage = location.pathname.includes("login");
|
const isLoginPage = location.pathname.includes("login");
|
||||||
|
|
||||||
|
|
@ -83,6 +98,10 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
return auth;
|
return auth;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function hasApiKeyCheck() {
|
||||||
|
return hasApiKey;
|
||||||
|
}
|
||||||
|
|
||||||
function login(newAccessToken: string, refreshToken: string) {
|
function login(newAccessToken: string, refreshToken: string) {
|
||||||
cookies.set("access_tkn_lflw", newAccessToken, { path: "/" });
|
cookies.set("access_tkn_lflw", newAccessToken, { path: "/" });
|
||||||
cookies.set("refresh_tkn_lflw", refreshToken, { path: "/" });
|
cookies.set("refresh_tkn_lflw", refreshToken, { path: "/" });
|
||||||
|
|
@ -101,6 +120,10 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
setIsAuthenticated(false);
|
setIsAuthenticated(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function apikey(apikey: string) {
|
||||||
|
cookies.set("apikey_tkn_lflw", apikey, { path: "/" });
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
// !! to convert string to boolean
|
// !! to convert string to boolean
|
||||||
<AuthContext.Provider
|
<AuthContext.Provider
|
||||||
|
|
@ -118,6 +141,9 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
authenticationErrorCount: 0,
|
authenticationErrorCount: 0,
|
||||||
setAutoLogin,
|
setAutoLogin,
|
||||||
autoLogin,
|
autoLogin,
|
||||||
|
setApiKey,
|
||||||
|
apiKey,
|
||||||
|
hasApiKey,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import { Button } from "../../components/ui/button";
|
||||||
import { Input } from "../../components/ui/input";
|
import { Input } from "../../components/ui/input";
|
||||||
import { CONTROL_NEW_API_KEY } from "../../constants/constants";
|
import { CONTROL_NEW_API_KEY } from "../../constants/constants";
|
||||||
import { alertContext } from "../../contexts/alertContext";
|
import { alertContext } from "../../contexts/alertContext";
|
||||||
|
import { AuthContext } from "../../contexts/authContext";
|
||||||
import { addApiKeyStore } from "../../controllers/API";
|
import { addApiKeyStore } from "../../controllers/API";
|
||||||
import {
|
import {
|
||||||
ApiKeyInputType,
|
ApiKeyInputType,
|
||||||
|
|
@ -23,6 +24,7 @@ export default function StoreApiKeyModal({
|
||||||
useState<ApiKeyInputType>(CONTROL_NEW_API_KEY);
|
useState<ApiKeyInputType>(CONTROL_NEW_API_KEY);
|
||||||
const { setSuccessData, setErrorData } = useContext(alertContext);
|
const { setSuccessData, setErrorData } = useContext(alertContext);
|
||||||
const inputRef = useRef<HTMLInputElement | null>(null);
|
const inputRef = useRef<HTMLInputElement | null>(null);
|
||||||
|
const { setApiKey, apiKey } = useContext(AuthContext);
|
||||||
|
|
||||||
function handleInput({
|
function handleInput({
|
||||||
target: { name, value },
|
target: { name, value },
|
||||||
|
|
@ -49,6 +51,7 @@ export default function StoreApiKeyModal({
|
||||||
setSuccessData({
|
setSuccessData({
|
||||||
title: "Success! Your API Key has been saved.",
|
title: "Success! Your API Key has been saved.",
|
||||||
});
|
});
|
||||||
|
setApiKey(inputState["apikey"]);
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ import {
|
||||||
} from "../../components/ui/select";
|
} from "../../components/ui/select";
|
||||||
import { Switch } from "../../components/ui/switch";
|
import { Switch } from "../../components/ui/switch";
|
||||||
import { alertContext } from "../../contexts/alertContext";
|
import { alertContext } from "../../contexts/alertContext";
|
||||||
|
import { AuthContext } from "../../contexts/authContext";
|
||||||
import { TabsContext } from "../../contexts/tabsContext";
|
import { TabsContext } from "../../contexts/tabsContext";
|
||||||
import { getStoreComponents, searchComponent } from "../../controllers/API";
|
import { getStoreComponents, searchComponent } from "../../controllers/API";
|
||||||
import StoreApiKeyModal from "../../modals/StoreApiKeyModal";
|
import StoreApiKeyModal from "../../modals/StoreApiKeyModal";
|
||||||
|
|
@ -24,6 +25,8 @@ import { MarketCardComponent } from "./components/market-card";
|
||||||
export default function StorePage(): JSX.Element {
|
export default function StorePage(): JSX.Element {
|
||||||
const { setTabId } = useContext(TabsContext);
|
const { setTabId } = useContext(TabsContext);
|
||||||
|
|
||||||
|
const { setApiKey, apiKey } = useContext(AuthContext);
|
||||||
|
|
||||||
// set null id
|
// set null id
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setTabId("");
|
setTabId("");
|
||||||
|
|
@ -91,7 +94,7 @@ export default function StorePage(): JSX.Element {
|
||||||
<span className="community-page-description-text">
|
<span className="community-page-description-text">
|
||||||
Search flows and components from the community.
|
Search flows and components from the community.
|
||||||
</span>
|
</span>
|
||||||
{!loading && (
|
{!loading && apiKey && (
|
||||||
<div className="flex w-full flex-col gap-4 p-4">
|
<div className="flex w-full flex-col gap-4 p-4">
|
||||||
<div className="flex items-center justify-center gap-4">
|
<div className="flex items-center justify-center gap-4">
|
||||||
<div className="flex w-[13%] items-center justify-center gap-3 text-sm">
|
<div className="flex w-[13%] items-center justify-center gap-3 text-sm">
|
||||||
|
|
@ -188,6 +191,16 @@ export default function StorePage(): JSX.Element {
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{!apiKey && (
|
||||||
|
<div className="flex w-full flex-col gap-4 p-4">
|
||||||
|
Try add an API Key :)
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{apiKey && loading && (
|
||||||
|
<div className="flex w-full flex-col gap-4 p-4">Loading...</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -14,4 +14,7 @@ export type AuthContextType = {
|
||||||
authenticationErrorCount: number;
|
authenticationErrorCount: number;
|
||||||
autoLogin: boolean;
|
autoLogin: boolean;
|
||||||
setAutoLogin: (autoLogin: boolean) => void;
|
setAutoLogin: (autoLogin: boolean) => void;
|
||||||
|
apiKey: string | null;
|
||||||
|
setApiKey: (apiKey: string | null) => void;
|
||||||
|
hasApiKey: boolean;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue