I'm developing an application using the Spotify Web API. Every time a user logs in, the correct user data is being processed and stored to a Redis session. However on the FIRST login attempt, when I redirect to another route, a new, the app attempts to access a blank, new session. On subsequent attempts, the app accesses the correct session, and the app behaves normally. The cycle repeats every time I log out.
Relevant code:
Redis Store config in app.js
let RedisStore = require('connect-redis')(session);
let client = redis.createClient({
host: 'localhost',
port: 6379,
//password: REDIS_KEY,
db: 1
})
client.unref();
client.on('error', console.log)
app.use(session({
store: new RedisStore({ client }),
secret: REDIS_KEY,
resave: false,
saveUninitialized: false,
rolling: true,
unset: 'destroy',
cookie: { maxAge: 3600000, sameSite: true, secure: false, httpOnly: false } // 1 hr
}))
Spotify API Callback route (the session is being fully saved here)
let set_json = new Promise((resolve, reject) =>{
FUNCTIONS.set_json(req, new CLASSES.user_info(user_id, display_name, profile_picture, playlists));
if (req.session.json) resolve(); else reject("error setting JSON");
})
set_json.then(
function(set_success) {
console.log("ID: " + req.sessionID);
req.session.save(function(){
res.redirect('/home');
});
},
function(set_error){
console.log(set_error);
}
);
set_json function
exports.set_json = function(req, data) {
let set_promise = new Promise((resolve, reject) =>{
req.session.json = JSON.parse(JSON.stringify(data));
if (req.session.json) resolve("success!"); else reject("error!");
});
set_promise.then(
function(success){
console.log(success);
},
function(failure) {
console.log(failure);
console.log("something went wrong!");
}
);
}
Home router, where the req.session I'd just set has seemingly been lost on the first login attempt. Subsequent login attempts successfully load the correct data.
exports.get_home = function(req, res, next) {
console.log("ID: " + req.sessionID);
res.render('home', { title: 'Spotify Playlist Optimizer', user: req.session.json});
}
Some example output:
First login attempt:
//Callback session id, holds correct data
ID: bvLDb6OwlBAPASAQqHBni2uIFfe-ASU3
//router session id, a blank, new session
ID: m2gEaduOyjde07b7aWvRVya4WkPze5M8
Subsequent login attempts until logout:
// Callback session id
ID: bvLDb6OwlBAPASAQqHBni2uIFfe-ASU3
// Router session id
ID: bvLDb6OwlBAPASAQqHBni2uIFfe-ASU3
// (matching and holding all needed data)