Next.js Discord

Discord Forum

Backend token and server-side storage

Answered
Oak shoot sawfly posted this in #help-forum
Open in Discord
Oak shoot sawflyOP
So i have a backend that returns a jwt token when auth is successful, besides the jwt token nextAuth has.
My question is when I try to make requests to this backend I need to set the bearer token, in this case how can I make bearer token from backend be seen globally on server-side storage?
I tried a singleton pattern in /app/utils like this[1]. But in middleware it returns no-token although when trying to call "getInstance"method from an api route it returns the backend's jwt. What better way is to do so?
Thank you for help!

Note that i use NextAuth so it would be helpful if a such functionalty would exists(to store multiple jwts based on session on server-side) but i couldn't find anything...

1.
var Singleton = (function () {
    var instance;

    function createInstance() {
        return "no-token";
    }

    return {
        getInstance: function () {
            if (!instance) {
                instance = createInstance();
            }
            return instance;
        },
        setToken: function (token: string) {
            instance = token;
            return instance;
        }
    };
})();

source: https://www.dofactory.com/javascript/design-patterns/singleton

2 Replies

Oak shoot sawflyOP
Answer
Oak shoot sawflyOP
I think server side cookies is the right way