0

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)

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, but I believe this example is for authentication using JSON Web Tokens (JWTs). I would like to authenticate with google account.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.