Reputation: 594
I am trying to create an Express dynamic web page on Node.js. I want to perform the following logic on the server (Firebase Cloud Functions) at path /
:
my_home_page.html
my_login_page.html
So I need to implement the isClientLoggedIn
function:
import * as functions from 'firebase-functions';
import * as express from 'express';
const app = express();
export const redirects = functions.https.onRequest(app);
app.set('views', './res');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
function isClientLoggedIn(request: Request): bool {
return // ???
}
app.get('/', (req, res) => {
if (isClientLoggedIn(req)) {
res.render('my_home_page.html');
} else {
res.render('my_login_page.html');
}
});
However, I have checked the Firebase Auth documentation and I do not know how to implement this function. Is it possible to check the clients' login state from the HTTP request, or are there other ways to render the correct content?
Upvotes: 4
Views: 4096
Reputation: 598817
There is no built-in way in HTTPS-triggered Cloud Functions to know if the request came from a user that is signed in. If you want to be able to determine that, you'll need to pass the user's ID token along with the request, and decode and verify that token in the Cloud Function. For a simple example of this, see Authenticated JSON API in the function-samples
repo.
Upvotes: 5