'firebase.auth().currentUser and onAuthStateChanged always null inspite of being signed in

The firebase.auth().currentUser is always null, as is the onAuthStateChanged listener in my node.js code. However, the user is signed in at my front end swift application. And it is from there that I call to the above node.js script in firebase. A snippet of my code is below. Please take a look and let me know how I can resolve this issue.

Version info firebase 3.17.5 npm 5.4.2 firebase SDK 5.0.4

Platform Information linux mac-book air 17.2.0

CODE:

'use strict';

const firebase = require('firebase');
const fireApp = require('firebase-app');
const functions = require('firebase-functions');
const fireAuth = require('firebase-auth');
const admin = require('firebase-admin');
const gcs = require('@google-cloud/storage');
const exec = require('child_process').exec;
const path = require('path');
const fs = require('fs');
const google = require('googleapis');
const sizeOf = require('image-size');

var config = {
    apiKey: "Ahigdhvd_icvisQijbdsivdbbvb_blisdvchsblksdbs",
    authDomain: "my_project.firebaseapp.com",
    databaseURL: "https://my_project.firebaseio.com",
    projectId: "my_project",
    storageBucket: "my_project.appspot.com",
    messagingSenderId: "2536384943632"
  };

firebase.initializeApp(config);
admin.initializeApp(config);

firebase.auth().onAuthStateChanged(function(user) {
            if (user) {
            console.log("user is signed in")
            } else {
            console.log("no user is signed in")
            }
              });


Solution 1:[1]

You can't use the Firebase Authentication SDK like this. The SDK only works on the client side. You can detect when the user logs in an out on the device (web, Android, iOS), but code running elsewhere can't set up listeners like this.

If you need to communicate with a backend about the login state of the user, you'll have to write some code to communicate between the frontend and backend.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Doug Stevenson