atharvgosling
atharvgosling
XXata
Created by atharvgosling on 3/3/2025 in #help
I am trying to update the user record in my database in the callback function of oauth2. FetchError
The code works fine without the Xata logic, i.e only for authentication, but gives Fetch Error in the catch block when i try to update the record. I have checked my schema against this and its correct.
exports.callback=async (req,res)=>{
const code=req.query.code;

try{
const response=await axios.post(process.env.TOKEN_ENDPOINT,{
code,
client_id:process.env.CLIENT_ID,
client_secret:process.env.CLIENT_SECRET,
redirect_uri:process.env.REDIRECT_URI,
grant_type:'authorization_code',
});

const {id_token, access_token}=response.data;

const userInfo=await axios.get('https://www.googleapis.com/oauth2/v3/userinfo',{
headers: { Authorization: `Bearer ${access_token}` }
});

// Checking if user exists in Xata database
let user = await xata.db.users.filter({ email: userInfo.data.email }).getFirst();
console.log(user);
// If user doesn't exist, creating a new user in Xata
if (!user) {

user = await xata.db.users.create({
user_id: Date.now(),
username: userInfo.data.name,
password_hash: null,
email: userInfo.data.email,
});
console.log("now "+user);
}

// Generating JWT with Xata user ID and other user info
const userToken = generateToken({ id: user.xata_id, ...userInfo.data });

res.cookie('token',userToken,{httpOnly:true});

res.redirect(process.env.FRONTEND_URL);
}
catch(err){
console.error('Error in OAuth callback:', err);
res.status(500).send("Error during OAuth callback")
}
};
exports.callback=async (req,res)=>{
const code=req.query.code;

try{
const response=await axios.post(process.env.TOKEN_ENDPOINT,{
code,
client_id:process.env.CLIENT_ID,
client_secret:process.env.CLIENT_SECRET,
redirect_uri:process.env.REDIRECT_URI,
grant_type:'authorization_code',
});

const {id_token, access_token}=response.data;

const userInfo=await axios.get('https://www.googleapis.com/oauth2/v3/userinfo',{
headers: { Authorization: `Bearer ${access_token}` }
});

// Checking if user exists in Xata database
let user = await xata.db.users.filter({ email: userInfo.data.email }).getFirst();
console.log(user);
// If user doesn't exist, creating a new user in Xata
if (!user) {

user = await xata.db.users.create({
user_id: Date.now(),
username: userInfo.data.name,
password_hash: null,
email: userInfo.data.email,
});
console.log("now "+user);
}

// Generating JWT with Xata user ID and other user info
const userToken = generateToken({ id: user.xata_id, ...userInfo.data });

res.cookie('token',userToken,{httpOnly:true});

res.redirect(process.env.FRONTEND_URL);
}
catch(err){
console.error('Error in OAuth callback:', err);
res.status(500).send("Error during OAuth callback")
}
};
error-
Error in OAuth callback: FetcherError: Internal Error (Request ID: d38e69c1-681f-9f4a-9b1c-58923c0aa05a)
at file:///C:/Users/athar/Documents/GitHub/mango-store/backend/node_modules/@xata.io/client/dist/index.mjs:2414:15
Error in OAuth callback: FetcherError: Internal Error (Request ID: d38e69c1-681f-9f4a-9b1c-58923c0aa05a)
at file:///C:/Users/athar/Documents/GitHub/mango-store/backend/node_modules/@xata.io/client/dist/index.mjs:2414:15
4 replies