I using node server and I need birthday and gender but I only get name, email, and photo. I using this (@react-oauth/google) library in frontend. I using react in the frontend. When I send api request from frontend I get only name, email, and photo. And here is the backend code. So please I need the solution of my problem.
const express = require('express');
const app = express();
const { OAuth2Client } = require('google-auth-library');
const client = new OAuth2Client(process.env.GOOGLE_CLIENT_ID);
const users = [];
function upsert(array, item) {
const i = array.findIndex((_item) => _item.email === item.email);
if (i > -1) array[i] = item;
else array.push(item);
}
app.post('/api/google-login', async (req, res) => {
const { token } = req.body;
const ticket = await client.verifyIdToken({
idToken: token,
audience: process.env.GOOGLE_CLIENT_ID,
});
const { name, email, picture } = ticket.getPayload();
upsert(users, { name, email, picture });
console.log(ticket.getPayload());
res.status(201);
res.json({ name, email, picture });
});
app.listen(process.env.PORT || 5000, () => {
console.log(
`Server is ready at http://localhost:${process.env.PORT || 5000}`
);
});
CodePudding user response:
You can check their documentation
They do not return gender and birthdate. Here is an example of a full response containing the information that you can use (from the documentation link above)
{
// These six fields are included in all Google ID Tokens.
"iss": "https://accounts.google.com",
"sub": "110169484474386276334",
"azp": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",
"aud": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",
"iat": "1433978353",
"exp": "1433981953",
// These seven fields are only included when the user has granted the "profile" and
// "email" OAuth scopes to the application.
"email": "[email protected]",
"email_verified": "true",
"name" : "Test User",
"picture": "https://lh4.googleusercontent.com/-kYgzyAWpZzJ/ABCDEFGHI/AAAJKLMNOP/tIXL9Ir44LE/s99-c/photo.jpg",
"given_name": "Test",
"family_name": "User",
"locale": "en"
}
CodePudding user response:
I'm the creator of @react-oauth I'm thrilled that you gave it a try.
Actually, I didn't try to get user age before