Is it possible to access the client firebase e.g. signInWithEmailAndPassword methods in the firebase SDK side? (i know sdk is for server generally and client is js, but i need a non js solution client side).
Ive setup the SDK side and and express thats all fine, but im just trying to send a form post request to my node express server to try login the user but its saying signInWithEmailAndPassword doesn’t exist in the sdk etc.
Is it possible to initiate the client side js into the server along side the sdk?
my app.js
const express = require('express');
const app = express();
const ejs = require('ejs');
const port = 3000;
const path = require('path');
const firebase = require('./api/firebase.js');
global.appRoot = path.resolve(__dirname);
app.use(express.json());
app.use(express.urlencoded({ extended: true }))
app.use(express.static("public"));
app.use('/api/auth', firebase);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
//? Render entry point index.ejs
app.get('/', function (req, res) {
// Render page using renderFile method
ejs.renderFile('views/index.ejs', { title: 'Oomph TV Login' },
{}, function (err, template) {
if (err) {
throw err;
} else {
res.end(template);
}
});
});
//? Server initialization
app.listen(port, async function (error) {
if (error)
throw error;
else
//* Create firebase admin connection
var firebaseAdmin = await require(global.appRoot + '/functions/firebase/backend.js').connect();
var firebaseClient = await require(global.appRoot + '/functions/firebase/frontend.js').connect();
console.log(firebaseAdmin.message);
console.log(firebaseClient.message);
console.log("Server is idle");
});
And then i have my backend.js firebase file which connects the sdk fine and then i was hoping to have a front end like this, but the sign in methods are still undefined
const firebase = require("firebase/app");
const { getAuth } = require('firebase/auth');
const firebaseConfig = {
};
module.exports.connect = () => {
return new Promise(async (resolve) => {
try {
firebase.initializeApp(firebaseConfig);
global.firebaseClient = getAuth();
resolve({ code: 1, message: 'Firebase Client Connected', data: null, error: null });
}
catch (error) {
resolve({ code: -1, message: 'Firebase Client Connection Failed', data: null, error: error });
}
});
};
2
Answers
If you really only want to use the authentication by firebase you can use the REST API endpoints for the same
you can checkout more at the docs
https://firebase.google.com/docs/reference/rest/auth
there are endpoints for almost many of the authentication methods
You can use the Firebase REST API for authentication.
There are multiple endpoints for authentication, like signup with email and password, login with email and password, login with phone number and whole lot of stuff. After the user is authenticated, you get a token in return which you can sent to the client to maintain the auth state. There is also an endpoint for token verification in case if it is required, or also can be done through admin sdk libraries.
For more information, check out the firebase REST API documentation