auth 提供者中的 makeUseAxios 和 http 基本身份验证

问题描述 投票:0回答:1

我定义了我的 axios 钩子

const axiosInstance = axios.create({baseURL: baseURL})

export const useAxios = makeUseAxios({axios: axiosInstance})

我定义身份验证提供者

export const AuthProvider = ({children}: { children: ReactNode }) => {
    const [, postLogin] = useAxios<Tokens>({method: 'post', url: '/tokens'})

    const login = async (username: string, password: string) => {
        const response = await postLogin({auth: {username, password}})
        if (response.status === 201) {
            ....
        }
    }
    ...
}

但是请求不包含http Authentication 标头。如果我将 auth 属性添加到传递给

useAxios<Tokens>(...)
的配置中,它会被包含在内,但在如此早期的阶段,我没有用户名和密码。

reactjs typescript axios
1个回答
0
投票

如果我将登录包装在 useCallback 中,它会起作用

© www.soinside.com 2019 - 2024. All rights reserved.