getting user in app
This commit is contained in:
parent
90fd2905fb
commit
28b8578605
6 changed files with 62 additions and 25 deletions
|
|
@ -24,38 +24,52 @@ interface IHTTPContext {
|
|||
options: (demo?: boolean) => AxiosRequestConfig;
|
||||
}
|
||||
|
||||
interface Props extends PropsWithChildren<any>{
|
||||
setGotToken: React.Dispatch<React.SetStateAction<boolean>>;
|
||||
}
|
||||
|
||||
export const HTTPContext = createContext<IHTTPContext>({} as IHTTPContext);
|
||||
|
||||
export default function HTTPProvider(props: PropsWithChildren<any>) {
|
||||
const { children } = props;
|
||||
export default function HTTPProvider(props: Props) {
|
||||
const { children, setGotToken } = props;
|
||||
const { isAuthenticated, /*token,*/ getAccessTokenSilently } = useAuth0();
|
||||
const [token, setToken] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
if (isAuthenticated) getAccessTokenSilently().then(t => {
|
||||
setToken(t)
|
||||
}).finally(() => {
|
||||
setGotToken(true)
|
||||
})
|
||||
}, [setToken, isAuthenticated])
|
||||
|
||||
const defaultOptions = (demo: boolean = false) => {
|
||||
if (demo || !isAuthenticated || !token) {
|
||||
return {
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
};
|
||||
}
|
||||
// if (demo || !isAuthenticated || !token) {
|
||||
// console.log("demo: ", demo)
|
||||
// console.log("Authenticated: ", isAuthenticated)
|
||||
// console.log("token: ", token)
|
||||
// return {
|
||||
// headers: {
|
||||
// "Content-Type": "application/json"
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
|
||||
return {
|
||||
headers: {
|
||||
Authorization: "Bearer " + token,
|
||||
// console.log("Getting options with Bearer")
|
||||
// console.log(token)
|
||||
|
||||
const config = {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
};
|
||||
return config;
|
||||
};
|
||||
|
||||
function get<T>(url: string, spreadOptions?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
||||
return axios.get(url, { ...defaultOptions(), ...spreadOptions });
|
||||
function get<T>(url: string): Promise<AxiosResponse<T>> {
|
||||
console.log(defaultOptions())
|
||||
return axios.get(url, {...defaultOptions()});
|
||||
}
|
||||
|
||||
function put<T>(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue