const axios = require('axios');
const weavyURL = 'WY_BACKEND_URL';
const weavyAPI = 'WY_API_*****************';
exports.main = async ({ body }, sendResponse) => {
const { uid, name, email, imageUrl } = body;
try {
// Create or update user in Weavy
const createUserResponse = await axios.put(
`${weavyURL}/api/users/${uid}`,
{
name: name,
email: email,
picture: imageUrl
},
{
headers: {
'Authorization': `Bearer ${weavyAPI}`,
'Content-Type': 'application/json'
}
}
);
// Generate access token
const tokenResponse = await axios.post(
`${weavyURL}/api/users/${uid}/tokens`,
{},
{
headers: {
'Authorization': `Bearer ${weavyAPI}`,
'Content-Type': 'application/json'
}
}
);
// Return the token
sendResponse({
body: tokenResponse.data,
statusCode: 200
});
} catch (error) {
console.error('Error in Weavy authentication:', error.response?.data || error.message);
sendResponse({
body: {
error: 'Failed to authenticate with Weavy',
details: error.response?.data || error.message
},
statusCode: error.response?.status || 500
});
}
};