1

I am trying to make a dynamic website in Cloud Functions for Firebase.

However, I am stuck at this point.

What I am trying to achieve is that I want to save particular form details and then push them to the DB if the user is logged in. If the user is not logged in I would like to save those forms details somewhere and then redirect the user to login. When he does login I save the form details under his userid.

Now I thought of using firebase-cookie-session for this. Since it's the only one allowed in firebase cloud functions.

However, my problem is that when the user is redirected to login. I no longer have access to the session in that request.

This is how I approached this:

I setup my express app:

var session = require('firebase-cookie-session');
app.use(session({
    keys: ['cookie_secret'],
    name: 'session',
    sameSite:true,
    proxy: true,
    resave: true,
    saveUninitialized: true,
    maxAge: 360*12*24*36*36
}));

then in post I save data in session: -> console.log(req.session.packageDetails) works perfectly

homepageRouter.post("/save-form-details", function (req, res, next) {
    const packageDetails = JSON.parse(JSON.stringify(req.body));
    firebaseController.isUserSignedIn().then(signedinUser => {
        if(signedinUser){
            firebaseController.getCurrentUser().then(currentUser => {
                firebaseController.putPackageToUser(currentUser, packageDetails).then(success => {
                    return success;
                });
            }).catch(errorMessage => {
                console.log(errorMessage);
            });
        } else {
            req.session.userSignedIn = false;
            req.session.packageIncluded = true;
            req.session.packageDetails = packageDetails;

            console.log('Hello Hello Hello Hello Hello Hello');
            console.log(req.session.packageDetails);
            res.send({
                userSignedIn: false,
                redirectTo: '/signup-login'
            });
        }
        console.log('User Status: ' + signedinUser);
        console.log('Server Side: ' + JSON.parse(JSON.stringify(req.body)));

    });
});

After the redirection from the above to below the session goes to undefined, No idea why.

signupLoginRouter.post("/login", function (req, res, next) {
    const accountJSON = JSON.stringify(req.body);
    let account = JSON.parse(accountJSON);
    console.log('HEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEELP');
    console.log(req.session.packageDetails);
}

I think I either setup something wrong or understood something wrong. If anyone can help. Thay would be appreciated

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Kamil Kamili
  • 1,757
  • 5
  • 24
  • 39

0 Answers0