Jestin Mathew
Jestin Mathew

Reputation: 3

Firebase google authentication with node js

I would like to add a button 'Login with google' to my app and I'm trying to do the authentication with firebase in node js. I didn't get any example from firebase official site, where the documentation is given for java script.

please find the code snippet below:

const firebaseAdmin = require('firebase-admin');
const serviceAccount = require('./SAK.json');
const FirebaseAuth = require('firebaseauth');
const authProvider = new FirebaseAuth("API_KEY");
const authToken = FirebaseAuth.initTokenMiddleware(serviceAccount);

firebaseAdmin.initializeApp({
    credential: firebaseAdmin.credential.cert(serviceAccount)
});

router.get('/loginWithGoogle/', (req,res) => {
    authProvider.loginWithGoogle(authToken, function(err, result) {
    if (err) {
        console.log('err');
    }
    else {
        console.log(result);
    }
});
});

The authToken value is null i guess. I'm getting the below error :

TypeError: Cannot read property 'trim' of null at loginWithProviderID (/workspace/sodiumBackend/node_modules/firebaseauth/dist/providers/social-providers.js:22:23) at Object.loginWithGoogle (/workspace/sodiumBackend/node_modules/firebaseauth/dist/providers/social-providers.js:57:5) at FirebaseAuth.loginWithGoogle (/workspace/sodiumBackend/node_modules/firebaseauth/dist/index.js:61:25) at router.get (/workspace/sodiumBackend/src/routes/api/user.js:8:18) at Layer.handle [as handle_request] (/workspace/sodiumBackend/node_modules/express/lib/router/layer.js:95:5) at next (/workspace/sodiumBackend/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/workspace/sodiumBackend/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/workspace/sodiumBackend/node_modules/express/lib/router/layer.js:95:5)

Upvotes: 0

Views: 1223

Answers (1)

rerez
rerez

Reputation: 111

Hi, First of all please check up the documentations for 'Manage Users' in firebase Admin.

https://firebase.google.com/docs/auth/admin/manage-users

Notice the firebase createUser method

    admin.auth().createUser({
  email: '[email protected]',
  emailVerified: false,
  phoneNumber: '+11234567890',
  password: 'secretPassword',
  displayName: 'John Doe',
  photoURL: 'http://www.example.com/12345678/photo.png',
  disabled: false
})
  .then(function(userRecord) {
    // See the UserRecord reference doc for the contents of userRecord.
    console.log('Successfully created new user:', userRecord.uid);
  })
  .catch(function(error) {
    console.log('Error creating new user:', error);
  });

"By default, Firebase Authentication will generate a random uid for the new user."

After the user was created you can create a token for it

var myTokenToSave;
   admin
        .auth()
        .createCustomToken(userRecord.uid)
        .then(function(customToken) {
          myTokenToSave = customToken;
        });

Then eventually authenticate it

 admin.auth().verifyIdToken(idToken)
  .then(function(decodedToken) {
    let uid = decodedToken.uid;
    // ...
  }).catch(function(error) {
    // Handle error
  });

Hopefully this make sense, the code placement and usage depends on your implementation.

Upvotes: 1

Related Questions