# Google Sign-in with Nodejs Backend

Follow the instructions on Google official website

<https://developers.google.com/identity/sign-in/web/backend-auth>

## Implementation

### Send the ID token to your server <a href="#send-the-id-token-to-your-server" id="send-the-id-token-to-your-server"></a>

Make a post call with token on your angular app to Nodejs backend

```javascript
import { HttpClient } from '@angular/common/http';
import {Injectable} from '@angular/core';
import { SocialUser } from 'angularx-social-login';
import {environment} from '../environments/environment';

const BACKEND_URL = environment.apiUrl;

@Injectable()
export class SigninService{

    constructor(private httpClient: HttpClient){}

    signin(usr: SocialUser){

        let url = `${BACKEND_URL}/test`;

        this.httpClient.post<{token: string}>(
            url,
            {
                token: usr.idToken,
            }
        ).subscribe((response) => {
            console.log(response);
        });

    }
}
```

### Install Google-auth-library on your Nodejs app

```bash
npm install google-auth-library --save
```

### Verify token with google auth lib

```javascript
const {OAuth2Client} = require('google-auth-library');
const CLIENT_ID = "Your client key on google";
const client = new OAuth2Client(CLIENT_ID);

exports.testPostApi = (req, res, next) => {

    client.verifyIdToken({
        idToken: req.body.token,
        audience: CLIENT_ID,
    }).then((result) => {
        res.status(200)
        .json({
            message: "Post test success",
            body: result
        });
    });
};
```

### Verify sign-in status in the response


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://owen31302.gitbook.io/github-education/util/google-sign-in-with-nodejs-backend.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
