File size: 875 Bytes
97f53b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const { OAuth2Client } = require('google-auth-library');
require('dotenv').config();
const googleClientId = process.env.GOOGLE_ID;

// Initialize the OAuth2 client with your Google Client ID
const client = new OAuth2Client('YOUR_GOOGLE_CLIENT_ID');

async function verifyGoogleToken(token) {
    try {
        // Verify the token
        const ticket = await client.verifyIdToken({
            idToken: token,
            audience: googleClientId, // Specify the CLIENT_ID of the app that accesses the backend
        });

        // Get the user payload from the ticket
        const payload = ticket.getPayload();

        // Return the payload with user information
        return ticket;
    } catch (error) {
        console.error('Google token verification failed:', error);
        throw new Error('Invalid Google token');
    }
}

module.exports = verifyGoogleToken;