1

I am trying to create a cookie every time a new user registers for my app, indicating that it is their first time logged in it. I have found this thread and roughly followed it but it isn't working.

Here is my current code:

// setting up the middle ware
export default function () {
  return function () {
    const app = this
    app.use(cookieParser())
    app.post('/app/signup/end', [firstTimeCookie(app), signUp(app)])
  }
}

The middleware that isn't working is firstTimeCookie(), signUp() works fine.

// in firstTimeCookie:

export default function (app) {
  return function (req, res, next) {
    const cname = 'FIRST_SIGNUP'
    const cvalue = true
    var cookie = req.cookies.cookieName

    if (!cookie) {
      res.cookie(cname, cvalue, { maxAge: 1000 * 60 * 60 * 24 * 999999 })
    } else {
      console.log('cookie already exists')
    }
    next()
  }
}

What am I doing wrong?

mscdex
  • 104,356
  • 15
  • 192
  • 153
theJuls
  • 6,788
  • 14
  • 73
  • 160

1 Answers1

0

The problem is that cookie is undefined.

Change:

var cookie = req.cookies.cookieName

For:

var cookie = req.cookies['FIRST_SIGNUP'];

Then you'll be able to handle the cookie:

if (!cookie) {
    console.log('Cookie not there');
    resp.cookie('FIRST_SIGNUP', 'Hello World', { maxAge: 1000 * 60 * 60 * 24 * 999999 });
} else {
    console.log(cookie);
    console.log('cookie already exists');
}

next();
M3talM0nk3y
  • 1,382
  • 14
  • 22