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qS8RjGz5ITNXpdwRSVYzbZdG3Jvm7ZJdVp64_lNXHAx-0JWl89enABHk8DCMmZdGkk_OgUqRqV0l0w447xiqArhDIkeHjzEuJfUfqngWGhA2OSKWY6eAshKlZbPDsdhZ3ElBwO0h1wNWc4TlmPe38SCa84GXoiU2O128yeGbJDVdBLO6YWzWu8NJCunGxaNP9Di-D375w"const CLIENT_ID = "607000044452-001g3g9obeecxxxxxxxxjvvj4c4vxxku.apps.googleusercontent.com"const { OAuth2Client } = require('google-auth-library');
const client = new OAuth2Client(CLIENT_ID);
async function verify() {
try {
const ticket = await client.verifyIdToken({
idToken: token,
audience: CLIENT_ID, // Specify the CLIENT_ID of the app that accesses the backend
// Or, if multiple clients access the backend:
//[CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3]
});
const payload = ticket.getPayload();
const userid = payload['sub'];
console.log(payload)
} catch (error) {
console.log(error)
}
}
verify()

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ankit Kumar Rajpoot

Ankit Kumar Rajpoot

I’m a MERN Developer. ( Redux | AWS | Python ) I enjoy taking on new things, building skills, and sharing what I’ve learned.