我定义了我的 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>(...)
的配置中,它会被包含在内,但在如此早期的阶段,我没有用户名和密码。
如果我将登录包装在 useCallback 中,它会起作用