Whenever I try to reference the user's idToken from server side I get a message saying there is no user logged in. I've been stuck on this for a while so any help would be great
Here is the start.js file:
const cookieParser = require("cookie-parser");
const csrf = require("csurf");
const bodyParser = require("body-parser");
const path = require('path');
const express = require('express');
const admin = require("firebase-admin");
// const fbAuth = require('./routes/fbAuth');
const serviceAccount = require("./serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "",
});
app.get("/view", function (req, res) {
const sessionCookie = req.cookies.session || "";
admin
.auth()
.verifySessionCookie(sessionCookie, true /** checkRevoked */)
.then(() => {
res.render("view.html");
})
.catch((error) => {
res.redirect("/login");
});
});
app.post("/sessionLogin", (req, res) => {
const idToken = req.body.idToken.toString();
const expiresIn = 60 * 60 * 24 * 5 * 1000;
admin
.auth()
.createSessionCookie(idToken, { expiresIn })
.then(
(sessionCookie) => {
const options = { maxAge: expiresIn, httpOnly: true };
res.cookie("session", sessionCookie, options);
res.end(JSON.stringify({ status: "success" }));
},
(error) => {
res.status(401).send("UNAUTHORIZED REQUEST!");
}
);
});
app.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); });
Here is the client side login:
window.addEventListener("DOMContentLoaded", () =>
{
const firebaseConfig = {
config stuff goes here...
};
firebase.initializeApp(firebaseConfig);
firebase.analytics();
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION);
document
.getElementById("login")
.addEventListener("submit", (event) =>
{
event.preventDefault();
const login = event.target.login.value;
const password = event.target.password.value;
firebase
.auth()
.signInWithEmailAndPassword(login, password)
.then((
{
user
}) =>
{
return user.getIdToken().then((idToken) =>
{
return fetch("/sessionLogin",
{
method: "POST",
headers:
{
Accept: "application/json",
"Content-Type": "application/json",
"CSRF-Token": Cookies.get("XSRF-TOKEN"),
},
body: JSON.stringify(
{
idToken
}),
});
});
})
.then(() =>
{
console.log(firebase.auth().currentUser);
window.location.assign("/view");
});
return false;
});
});
And here is where I try to reference the token in one of the routes files called oss.js (On line 9 is where the problem is identified):
axios(config)
.then(function(response)
{
response = response.data;
console.log(response.progress, response.status);
if (response.progress === "complete")
{
if (response.status === "success")
{
console.log("JOB COMPLETE");
// upload to firebase here
var user = firebase.auth().currentUser;
if (user)
{
admin.auth().verifyIdToken(idToken)
.then(function(decodedToken) {
var uid = decodedToken.uid;
console.log("uid ->", uid);
return uid;
}).catch(function(error)
{
//Handle error
});
}
else
{
console.log("There is no current user.");
}
}
}
})
});