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

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

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

npm install google-auth-library --save

Verify token with google auth lib

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

Last updated