I'm working on a Nuxt project (for the first time) where the API that I'm consuming requires us to authenticate via client oAuth2. The application holds the client ID & client secret but I don't want to expose this data to the user.
My plan is to retrieve the generated bearer token on the server side using the oAuth token endpoint. Then I can locally store the generated token on the filesystem and push it through to the user.
The problem I'm having is that I can't work out how best to store the bearer token. I've created a serverMiddleware
that I'm trying to use FS to write the generated token to file. The problem with this is that I can't actually seem to use FS as it throws an error when being required in. I thought the idea of serverMiddleware is that it always runs on the server side?
I'm getting -
[HMR] bundle 'client' has 1 warnings
client.js?1b93:196 ./middleware/server/bearerToken.tsModule not found: Error: Can't resolve 'fs' in '/middleware/server'
Here's the middleware I've created -
const myServerMiddleware = (req, res, next) => {
try {
const fs = require('fs');
// ToDo: read and/or generate bearer token
} catch (e) {
console.log('No FS available', e);
}
next();
};
export default {
path: '/',
handler: myServerMiddleware,
mode: 'server'
};
And this is being loaded within my configuration using -
{
serverMiddleware: [
'@/middleware/server/bearerToken.ts'
],
}
I hope someone can point me in the right direction here!