feat: remove context use
This commit is contained in:
@ -1,35 +0,0 @@
|
|||||||
import { createContext, useContext } from "react";
|
|
||||||
|
|
||||||
interface OAuthContextValues {
|
|
||||||
active: boolean;
|
|
||||||
clientID: string;
|
|
||||||
redirectURI: string;
|
|
||||||
scope: string[];
|
|
||||||
state: string;
|
|
||||||
nonce: string;
|
|
||||||
setActive: (state: boolean) => void;
|
|
||||||
setClientID: (id: string) => void;
|
|
||||||
setRedirectURI: (uri: string) => void;
|
|
||||||
setScope: (scopes: string[]) => void;
|
|
||||||
setState: (state: string) => void;
|
|
||||||
setNonce: (nonce: string) => void;
|
|
||||||
selectSession: (token: string) => Promise<void>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const OAuthContext = createContext<OAuthContextValues>({
|
|
||||||
active: false,
|
|
||||||
clientID: "",
|
|
||||||
redirectURI: "",
|
|
||||||
scope: [],
|
|
||||||
state: "",
|
|
||||||
nonce: "",
|
|
||||||
setActive: () => {},
|
|
||||||
setClientID: () => {},
|
|
||||||
setRedirectURI: () => {},
|
|
||||||
setScope: () => {},
|
|
||||||
setState: () => {},
|
|
||||||
setNonce: () => {},
|
|
||||||
selectSession: async () => {},
|
|
||||||
});
|
|
||||||
|
|
||||||
export const useOAuthContext = () => useContext(OAuthContext);
|
|
@ -1,54 +0,0 @@
|
|||||||
import { useCallback, useState, type FC, type ReactNode } from "react";
|
|
||||||
import { OAuthContext } from ".";
|
|
||||||
import { codeApi } from "@/api/code";
|
|
||||||
|
|
||||||
interface IOAuthProvider {
|
|
||||||
children: ReactNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const OAuthProvider: FC<IOAuthProvider> = ({ children }) => {
|
|
||||||
const [active, setActive] = useState(false);
|
|
||||||
const [clientID, setClientID] = useState("");
|
|
||||||
const [redirectURI, setRedirectURI] = useState("");
|
|
||||||
const [scope, setScope] = useState<string[]>([]);
|
|
||||||
const [state, setState] = useState("");
|
|
||||||
const [nonce, setNonce] = useState("");
|
|
||||||
|
|
||||||
const selectSession = useCallback(
|
|
||||||
async (token: string) => {
|
|
||||||
if (active && redirectURI) {
|
|
||||||
const codeResponse = await codeApi(token, nonce);
|
|
||||||
|
|
||||||
const params = new URLSearchParams({
|
|
||||||
code: codeResponse.code,
|
|
||||||
state,
|
|
||||||
});
|
|
||||||
|
|
||||||
window.location.replace(`${redirectURI}?${params.toString()}`);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[active, nonce, redirectURI, state],
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<OAuthContext.Provider
|
|
||||||
value={{
|
|
||||||
active,
|
|
||||||
clientID,
|
|
||||||
redirectURI,
|
|
||||||
scope,
|
|
||||||
state,
|
|
||||||
nonce,
|
|
||||||
setActive,
|
|
||||||
setClientID,
|
|
||||||
setRedirectURI,
|
|
||||||
setScope,
|
|
||||||
setState,
|
|
||||||
setNonce,
|
|
||||||
selectSession,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{children}
|
|
||||||
</OAuthContext.Provider>
|
|
||||||
);
|
|
||||||
};
|
|
Reference in New Issue
Block a user