Currently, I am using npm disco-oauth to authenticate with Discord. After authenticating in the main app.js file, I store the userKey in a cookie using cookie-parser. This allows me to access user information and the guilds that the user is a part of. However, I need to pass this user and filtered guilds information to a routes file where I handle dashboard routes. I prefer not to use queries or methods involving the URL to avoid cluttering the code.
The issue arises when attempting to provide cookieParser with an object like the one below:
app.get('/auth/discord', (req, res) => {
if (req.cookies.userConfig.userKey) res.redirect('/guilds');
else res.redirect(oauthClient.authCodeLink);
})
app.get('/login', async (req, res) => {
try {
let userKey = await oauthClient.getAccess(req.query.code);
let userConfig = {
userKey: userKey,
userID: " ",
guilds: Guilds[]
}
await res.cookie('userConfig', userConfig);
await res.redirect('/guilds');
} catch (err) {
res.render('error', {
message: err.message,
error: err
});
}
});
app.get('/logout', (req, res) => {
res.clearCookie("userConfig")
res.redirect('/')
})
app.get('/guilds', async (req, res) => {
if (!req.cookies.userConfig.userKey) res.redirect('/');
else {
try {
let user = await oauthClient.getUser(req.cookies.userConfig.userKey);
let GUILDS = await oauthClient.getGuilds(req.cookies.userConfig.userKey)
var Guilds = []
await GUILDS.forEach(async g => {
if (g.permissions.includes("MANAGE_GUILD")) {
await Guilds.push(g)
}
})
await res.render('guilds', {
user,
Guilds,
Servers: client.guilds.cache,
AdminIDs: keys.admin.discord_ids
});
} catch (err) {
console.log(err)
res.render('error', {
message: err.message,
error: err
});
}
}
})
The challenge lies in encountering a "cannot read property userKey of undefined" error when trying to implement the new cookie object. Strangely, it works fine when only passing the userKey. Is there a limitation on cookies or is my approach incorrect? Any guidance would be appreciated.
If the initial implementation remains problematic, I'm hesitant to modify the cookie further (referencing the comment in the code) and utilize it in other files and routes.
LATEST FUNCTIONAL VERSION
app.get('/auth/discord', (req, res) => {
if (req.cookies.userKey) res.redirect('/guilds');
else res.redirect(oauthClient.authCodeLink);
})
app.get('/login', async (req, res) => {
try {
let userKey = await oauthClient.getAccess(req.query.code);
await res.cookie('userKey', userKey);
await res.redirect('/guilds');
} catch (err) {
res.render('error', {
message: err.message,
error: err
});
}
});
app.get('/logout', (req, res) => {
res.clearCookie("userKey")
res.redirect('/')
})
app.get('/guilds', async (req, res) => {
if (!req.cookies.userKey) res.redirect('/');
else {
try {
let user = await oauthClient.getUser(req.cookies.userKey);
let GUILDS = await oauthClient.getGuilds(req.cookies.userKey)
var Guilds = []
await GUILDS.forEach(async g => {
if (g.permissions.includes("MANAGE_GUILD")) {
await Guilds.push(g)
}
})
await res.render('guilds', {
user,
Guilds,
Servers: client.guilds.cache,
AdminIDs: keys.admin.discord_ids
});
} catch (err) {
console.log(err)
res.render('error', {
message: err.message,
error: err
});
}
}
})