好的,我想出了一个修复方法。首先,我只是尝试以与userCredntials
社交身份验证文档中相同的方式将身份验证令牌传递给对象,这在我上面从他们的文档中获取的代码片段中的描述中没有。
然后我在异步函数内部承诺 API 调用,useEffect
以确保一切都以正确的顺序发生,并且它可以工作:
export default function App() {
const createAppSession = () => {
return new Promise((resolve, reject) => {
ConnectyCube.createSession((error, session) => {
!error
? resolve(session.token)
: reject(error, '=====1=====');
});
})
}
const loginUser = (credentials) => {
return new Promise((resolve, reject) => {
ConnectyCube.login(credentials, ((error, user) => {
!error
? resolve(user)
: reject(error, '=====2=====');
}));
})
}
useEffect(() => {
const ccFunc = async () => {
ConnectyCube.init(...config)
const appSessionToken = await createAppSession();
const userCredentials = { login: 'xxxxx', password: 'xxxxxxx', keys: { token: appSessionToken } };
const user = await loginUser(userCredentials);
console.log(user);
}
ccFunc()
}, []);