Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get this error auth/operation-not-supported-in-this-environment using express nodejs

I'm working with Firebase in my project but getting this error auth/operation-not-supported-in-this-environment when login with google credentials.

.hbs file code

<span class="google-bg session-icon">
  <a href="#!" id="google" onclick=" return loginWithGoogle(this)">
      <i class="ti-google"></i>
   </a>
</span>

script code

function loginWithGoogle(event){
  $.ajax({
    url: "/session/google/login",
       type: "POST"
    })
    .done(function (data) {
    error= JSON.stringify(data);
    console.log(error);
    M.toast({html: error})
 });
}

Express code

router.post('/session/google/login',function (req, res, next){
   //console.log('get resqust');
   firebase.auth().signInWithRedirect(googleAuthProvider).then(function(result){
       console.log(result);
   }, function(error){
      console.log(error.code);
      res.json({
         data: error.code
      })
   });   
})

When I click on Google icon then call loginWithGoogle function and get the router path /session/google/login after that execute express code and generate error. I`m wondering to resolve this issue and what could be wrong ?

Thankyou!!!

Updated(16-10-18)

Call dashboard route after successfully login with Gmail account.

router.get('/dashboard', function(req, res, next) {
      console.log(firebase.auth().currentUser);
      if(firebase.auth().currentUser != null){
         res.render('dashboard', { 
            title: 'App'
         });
      }else {
      req.session.error = 'Access denied!';
      console.log(req.session.error);
      res.redirect('/login');
   }
}); 

After successfully login with gmail account I have call dashboard route and using condition before render the dashboard page but currentUser returns null. I have check in Firebase console that shows recently new user login with gmail and if i have login with simple userid and password then currentUser returns the data. where i`m wrong ??

Updated 17-10-18

function loginWithGoogle(event) {
    firebase.initializeApp(config);
    var provider = new firebase.auth.GoogleAuthProvider();
    firebase.auth().signInWithPopup(googleAuthProvider)
       .then(function (user) {
           // localStorage.setItem('user',JSON.stringify(user));
           window.location.href = '/dashboard'; 
        })
        .catch(function (error) {
            var errorCode = error.code;
            var errorMessage = error.message;
            var email = error.email;
            var credential = error.credential
        })
    }

After successfully login I have redirect to '/dashboard' and express call defined route for dashboard. which I have mention yesterday. now please tell me where I call dashboard route??

like image 491
Varinder Sohal Avatar asked Sep 28 '18 10:09

Varinder Sohal


2 Answers

Before you start check that you've added firebase to your front end if you didn't do it yet here are instructions how to do it: https://firebase.google.com/docs/web/setup

Login with Google is done on front end and its pretty simple, you don't need to change your html code, only javascript like below and no need to call cloud functions

function loginWithGoogle(event){
  var provider = new firebase.auth.GoogleAuthProvider();
  firebase.auth().signInWithRedirect(provider);
}

This is only one of use cases when you sign in by redirect, there are sign in with pop up and more complex handling of sign in that you can try after you will make this one to work: https://firebase.google.com/docs/auth/web/google-signin

For authenticated calls to your cloud functions you need to get token first and then make a call. But you can do it only after you have already signed in.

function authenticatedCallToFunctions(event){
  firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) {
    $.ajax({
        url: "someurl",
        type: "POST",
        headers: {"firebase-token": idToken}
      })
      .done(function (data) {
        error= JSON.stringify(data);
        console.log(error);
        M.toast({html: error})
      });
  }).catch(function(error) {
    // Handle error
  });
}

And you can process your authenticated request in cloud functions like

import * as admin from 'firebase-admin';

router.post('someurl',function (req, res, next){
  var token = req.headers['firebase-token'];

  admin.auth().verifyIdToken(token).then(function(result){
      console.log(result);
  }, function(error){
     console.log(error.code);
     res.json({
        data: error.code
     })
  });
})
like image 133
Yevgen Avatar answered Oct 02 '22 07:10

Yevgen


The Firebase Authentication SDK for JavaScript only works in the browser, not in a node.js environment. If you need to work with Firebase Auth on your backend, you'll need to use the Firebase Admin SDK, which you can use to manage user sessions.

like image 41
Doug Stevenson Avatar answered Oct 02 '22 08:10

Doug Stevenson