A broad question in the context of learning and experimenting with nextjs on a hobby app (using the old folder layout):
I have a single /API route that simply fetches another external api for content. This other external api requires a bearer token, and in my naive implementation I just got the token the via a manual POST and saved the string token as env variable, which is clearly not the solution since it expires every month and I need a way to automate this.
My first thought is to somehow create a middleware (overshoot for a single handler but I'm trying to exercise), that checks if the auth token is available / not expired and if not to refetches it and pass it to the next function which will effectively fetch for data. Here is where I'm missing it, where should such token be stored (because I guess requesting another auth for each page visit is not correct) ? Necessarily on an external service, like a db that needs to be queried? Because my uneducated guess is cookies are on the browser side, and the API that is run on the server does not have access to that? Wrong? And if so, is it safe to store such a token in the browser as a cookie?